64 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
	
			
		
		
	
	
			64 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
	
| <?php
 | |
| 
 | |
| namespace IpSupply\CartToQuote\Controller\Quote;
 | |
| 
 | |
| use Magento\Framework\App\Action\Context;
 | |
| use Magento\Framework\View\Result\PageFactory;
 | |
| 
 | |
| class Clean extends \Magento\Framework\App\Action\Action {
 | |
| 
 | |
|     protected $pageFactory;
 | |
|     protected $cartItemFactory;
 | |
|     protected $_customerRepositoryInterface;
 | |
|     protected $request;
 | |
|     protected $customerSession;
 | |
|     protected $objectManager;
 | |
| 
 | |
|     public function __construct(
 | |
|         Context $context,
 | |
|         PageFactory $pageFactory,
 | |
|         \IpSupply\CartToQuote\Model\CartItemFactory $cartItemFactory,
 | |
|         \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
 | |
|         \Magento\Framework\Webapi\Rest\Request $request
 | |
|         )
 | |
|     {
 | |
|         $this->cartItemFactory = $cartItemFactory;
 | |
|         $this->_customerRepositoryInterface = $customerRepositoryInterface;
 | |
|         $this->request = $request;
 | |
|         $this->objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 | |
|         $this->customerSession = $this->objectManager->get(\Magento\Customer\Model\Session::class);
 | |
| 
 | |
|         parent::__construct($context);
 | |
|     }
 | |
| 
 | |
|     private function getCartItems($customerId) {
 | |
|         $collection = $this->cartItemFactory->create()->getCollection();
 | |
|         $data = $collection->addFieldToFilter('customer_id', ['eq' => $customerId])
 | |
|             ->setOrder('name','ASC')
 | |
|             ->getData();
 | |
|         return $data;
 | |
|     }
 | |
| 
 | |
|     private function clearCartItem($customerId) {
 | |
|         $collection = $this->cartItemFactory->create()->getCollection();
 | |
|         $count = $collection->addFieldToSelect('id')
 | |
|             ->addFieldToFilter('customer_id', ['eq' => $customerId])
 | |
|             ->walk('delete');
 | |
|     }
 | |
| 
 | |
| 
 | |
|     public function execute()
 | |
|     {
 | |
|         if($this->customerSession->isLoggedIn()) {
 | |
|             $customer = $this->customerSession->getCustomer();
 | |
|             $this->clearCartItem($customer->getId());
 | |
|             echo json_encode($this->getCartItems($customer->getId()));
 | |
|             exit;
 | |
|         } else {
 | |
|             echo json_encode(array());
 | |
|         }
 | |
| 
 | |
|     }
 | |
| 
 | |
| }
 |