Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- declare(strict_types=1);
- /**
- * File: AppendProduct.php
- * @package LizardMedia
- * @category CategorySales
- * @author Krzysztof Kuźniar <krzysztof.kuzniar@lizardmedia.pl>
- * @copyright Copyright (C) 2019 LizardMedia (http://www.lizardmedia.pl)
- */
- namespace LizardMedia\CategorySale\Model\SynchronizeCategory;
- use Exception;
- use LizardMedia\CategorySale\Api\ConfigProviderInterface;
- use LizardMedia\CategorySale\Api\SynchronizeCategory\AppendProductInterface;
- use LizardMedia\CategorySale\Api\SynchronizeCategory\ProductPositionInterface;
- use LizardMedia\CategorySale\Exception\AppendProductException;
- use Smile\ElasticsuiteVirtualCategory\Model\ResourceModel\Category\Product\Position;
- use Magento\Catalog\Model\Category;
- use Magento\Catalog\Api\CategoryRepositoryInterface;
- use Magento\Catalog\Api\Data\ProductInterface;
- use Magento\Catalog\Api\Data\CategoryProductLinkInterface;
- use Magento\Catalog\Api\Data\CategoryProductLinkInterfaceFactory;
- use Magento\Catalog\Api\CategoryLinkRepositoryInterface;
- use Magento\Catalog\Model\ResourceModel\Category as CategoryResource;
- use Magento\Catalog\Model\ResourceModel\Product\Collection;
- use Magento\Framework\Exception\NoSuchEntityException;
- use Magento\Framework\Serialize\SerializerInterface;
- use Psr\Log\LoggerInterface;
- /**
- * Class AppendProduct
- * @package LizardMedia\CategorySale\Model\SynchronizeCategory
- */
- class AppendProduct implements AppendProductInterface
- {
- /**
- * @var CategoryProductLinkInterfaceFactory
- */
- private $categoryProductLinkInterfaceFactory;
- /**
- * @var CategoryLinkRepositoryInterface
- */
- private $categoryLinkRepository;
- /**
- * @var ConfigProviderInterface
- */
- private $configProvider;
- /**
- * @var ProductPositionInterface
- */
- private $productPosition;
- /**
- * @var CategoryRepositoryInterface
- */
- private $categoryRepository;
- /**
- * @var SerializerInterface
- */
- private $serializer;
- /**
- * @var CategoryResource
- */
- private $categoryResource;
- /**
- * @var LoggerInterface
- */
- private $logger;
- /**
- * @var string
- */
- private $saleCategoryId;
- /**
- * @var Position
- */
- private $smileVirtualCategoryPositionResource;
- /**
- * AppendProduct constructor.
- * @param CategoryProductLinkInterfaceFactory $categoryProductLinkInterfaceFactory
- * @param CategoryLinkRepositoryInterface $categoryLinkRepository
- * @param ConfigProviderInterface $configProvider
- * @param ProductPositionInterface $productPosition
- * @param CategoryRepositoryInterface $categoryRepository
- * @param CategoryResource $categoryResource
- * @param SerializerInterface $serializer
- * @param LoggerInterface $logger
- * @param Position $smileVirtualCategoryPositionResource
- */
- public function __construct(
- CategoryProductLinkInterfaceFactory $categoryProductLinkInterfaceFactory,
- CategoryLinkRepositoryInterface $categoryLinkRepository,
- ConfigProviderInterface $configProvider,
- ProductPositionInterface $productPosition,
- CategoryRepositoryInterface $categoryRepository,
- CategoryResource $categoryResource,
- SerializerInterface $serializer,
- LoggerInterface $logger,
- Position $smileVirtualCategoryPositionResource
- ) {
- $this->categoryProductLinkInterfaceFactory = $categoryProductLinkInterfaceFactory;
- $this->categoryLinkRepository = $categoryLinkRepository;
- $this->configProvider = $configProvider;
- $this->productPosition = $productPosition;
- $this->categoryRepository = $categoryRepository;
- $this->categoryResource = $categoryResource;
- $this->serializer = $serializer;
- $this->logger = $logger;
- $this->smileVirtualCategoryPositionResource = $smileVirtualCategoryPositionResource;
- $this->saleCategoryId = $this->configProvider->getSaleCategory();
- }
- /**
- * @param Collection $collection
- * @param array $productIds
- * @throws AppendProductException
- * @throws NoSuchEntityException
- */
- public function assignSimple(Collection $collection, array $productIds): void
- {
- $collection->clear();
- $collection->addAttributeToFilter('entity_id', ['in' => $productIds]);
- /** @var ProductInterface $product */
- foreach ($collection->getItems() as $product) {
- /** @var CategoryProductLinkInterface $categoryProductLink */
- $categoryProductLink = $this->categoryProductLinkInterfaceFactory->create();
- $categoryProductLink->setSku($product->getSku());
- $categoryProductLink->setCategoryId($this->saleCategoryId);
- $categoryProductLink->setPosition($this->productPosition->calculate((int)$product->getEntityId()));
- try {
- $this->categoryLinkRepository->save($categoryProductLink);
- } catch (Exception $exception) {
- $this->logger->error($exception->getMessage());
- throw new AppendProductException(__('Append Product Exception'), $exception);
- }
- }
- // $this->setProductPositionOnCategoryListing($productIds);
- }
- public function assignGrouped(array $simpleProductIds): void
- {
- }
- /**
- * @param array $productIds
- * @return void
- * @throws NoSuchEntityException
- */
- private function setProductPositionOnCategoryListing(array $productIds): void
- {
- /** @var Category $category */
- $category = $this->categoryRepository->get((int)$this->saleCategoryId);
- $position = [];
- foreach ($productIds as $key => $value) {
- $position[$value] = $this->productPosition->calculate((int) $value);
- }
- // arsort($position);
- $category->setSortedProducts($this->serializer->serialize($position));
- // $this->smileVirtualCategoryPositionResource->saveProductPositions($category);
- $this->categoryResource->save($category);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement