![]() 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/Observer/ |
<?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\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\Serialize\Serializer\Json; use Magento\Framework\Message\ManagerInterface; use Magento\Checkout\Model\Cart; use Magento\Catalog\Model\ProductFactory; use Magento\Customer\Model\CustomerFactory; use Webkul\PrivateShop\Model\PrivateProductFactory; class VerfiyCart implements ObserverInterface { /** * @var ProductFactory */ protected $productFactory; /** * @var \Magento\Framework\Serialize\Serializer\Json */ protected $jsonSerializer; /** * @var ManagerInterface */ protected $messageManager; /** * @var Cart */ protected $cart; /** * @var CustomerFactory */ protected $customerFactory; /** * @var PrivateProductFactory */ protected $privateProductFactory; /** * * @param Json $jsonSerializer * @param ManagerInterface $messageManager * @param Cart $cart * @param ProductFactory $productFactory * @param CustomerFactory $customerFactory * @param PrivateProductFactory $privateProductFactory */ public function __construct( Json $jsonSerializer, ManagerInterface $messageManager, Cart $cart, ProductFactory $productFactory, CustomerFactory $customerFactory, PrivateProductFactory $privateProductFactory ) { $this->jsonSerializer = $jsonSerializer; $this->messageManager = $messageManager; $this->cart = $cart; $this->productFactory = $productFactory; $this->customerFactory = $customerFactory; $this->privateProductFactory = $privateProductFactory; } /** * @inheritDoc */ public function execute(\Magento\Framework\Event\Observer $observer) { $cart = $this->cart; $cartItems = $cart->getItems(); if ($cartItems && $cartItems->getSize()) { $message = false; foreach ($cartItems as $item) { $productId = $item->getProduct()->getId(); $flag = $this->isPrivateProduct($productId); if ($flag) { $cart->removeItem($item->getId()); $cart->getQuote()->setTotalsCollectedFlag(false); $cart->save(); $message = true; } } if ($message) { $this->messageManager->addNotice(__('Some unauthorized items removed from your cart.')); } } } /** * Is PrivateProduct * * @param [int] $productId * @return boolean */ public function isPrivateProduct($productId) { $product = $this->productFactory->create()->load($productId); $customerGroups = []; if ($product->getIsPrivateProduct()) { $customerSession = $this->cart->getCustomerSession(); $customerId = $customerSession->getId(); if ($customerSession->isLoggedIn() && $customerId) { $customer = $this->customerFactory->create()->load($customerId); $privateGroup = $customer->getData('customer_private_group'); if ($privateGroup) { $customerGroups = $this->jsonSerializer->unserialize( $privateGroup ); } } $collection = $this->privateProductFactory->create()->getCollection() ->addFieldToFilter('product_id', ['eq' => $productId]) ->addFieldToFilter('group_id', ['in' => $customerGroups]); if ($collection->getSize()) { return false; } return true; } return false; } }