Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.11 KB | None | 0 0
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * File: AppendProduct.php
  5.  * @package     LizardMedia
  6.  * @category    CategorySales
  7.  * @author      Krzysztof Kuźniar <krzysztof.kuzniar@lizardmedia.pl>
  8.  * @copyright   Copyright (C) 2019 LizardMedia (http://www.lizardmedia.pl)
  9.  */
  10. namespace LizardMedia\CategorySale\Model\SynchronizeCategory;
  11.  
  12. use Exception;
  13. use LizardMedia\CategorySale\Api\ConfigProviderInterface;
  14. use LizardMedia\CategorySale\Api\SynchronizeCategory\AppendProductInterface;
  15. use LizardMedia\CategorySale\Api\SynchronizeCategory\ProductPositionInterface;
  16. use LizardMedia\CategorySale\Exception\AppendProductException;
  17. use Smile\ElasticsuiteVirtualCategory\Model\ResourceModel\Category\Product\Position;
  18. use Magento\Catalog\Model\Category;
  19. use Magento\Catalog\Api\CategoryRepositoryInterface;
  20. use Magento\Catalog\Api\Data\ProductInterface;
  21. use Magento\Catalog\Api\Data\CategoryProductLinkInterface;
  22. use Magento\Catalog\Api\Data\CategoryProductLinkInterfaceFactory;
  23. use Magento\Catalog\Api\CategoryLinkRepositoryInterface;
  24. use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
  25. use Magento\Catalog\Model\ResourceModel\Product\Collection;
  26. use Magento\Framework\Exception\NoSuchEntityException;
  27. use Magento\Framework\Serialize\SerializerInterface;
  28. use Psr\Log\LoggerInterface;
  29.  
  30. /**
  31.  * Class AppendProduct
  32.  * @package LizardMedia\CategorySale\Model\SynchronizeCategory
  33.  */
  34. class AppendProduct implements AppendProductInterface
  35. {
  36.     /**
  37.      * @var CategoryProductLinkInterfaceFactory
  38.      */
  39.     private $categoryProductLinkInterfaceFactory;
  40.  
  41.     /**
  42.      * @var CategoryLinkRepositoryInterface
  43.      */
  44.     private $categoryLinkRepository;
  45.  
  46.     /**
  47.      * @var ConfigProviderInterface
  48.      */
  49.     private $configProvider;
  50.  
  51.     /**
  52.      * @var ProductPositionInterface
  53.      */
  54.     private $productPosition;
  55.  
  56.     /**
  57.      * @var CategoryRepositoryInterface
  58.      */
  59.     private $categoryRepository;
  60.  
  61.     /**
  62.      * @var SerializerInterface
  63.      */
  64.     private $serializer;
  65.  
  66.     /**
  67.      * @var CategoryResource
  68.      */
  69.     private $categoryResource;
  70.  
  71.     /**
  72.      * @var LoggerInterface
  73.      */
  74.     private $logger;
  75.  
  76.     /**
  77.      * @var string
  78.      */
  79.     private $saleCategoryId;
  80.     /**
  81.      * @var Position
  82.      */
  83.     private $smileVirtualCategoryPositionResource;
  84.  
  85.     /**
  86.      * AppendProduct constructor.
  87.      * @param CategoryProductLinkInterfaceFactory $categoryProductLinkInterfaceFactory
  88.      * @param CategoryLinkRepositoryInterface $categoryLinkRepository
  89.      * @param ConfigProviderInterface $configProvider
  90.      * @param ProductPositionInterface $productPosition
  91.      * @param CategoryRepositoryInterface $categoryRepository
  92.      * @param CategoryResource $categoryResource
  93.      * @param SerializerInterface $serializer
  94.      * @param LoggerInterface $logger
  95.      * @param Position $smileVirtualCategoryPositionResource
  96.      */
  97.     public function __construct(
  98.         CategoryProductLinkInterfaceFactory $categoryProductLinkInterfaceFactory,
  99.         CategoryLinkRepositoryInterface $categoryLinkRepository,
  100.         ConfigProviderInterface $configProvider,
  101.         ProductPositionInterface $productPosition,
  102.         CategoryRepositoryInterface $categoryRepository,
  103.         CategoryResource $categoryResource,
  104.         SerializerInterface $serializer,
  105.         LoggerInterface $logger,
  106.         Position $smileVirtualCategoryPositionResource
  107.     ) {
  108.         $this->categoryProductLinkInterfaceFactory = $categoryProductLinkInterfaceFactory;
  109.         $this->categoryLinkRepository = $categoryLinkRepository;
  110.         $this->configProvider = $configProvider;
  111.         $this->productPosition = $productPosition;
  112.         $this->categoryRepository = $categoryRepository;
  113.         $this->categoryResource = $categoryResource;
  114.         $this->serializer = $serializer;
  115.         $this->logger = $logger;
  116.         $this->smileVirtualCategoryPositionResource = $smileVirtualCategoryPositionResource;
  117.         $this->saleCategoryId = $this->configProvider->getSaleCategory();
  118.     }
  119.  
  120.     /**
  121.      * @param Collection $collection
  122.      * @param array $productIds
  123.      * @throws AppendProductException
  124.      * @throws NoSuchEntityException
  125.      */
  126.     public function assignSimple(Collection $collection, array $productIds): void
  127.     {
  128.         $collection->clear();
  129.         $collection->addAttributeToFilter('entity_id', ['in' => $productIds]);
  130.  
  131.         /** @var ProductInterface $product */
  132.         foreach ($collection->getItems() as $product) {
  133.             /** @var CategoryProductLinkInterface $categoryProductLink */
  134.             $categoryProductLink = $this->categoryProductLinkInterfaceFactory->create();
  135.             $categoryProductLink->setSku($product->getSku());
  136.             $categoryProductLink->setCategoryId($this->saleCategoryId);
  137.             $categoryProductLink->setPosition($this->productPosition->calculate((int)$product->getEntityId()));
  138.  
  139.             try {
  140.                 $this->categoryLinkRepository->save($categoryProductLink);
  141.             } catch (Exception $exception) {
  142.                 $this->logger->error($exception->getMessage());
  143.                 throw new AppendProductException(__('Append Product Exception'), $exception);
  144.             }
  145.         }
  146.  
  147. //        $this->setProductPositionOnCategoryListing($productIds);
  148.     }
  149.  
  150.  
  151.     public function assignGrouped(array $simpleProductIds): void
  152.     {
  153.        
  154.  
  155.  
  156.     }
  157.  
  158.     /**
  159.      * @param array $productIds
  160.      * @return void
  161.      * @throws NoSuchEntityException
  162.      */
  163.     private function setProductPositionOnCategoryListing(array $productIds): void
  164.     {
  165.         /** @var Category $category */
  166.         $category = $this->categoryRepository->get((int)$this->saleCategoryId);
  167.         $position = [];
  168.  
  169.         foreach ($productIds as $key => $value) {
  170.             $position[$value] = $this->productPosition->calculate((int) $value);
  171.         }
  172.  
  173. //        arsort($position);
  174.         $category->setSortedProducts($this->serializer->serialize($position));
  175.  
  176. //        $this->smileVirtualCategoryPositionResource->saveProductPositions($category);
  177.         $this->categoryResource->save($category);
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement