Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Cart.php
Go to the documentation of this file.
1 <?php
7 
11 use Magento\Customer\Api\Data\AddressInterfaceFactory;
12 use Magento\Customer\Api\Data\CustomerInterfaceFactory;
14 use Magento\Framework\DataObjectFactory as ObjectFactory;
16 
22 {
26  private $quoteFactory;
27 
59  public function __construct(
60  \Magento\Backend\App\Action\Context $context,
61  \Magento\Framework\Registry $coreRegistry,
62  \Magento\Framework\App\Response\Http\FileFactory $fileFactory,
63  \Magento\Customer\Model\CustomerFactory $customerFactory,
64  \Magento\Customer\Model\AddressFactory $addressFactory,
65  \Magento\Customer\Model\Metadata\FormFactory $formFactory,
66  \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory,
67  \Magento\Customer\Helper\View $viewHelper,
68  \Magento\Framework\Math\Random $random,
70  \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
74  CustomerInterfaceFactory $customerDataFactory,
75  AddressInterfaceFactory $addressDataFactory,
76  \Magento\Customer\Model\Customer\Mapper $customerMapper,
77  \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
79  ObjectFactory $objectFactory,
80  \Magento\Framework\View\LayoutFactory $layoutFactory,
81  \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory,
82  \Magento\Framework\View\Result\PageFactory $resultPageFactory,
83  \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory,
84  \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory,
85  \Magento\Quote\Model\QuoteFactory $quoteFactory = null
86  ) {
87  parent::__construct(
88  $context,
89  $coreRegistry,
90  $fileFactory,
93  $formFactory,
94  $subscriberFactory,
95  $viewHelper,
96  $random,
98  $extensibleDataObjectConverter,
107  $objectFactory,
113  );
114  $this->quoteFactory = $quoteFactory ?: $this->_objectManager->get(\Magento\Quote\Model\QuoteFactory::class);
115  }
116 
122  public function execute()
123  {
124  $customerId = $this->initCurrentCustomer();
125  $websiteId = $this->getRequest()->getParam('website_id');
126 
127  // delete an item from cart
128  $deleteItemId = $this->getRequest()->getPost('delete');
129  if ($deleteItemId) {
131  $quoteRepository = $this->_objectManager->create(\Magento\Quote\Api\CartRepositoryInterface::class);
133  try {
134  $quote = $quoteRepository->getForCustomer($customerId);
135  } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
136  $quote = $this->quoteFactory->create();
137  }
138  $quote->setWebsite(
139  $this->_objectManager->get(\Magento\Store\Model\StoreManagerInterface::class)->getWebsite($websiteId)
140  );
141  $item = $quote->getItemById($deleteItemId);
142  if ($item && $item->getId()) {
143  $quote->removeItem($deleteItemId);
144  $quoteRepository->save($quote->collectTotals());
145  }
146  }
147 
148  $resultLayout = $this->resultLayoutFactory->create();
149  $resultLayout->getLayout()->getBlock('admin.customer.view.edit.cart')->setWebsiteId($websiteId);
150  return $resultLayout;
151  }
152 }
$quote
__construct(\Magento\Backend\App\Action\Context $context, \Magento\Framework\Registry $coreRegistry, \Magento\Framework\App\Response\Http\FileFactory $fileFactory, \Magento\Customer\Model\CustomerFactory $customerFactory, \Magento\Customer\Model\AddressFactory $addressFactory, \Magento\Customer\Model\Metadata\FormFactory $formFactory, \Magento\Newsletter\Model\SubscriberFactory $subscriberFactory, \Magento\Customer\Helper\View $viewHelper, \Magento\Framework\Math\Random $random, CustomerRepositoryInterface $customerRepository, \Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter, Mapper $addressMapper, AccountManagementInterface $customerAccountManagement, AddressRepositoryInterface $addressRepository, CustomerInterfaceFactory $customerDataFactory, AddressInterfaceFactory $addressDataFactory, \Magento\Customer\Model\Customer\Mapper $customerMapper, \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor, DataObjectHelper $dataObjectHelper, ObjectFactory $objectFactory, \Magento\Framework\View\LayoutFactory $layoutFactory, \Magento\Framework\View\Result\LayoutFactory $resultLayoutFactory, \Magento\Framework\View\Result\PageFactory $resultPageFactory, \Magento\Backend\Model\View\Result\ForwardFactory $resultForwardFactory, \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, \Magento\Quote\Model\QuoteFactory $quoteFactory=null)
Definition: Cart.php:59
$customerRepository
$addressFactory
Definition: quote.php:20
$quoteRepository