![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/cartforge.co/app/code/Webkul/PrivateShop/Model/Plugin/ |
<?php /** * Webkul Software * * @category Webkul * @package Webkul_PrivateShop * @author Webkul Software Private Limited * @copyright Webkul Software Private Limited (https://webkul.com) * @license https://store.webkul.com/license.html */ namespace Webkul\PrivateShop\Model\Plugin; use Magento\Framework\App\State; use Magento\Framework\App\RequestInterface; use Magento\Catalog\Api\CategoryRepositoryInterface; use Magento\Catalog\Api\Data\ProductInterface; use Webkul\PrivateShop\Api\PrivateProductRepositoryInterface; use Webkul\PrivateShop\Api\Data\PrivateProductInterfaceFactory; use Webkul\PrivateShop\Model\ResourceModel\PrivateProduct as PrivateProductResource; /** * Class ProductPlugin * Webkul\PrivateShop\Model\Plugin */ class ProductPlugin { /** * @var State */ private $state; /** * @var \Magento\Framework\Serialize\Serializer\Json */ protected $jsonSerializer; /** * @var PrivateProductRepositoryInterface */ protected $privateProductRepository; /** * @var PrivateProductInterfaceFactory */ protected $privateProductFactory; /** * @var PrivateProductResource */ protected $resource; /** * @var RequestInterface */ protected $request; /** * * @param State $state * @param PrivateProductRepositoryInterface $privateProductRepository * @param PrivateProductInterfaceFactory $privateProductFactory * @param PrivateProductResource $resource * @param RequestInterface $request * @param \Magento\Framework\Serialize\Serializer\Json $jsonSerializer */ public function __construct( State $state, PrivateProductRepositoryInterface $privateProductRepository, PrivateProductInterfaceFactory $privateProductFactory, PrivateProductResource $resource, RequestInterface $request, \Magento\Framework\Serialize\Serializer\Json $jsonSerializer ) { $this->state = $state; $this->privateProductRepository = $privateProductRepository; $this->privateProductFactory = $privateProductFactory; $this->resource = $resource; $this->request = $request; $this->jsonSerializer = $jsonSerializer; } /** * Around Save * * @param ProductInterface $subject * @param callable $proceed * * @return array */ public function aroundSave(ProductInterface $subject, callable $proceed) { $data = []; if ($this->state->getAreaCode() == 'adminhtml' && is_array($subject->getData('product_private_group')) ) { $groups = $subject->getData('product_private_group'); $grouped = $this->jsonSerializer->serialize( $subject->getData('product_private_group') ); $subject->setData('product_private_group', $grouped); $updateData = []; $alreadySaved = []; $newData = []; $collection = $this->privateProductFactory->create()->getCollection() ->addFieldToFilter('product_id', ['eq' => $subject->getId()]); if ($collection->getSize()) { foreach ($collection as $model) { if (!in_array($model->getGroupId(), $groups)) { $this->privateProductRepository->deleteById($model->getId()); } else { $alreadySaved[] = $model->getGroupId(); $updateData[] = [ 'entity_id'=> $model->getId(), 'group_id' => $model->getGroupId(), ]; } } } $data = $subject; $data = $proceed(); $diffrence = array_diff($groups, $alreadySaved); if (!empty($diffrence)) { foreach ($diffrence as $groupId) { $newData[] = [ 'product_id' => $subject->getId(), 'group_id' => $groupId, 'store_id' => $subject->getStoreId() ]; } } $this->resource->saveMultiple($newData); $this->resource->updateMultiple($updateData); } else { $productData = $this->request->getParam('product'); if (!isset($productData['product_private_group'])) { $subject->setData('product_private_group', ''); } $data = $subject; $data = $proceed(); } return $data; } }