magento2-docker/app/code/IpSupply/CartToQuote/Controller/Quote/Data.php

55 lines
1.7 KiB
PHP
Executable File

<?php
namespace IpSupply\CartToQuote\Controller\Quote;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class Data 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;
}
public function execute()
{
if($this->customerSession->isLoggedIn()) {
$customer = $this->customerSession->getCustomer();
echo json_encode($this->getCartItems($customer->getId()));
exit;
} else {
echo json_encode(array());
}
}
}