SHARE
TWEET

Untitled

a guest Sep 19th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. namespace Magebay\Marketplace\Controller\Product\Compare;
  4.  
  5. use Magento\Catalog\Api\ProductRepositoryInterface;
  6. use Magento\Framework\App\ResponseInterface;
  7. use Magento\Framework\Data\Form\FormKey\Validator;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Catalog\Controller\Product\Compare;
  10. use Magento\Framework\Controller\ResultInterface;
  11. use Magento\Framework\Controller\Result\JsonFactory;
  12. use Magento\Framework\App\Action\Context;
  13. use Magento\Framework\View\Result\PageFactory;
  14. use Magento\Catalog\Model\Product\Compare\ItemFactory;
  15. use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory;
  16. use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection as CompareCollection;
  17. use Magento\Customer\Model\Session;
  18. use Magento\Customer\Model\Visitor;
  19. use Magento\Catalog\Model\Product\Compare\ListCompare;
  20. use Magento\Store\Model\StoreManagerInterface;
  21. use Magento\Framework\Webapi\Exception;
  22. use Magento\Catalog\Helper\Product\Compare as CompareHelper;
  23. use Magento\Framework\Escaper;
  24.  
  25. /**
  26.  * Class Add
  27.  *
  28.  * @package Compare
  29.  */
  30. class Add extends Compare
  31. {
  32.     /**
  33.      * Result page factory
  34.      *
  35.      * @var JsonFactory;
  36.      */
  37.     protected $resultJsonFactory;
  38.  
  39.     /**
  40.      * @var CompareHelper
  41.      */
  42.     protected $compareHelper;
  43.  
  44.     /**
  45.      * @var Validator
  46.      */
  47.     protected $formKeyValidator;
  48.  
  49.     /**
  50.      * @var Visitor
  51.      */
  52.     protected $customerVisitor;
  53.  
  54.     /**
  55.      * @var Session
  56.      */
  57.     protected $customerSession;
  58.  
  59.     /**
  60.      * @var StoreManagerInterface
  61.      */
  62.     protected $storeManager;
  63.  
  64.     /**
  65.      * @var ListCompare
  66.      */
  67.     protected $catalogProductCompareList;
  68.  
  69.     /**
  70.      * @var Escaper
  71.      */
  72.     protected $escaper;
  73.  
  74.  
  75.     /**
  76.      * Add constructor.
  77.      * @param JsonFactory $resultJsonFactory
  78.      * @param Context $context
  79.      * @param ItemFactory $compareItemFactory
  80.      * @param CollectionFactory $itemCollectionFactory
  81.      * @param Session $customerSession
  82.      * @param Visitor $customerVisitor
  83.      * @param ListCompare $catalogProductCompareList
  84.      * @param \Magento\Catalog\Model\Session $catalogSession
  85.      * @param StoreManagerInterface $storeManager
  86.      * @param Validator $formKeyValidator
  87.      * @param PageFactory $resultPageFactory
  88.      * @param ProductRepositoryInterface $productRepository
  89.      * @param CompareHelper $compareHelper
  90.      * @param Escaper $escaper
  91.      */
  92.     public function __construct(
  93.         JsonFactory $resultJsonFactory,
  94.         Context $context,
  95.         ItemFactory $compareItemFactory,
  96.         CollectionFactory $itemCollectionFactory,
  97.         Session $customerSession,
  98.         Visitor $customerVisitor,
  99.         ListCompare $catalogProductCompareList,
  100.         \Magento\Catalog\Model\Session $catalogSession,
  101.         StoreManagerInterface $storeManager,
  102.         Validator $formKeyValidator,
  103.         PageFactory $resultPageFactory,
  104.         ProductRepositoryInterface $productRepository,
  105.         CompareHelper $compareHelper,
  106.         Escaper $escaper
  107.     ) {
  108.         parent::__construct(
  109.             $context,
  110.             $compareItemFactory,
  111.             $itemCollectionFactory,
  112.             $customerSession,
  113.             $customerVisitor,
  114.             $catalogProductCompareList,
  115.             $catalogSession,
  116.             $storeManager,
  117.             $formKeyValidator,
  118.             $resultPageFactory,
  119.             $productRepository
  120.         );
  121.         $this->resultJsonFactory = $resultJsonFactory;
  122.         $this->compareHelper = $compareHelper;
  123.         $this->formKeyValidator = $formKeyValidator;
  124.         $this->customerVisitor = $customerVisitor;
  125.         $this->customerSession = $customerSession;
  126.         $this->storeManager = $storeManager;
  127.         $this->catalogProductCompareList = $catalogProductCompareList;
  128.         $this->escaper = $escaper;
  129.     }
  130.  
  131.     /**
  132.      * @return ResponseInterface|ResultInterface
  133.      * @throws NoSuchEntityException
  134.      */
  135.     public function execute()
  136.     {
  137.         $resultJson = $this->resultJsonFactory->create();
  138.  
  139.         if (!$this->formKeyValidator->validate($this->getRequest())) {
  140.             $resultJson->setHttpResponseCode(Exception::HTTP_INTERNAL_ERROR);
  141.  
  142.             return $resultJson->setData(['response' => 'Not valid data']);
  143.         }
  144.  
  145.         $productId = (int)$this->getRequest()->getParam('product');
  146.  
  147.         if ($productId && ($this->customerVisitor->getId()
  148.             || $this->customerSession->isLoggedIn())
  149.         ) {
  150.             $storeId = $this->storeManager->getStore()->getId();
  151.             try {
  152.                 $product = $this->productRepository
  153.                     ->getById($productId, false, $storeId);
  154.             } catch (NoSuchEntityException $e) {
  155.                 $product = null;
  156.             }
  157.  
  158.             if ($product) {
  159.                 if ($this->findCompare($productId)) {
  160.                     $resultJson->setHttpResponseCode(201);
  161.  
  162.                     $this->messageManager->addNoticeMessage(
  163.                         'addCompareSuccessMessage',
  164.                         [
  165.                             'product_name' => $this->escaper
  166.                                 ->escapeHtml($product->getName()),
  167.                             'compare_list_url' => $this->_url->getUrl(
  168.                                 'catalog/product_compare'
  169.                             )
  170.                         ]
  171.                     );
  172.  
  173.  
  174.                     return $resultJson->setData(['response' => 'All right!']);
  175.                 }
  176.  
  177.                 $this->catalogProductCompareList->addProduct($product);
  178.                 $this->messageManager->addComplexSuccessMessage(
  179.                     'addCompareSuccessMessage',
  180.                     [
  181.                         'product_name' => $this->escaper
  182.                             ->escapeHtml($product->getName()),
  183.                         'compare_list_url' => $this->_url->getUrl(
  184.                             'catalog/product_compare'
  185.                         )
  186.                     ]
  187.                 );
  188.  
  189.  
  190.                 $this->_eventManager
  191.                     ->dispatch(
  192.                         'catalog_product_compare_add_product', [
  193.                             'product' => $product
  194.                         ]
  195.                     );
  196.             }
  197.  
  198.             $this->compareHelper->calculate();
  199.         }
  200.  
  201.         $resultJson->setHttpResponseCode(200);
  202.  
  203.         return $resultJson->setData(['response' => 'All right!']);
  204.     }
  205.  
  206.     /**
  207.      * @param int $productId
  208.      * @return bool
  209.      */
  210.     protected function findCompare(int $productId): bool
  211.     {
  212.         $productsCollection = $this->getCompareOrderCollectionByCustomerId(4);
  213.         $isset = false;
  214.         $count = count($productsCollection);
  215.         $sdfdsf = 23;
  216.         foreach ($productsCollection as $product) {
  217.             $product;
  218.             if ($product === $productId) {
  219.                 $isset = true;
  220.                 break;
  221.             }
  222.         }
  223.  
  224.         return $isset;
  225.     }
  226.  
  227.     /**
  228.      * @param int $customerId
  229.      * @return CompareCollection
  230.      */
  231.     public function getCompareOrderCollectionByCustomerId(int $customerId)
  232.     {
  233.         /** @var CompareCollection $collection */
  234.         $collection = $this->_itemCollectionFactory->create();
  235.  
  236.         $collection
  237.             ->addStoreFilter()
  238.             ->_addJoinToSelect()
  239.             ->addAttributeToFilter('customer_id',$customerId);
  240.  
  241.         $collection->load();
  242.  
  243.         return $collection;
  244.     }
  245. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top