本文整理汇总了PHP中Magento\Quote\Api\CartRepositoryInterface::getForCustomer方法的典型用法代码示例。如果您正苦于以下问题:PHP CartRepositoryInterface::getForCustomer方法的具体用法?PHP CartRepositoryInterface::getForCustomer怎么用?PHP CartRepositoryInterface::getForCustomer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Magento\Quote\Api\CartRepositoryInterface
的用法示例。
在下文中一共展示了CartRepositoryInterface::getForCustomer方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: loadCustomerQuote
/**
* Load data for customer quote and merge with current quote
*
* @return $this
*/
public function loadCustomerQuote()
{
if (!$this->_customerSession->getCustomerId()) {
return $this;
}
$this->_eventManager->dispatch('load_customer_quote_before', ['checkout_session' => $this]);
try {
$customerQuote = $this->quoteRepository->getForCustomer($this->_customerSession->getCustomerId());
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$customerQuote = $this->quoteFactory->create();
}
$customerQuote->setStoreId($this->_storeManager->getStore()->getId());
if ($customerQuote->getId() && $this->getQuoteId() != $customerQuote->getId()) {
if ($this->getQuoteId()) {
$this->quoteRepository->save($customerQuote->merge($this->getQuote())->collectTotals());
}
$this->setQuoteId($customerQuote->getId());
if ($this->_quote) {
$this->quoteRepository->delete($this->_quote);
}
$this->_quote = $customerQuote;
} else {
$this->getQuote()->getBillingAddress();
$this->getQuote()->getShippingAddress();
$this->getQuote()->setCustomer($this->_customerSession->getCustomerDataObject())->setTotalsCollectedFlag(false)->collectTotals();
$this->quoteRepository->save($this->getQuote());
}
return $this;
}
示例2: assignCustomer
/**
* {@inheritdoc}
*/
public function assignCustomer($cartId, $customerId, $storeId)
{
$quote = $this->quoteRepository->getActive($cartId);
$customer = $this->customerRepository->getById($customerId);
$customerModel = $this->customerModelFactory->create();
if (!in_array($storeId, $customerModel->load($customerId)->getSharedStoreIds())) {
throw new StateException(__('Cannot assign customer to the given cart. The cart belongs to different store.'));
}
if ($quote->getCustomerId()) {
throw new StateException(__('Cannot assign customer to the given cart. The cart is not anonymous.'));
}
try {
$this->quoteRepository->getForCustomer($customerId);
throw new StateException(__('Cannot assign customer to the given cart. Customer already has active cart.'));
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
}
$quote->setCustomer($customer);
$quote->setCustomerIsGuest(0);
$quoteIdMaskFactory = $this->getQuoteIdMaskFactory();
/** @var \Magento\Quote\Model\QuoteIdMask $quoteIdMask */
$quoteIdMask = $quoteIdMaskFactory->create()->load($cartId, 'quote_id');
if ($quoteIdMask->getId()) {
$quoteIdMask->delete();
}
$this->quoteRepository->save($quote);
return true;
}
示例3: _initData
/**
* Loads customer, quote and quote item by request params
*
* @return $this
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _initData()
{
$this->_customerId = (int) $this->getRequest()->getParam('customer_id');
if (!$this->_customerId) {
throw new \Magento\Framework\Exception\LocalizedException(__('No customer ID defined.'));
}
$quoteItemId = (int) $this->getRequest()->getParam('id');
$websiteId = (int) $this->getRequest()->getParam('website_id');
try {
$this->_quote = $this->quoteRepository->getForCustomer($this->_customerId);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$this->_quote = $this->quoteFactory->create();
}
$this->_quote->setWebsite($this->_objectManager->get('Magento\\Store\\Model\\StoreManagerInterface')->getWebsite($websiteId));
$this->_quoteItem = $this->_quote->getItemById($quoteItemId);
if (!$this->_quoteItem) {
throw new LocalizedException(__('Please correct the quote items and try again.'));
}
return $this;
}
示例4: testGetForCustomer
public function testGetForCustomer()
{
$cartId = 17;
$customerId = 23;
$this->quoteFactoryMock->expects($this->once())->method('create')->willReturn($this->quoteMock);
$this->storeManagerMock->expects($this->once())->method('getStore')->willReturn($this->storeMock);
$this->storeMock->expects($this->once())->method('getId')->willReturn(1);
$this->quoteMock->expects($this->never())->method('setSharedStoreIds');
$this->quoteMock->expects($this->once())->method('loadByCustomer')->with($customerId)->willReturn($this->storeMock);
$this->quoteMock->expects($this->exactly(2))->method('getId')->willReturn($cartId);
$this->assertEquals($this->quoteMock, $this->model->getForCustomer($customerId));
$this->assertEquals($this->quoteMock, $this->model->getForCustomer($customerId));
}
示例5: getQuote
/**
* Get the quote of the cart
*
* @return \Magento\Quote\Model\Quote
*/
protected function getQuote()
{
if (null === $this->quote) {
$customerId = $this->getCustomerId();
$storeIds = $this->_storeManager->getWebsite($this->getWebsiteId())->getStoreIds();
try {
$this->quote = $this->quoteRepository->getForCustomer($customerId, $storeIds);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$this->quote = $this->quoteFactory->create()->setSharedStoreIds($storeIds);
}
}
return $this->quote;
}
示例6: execute
/**
* Set new customer group to all his quotes
*
* @param Observer $observer
* @return void
*/
public function execute(Observer $observer)
{
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
$customer = $observer->getEvent()->getCustomerDataObject();
try {
$quote = $this->quoteRepository->getForCustomer($customer->getId());
if ($customer->getGroupId() !== $quote->getCustomerGroupId()) {
/**
* It is needed to process customer's quotes for all websites
* if customer accounts are shared between all of them
*/
/** @var $websites \Magento\Store\Model\Website[] */
$websites = $this->config->isWebsiteScope() ? [$this->storeManager->getWebsite($customer->getWebsiteId())] : $this->storeManager->getWebsites();
foreach ($websites as $website) {
$quote->setWebsite($website);
$quote->setCustomerGroupId($customer->getGroupId());
$quote->collectTotals();
$this->quoteRepository->save($quote);
}
}
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
}
}
示例7: getQuote
/**
* Get quote
*
* @return \Magento\Quote\Model\Quote
*/
protected function getQuote()
{
if (null == $this->quote) {
$storeIds = $this->_storeManager->getWebsite($this->getWebsiteId())->getStoreIds();
$this->quote = $this->quoteFactory->create()->setSharedStoreIds($storeIds);
$currentCustomerId = $this->_coreRegistry->registry(RegistryConstants::CURRENT_CUSTOMER_ID);
if (!empty($currentCustomerId)) {
try {
$this->quote = $this->quoteRepository->getForCustomer($currentCustomerId, $storeIds);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
}
}
}
return $this->quote;
}
示例8: addCartInfo
/**
* Add cart info to collection
*
* @return $this
*/
public function addCartInfo()
{
foreach ($this->getItems() as $item) {
try {
$quote = $this->quoteRepository->getForCustomer($item->getId());
$totals = $quote->getTotals();
$item->setTotal($totals['subtotal']->getValue());
$quoteItems = $this->_quoteItemFactory->create()->setQuoteFilter($quote->getId());
$quoteItems->load();
$item->setItems($quoteItems->count());
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$item->remove();
}
}
return $this;
}
示例9: getCustomerCart
/**
* Retrieve customer cart quote object model
*
* @return \Magento\Quote\Model\Quote
*/
public function getCustomerCart()
{
if (!is_null($this->_cart)) {
return $this->_cart;
}
$this->_cart = $this->quoteFactory->create();
$customerId = (int) $this->getSession()->getCustomerId();
if ($customerId) {
try {
$this->_cart = $this->quoteRepository->getForCustomer($customerId);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
$this->_cart->setStore($this->getSession()->getStore());
$customerData = $this->customerRepository->getById($customerId);
$this->_cart->assignCustomer($customerData);
$this->quoteRepository->save($this->_cart);
}
}
return $this->_cart;
}