custom/plugins/DtgsGoogleTagManagerSw6/src/Components/Helper/ProductHelper.php line 27

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: constantin
  5.  * Date: 28.02.17
  6.  * Time: 15:35
  7.  */
  8. namespace Dtgs\GoogleTagManager\Components\Helper;
  9. use Shopware\Core\Content\Product\Exception\ProductNotFoundException;
  10. use Shopware\Core\Content\Product\ProductCollection;
  11. use Shopware\Core\Content\Product\ProductEntity;
  12. use Shopware\Core\Content\Product\SalesChannel\Detail\AbstractProductDetailRoute;
  13. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  14. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  16. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  17. use Symfony\Component\HttpFoundation\Request;
  18. class ProductHelper
  19. {
  20.     public function __construct(EntityRepositoryInterface $productRepository,
  21.                                 AbstractProductDetailRoute $productDetailRoute)
  22.     {
  23.         $this->productRepository $productRepository;
  24.         $this->productDetailRoute $productDetailRoute;
  25.     }
  26.     /**
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     private EntityRepositoryInterface $productRepository;
  30.     /**
  31.      * @param $productId
  32.      * @param SalesChannelContext $context
  33.      * @return ProductEntity|null
  34.      */
  35.     public function getProductyById($productId$context)
  36.     {
  37.         $criteria = new Criteria([$productId]);
  38.         $criteria->addAssociation('seoUrls');
  39.         /** @var ProductCollection $productCollection */
  40.         $productCollection $this->productRepository->search($criteria$context->getContext())->getEntities();
  41.         return $productCollection->get($productId);
  42.     }
  43.     /**
  44.      * @param $productId
  45.      * @param $context
  46.      * @return SalesChannelProductEntity|null
  47.      */
  48.     public function getSalesChannelProductEntityByProductId($productId$context)
  49.     {
  50.         try {
  51.             $result $this->productDetailRoute->load($productId, new Request(), $context, new Criteria());
  52.         }
  53.         catch (ProductNotFoundException $exception) {
  54.             return null;
  55.         }
  56.         catch (\Exception $exception) {
  57.             return null;
  58.         }
  59.         return $result->getProduct();
  60.     }
  61. }