本文整理汇总了PHP中Magento\Quote\Model\QuoteRepository类的典型用法代码示例。如果您正苦于以下问题:PHP QuoteRepository类的具体用法?PHP QuoteRepository怎么用?PHP QuoteRepository使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了QuoteRepository类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: dispatch
/**
* Set new customer group to all his quotes
*
* @param Observer $observer
* @return void
*/
public function dispatch(Observer $observer)
{
/** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
$customer = $observer->getEvent()->getCustomerDataObject();
/** @var \Magento\Customer\Api\Data\CustomerInterface $origCustomer */
$origCustomer = $observer->getEvent()->getOrigCustomerDataObject();
if ($customer->getGroupId() !== $origCustomer->getGroupId()) {
/**
* 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) {
try {
$quote = $this->quoteRepository->getForCustomer($customer->getId());
$quote->setWebsite($website);
$quote->setCustomerGroupId($customer->getGroupId());
$quote->collectTotals();
$this->quoteRepository->save($quote);
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
}
}
}
}
示例2: get
/**
* {@inheritDoc}
*
* @param int $cartId The cart ID.
* @return Totals Quote totals data.
*/
public function get($cartId)
{
/**
* Quote.
*
* @var \Magento\Quote\Model\Quote $quote
*/
$quote = $this->quoteRepository->getActive($cartId);
$shippingAddress = $quote->getShippingAddress();
if ($quote->isVirtual()) {
$totalsData = array_merge($quote->getBillingAddress()->getData(), $quote->getData());
} else {
$totalsData = array_merge($shippingAddress->getData(), $quote->getData());
}
$totals = $this->totalsFactory->create();
$this->dataObjectHelper->populateWithArray($totals, $totalsData, '\\Magento\\Quote\\Api\\Data\\TotalsInterface');
$items = [];
$weeeTaxAppliedAmount = 0;
foreach ($quote->getAllVisibleItems() as $index => $item) {
$items[$index] = $this->itemConverter->modelToDataObject($item);
$weeeTaxAppliedAmount += $item->getWeeeTaxAppliedAmount();
}
$totals->setCouponCode($this->couponService->get($cartId));
$calculatedTotals = $this->totalsConverter->process($quote->getTotals());
$amount = $totals->getGrandTotal() - $totals->getTaxAmount();
$amount = $amount > 0 ? $amount : 0;
$totals->setGrandTotal($amount);
$totals->setTotalSegments($calculatedTotals);
$totals->setItems($items);
$totals->setWeeeTaxAppliedAmount($weeeTaxAppliedAmount);
return $totals;
}
示例3: aroundGet
/**
* @param CartTotalRepository $subject
* @param \Closure $proceed
* @param int $cartId
* @return \Magento\Quote\Model\Cart\Totals
* @throws \Magento\Framework\Exception\NoSuchEntityException
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function aroundGet(CartTotalRepository $subject, \Closure $proceed, $cartId)
{
$result = $proceed($cartId);
$quote = $this->quoteRepository->getActive($cartId);
$totals = $quote->getTotals();
if (!array_key_exists('tax', $totals)) {
return $result;
}
$taxes = $totals['tax']->getData();
if (!array_key_exists('full_info', $taxes)) {
return $result;
}
$detailsId = 1;
$finalData = [];
foreach ($taxes['full_info'] as $info) {
if (array_key_exists('hidden', $info) && $info['hidden'] || $info['amount'] == 0 && $this->taxConfig->displayCartZeroTax()) {
continue;
}
$taxDetails = $this->detailsFactory->create([]);
$taxDetails->setAmount($info['amount']);
$taxRates = $this->getRatesData($info['rates']);
$taxDetails->setRates($taxRates);
$taxDetails->setGroupId($detailsId);
$finalData[] = $taxDetails;
$detailsId++;
}
$attributes = $result->getExtensionAttributes();
if ($attributes === null) {
$attributes = $this->extensionFactory->create();
}
$attributes->setTaxGrandtotalDetails($finalData);
/** @var $result \Magento\Quote\Model\Cart\Totals */
$result->setExtensionAttributes($attributes);
return $result;
}
示例4: execute
/**
* Initialize coupon
*
* @return \Magento\Framework\Controller\Result\Redirect
* @throws \Magento\Framework\Exception\LocalizedException|\Exception
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
/**
* No reason continue with empty shopping cart
*/
if (!$this->cart->getQuote()->getItemsCount()) {
return $this->_goBack();
}
$couponCode = $this->getRequest()->getParam('remove') == 1 ? '' : trim($this->getRequest()->getParam('coupon_code'));
$oldCouponCode = $this->cart->getQuote()->getCouponCode();
if (!strlen($couponCode) && !strlen($oldCouponCode)) {
return $this->_goBack();
}
$codeLength = strlen($couponCode);
$isCodeLengthValid = $codeLength && $codeLength <= \Magento\Checkout\Helper\Cart::COUPON_CODE_MAX_LENGTH;
$this->cart->getQuote()->getShippingAddress()->setCollectShippingRates(true);
$this->cart->getQuote()->setCouponCode($isCodeLengthValid ? $couponCode : '')->collectTotals();
$this->quoteRepository->save($this->cart->getQuote());
if ($codeLength) {
if ($isCodeLengthValid && $couponCode == $this->cart->getQuote()->getCouponCode()) {
$this->messageManager->addSuccess(__('The coupon code "%1" was applied.', $this->_objectManager->get('Magento\\Framework\\Escaper')->escapeHtml($couponCode)));
} else {
$this->messageManager->addError(__('The coupon code "%1" is not valid.', $this->_objectManager->get('Magento\\Framework\\Escaper')->escapeHtml($couponCode)));
$this->cart->save();
}
} else {
$this->messageManager->addSuccess(__('The coupon code was canceled.'));
}
return $this->_goBack();
}
示例5: testSaveAddresses
public function testSaveAddresses()
{
$cartId = 100;
$additionalData = $this->getMock('\\Magento\\Quote\\Api\\Data\\AddressAdditionalDataInterface');
$billingAddressMock = $this->getMock('\\Magento\\Quote\\Model\\Quote\\Address', [], [], '', false);
$shippingAddressMock = $this->getMock('\\Magento\\Quote\\Model\\Quote\\Address', [], [], '', false);
$this->billingAddressManagement->expects($this->once())->method('assign')->with($cartId, $billingAddressMock)->willReturn(1);
$billingAddressMock->expects($this->once())->method('format')->with('html');
$this->billingAddressManagement->expects($this->once())->method('get')->with($cartId)->willReturn($billingAddressMock);
$this->shippingAddressManagement->expects($this->once())->method('assign')->with($cartId, $shippingAddressMock)->willReturn(1);
$shippingAddressMock->expects($this->once())->method('format')->with('html');
$this->shippingAddressManagement->expects($this->once())->method('get')->with($cartId)->willReturn($shippingAddressMock);
$shippingMethodMock = $this->getMock('\\Magento\\Quote\\Api\\Data\\ShippingMethodInterface');
$this->shippingMethodManagement->expects($this->once())->method('getList')->with($cartId)->willReturn([$shippingMethodMock]);
$paymentMethodMock = $this->getMock('\\Magento\\Quote\\Api\\Data\\PaymentMethodInterface');
$this->paymentMethodManagement->expects($this->once())->method('getList')->with($cartId)->willReturn([$paymentMethodMock]);
$addressDetailsMock = $this->getMock('\\Magento\\Quote\\Model\\AddressDetails', [], [], '', false);
$this->addressDetailsFactory->expects($this->once())->method('create')->willReturn($addressDetailsMock);
$addressDetailsMock->expects($this->once())->method('setShippingMethods')->with([$shippingMethodMock])->willReturnSelf();
$addressDetailsMock->expects($this->once())->method('setPaymentMethods')->with([$paymentMethodMock])->willReturnSelf();
$this->dataProcessor->expects($this->once())->method('process')->with($additionalData);
$quote = $this->getMock('Magento\\Quote\\Model\\Quote', [], [], '', false);
$quote->expects($this->once())->method('setCheckoutMethod')->willReturnSelf();
$this->quoteRepository->expects($this->once())->method('getActive')->willReturn($quote);
$this->model->saveAddresses($cartId, $billingAddressMock, $shippingAddressMock, $additionalData, 'register');
}
示例6: savePaymentInQuote
/**
* Saves payment information in quote
*
* @param Object $response
* @return void
*/
public function savePaymentInQuote($response)
{
$quote = $this->quoteRepository->get($this->sessionTransparent->getQuoteId());
/** @var InfoInterface $payment */
$payment = $this->paymentManagement->get($quote->getId());
$payment->setAdditionalInformation('pnref', $response->getPnref());
$this->errorHandler->handle($payment, $response);
$this->paymentManagement->set($quote->getId(), $payment);
}
示例7: beforeSaveAddressInformation
/**
* @param \Magento\Checkout\Model\ShippingInformationManagement $subject
* @param $cartId
* @param \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation
*/
public function beforeSaveAddressInformation(\Magento\Checkout\Model\ShippingInformationManagement $subject, $cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation)
{
$customFee = $addressInformation->getExtensionAttributes()->getFee();
$quote = $this->quoteRepository->getActive($cartId);
if ($customFee) {
$fee = $this->dataHelper->getCustomFee();
$quote->setFee($fee);
} else {
$quote->setFee(NULL);
}
}
示例8: execute
/**
* Return shipping options items for shipping address from request
*
* @return void
*/
public function execute()
{
try {
$quoteId = $this->getRequest()->getParam('quote_id');
$this->_quote = $this->quoteRepository->get($quoteId);
$this->_initCheckout();
$response = $this->_checkout->getShippingOptionsCallbackResponse($this->getRequest()->getParams());
$this->getResponse()->setBody($response);
} catch (\Exception $e) {
$this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
}
}
示例9: execute
/**
* Initialize shipping information
*
* @return \Magento\Framework\Controller\Result\Redirect
*/
public function execute()
{
$country = (string) $this->getRequest()->getParam('country_id');
$postcode = (string) $this->getRequest()->getParam('estimate_postcode');
$city = (string) $this->getRequest()->getParam('estimate_city');
$regionId = (string) $this->getRequest()->getParam('region_id');
$region = (string) $this->getRequest()->getParam('region');
$this->cart->getQuote()->getShippingAddress()->setCountryId($country)->setCity($city)->setPostcode($postcode)->setRegionId($regionId)->setRegion($region)->setCollectShippingRates(true);
$this->quoteRepository->save($this->cart->getQuote());
$this->cart->save();
return $this->_goBack();
}
示例10: execute
/**
* Initialize coupon
*
* @return \Magento\Framework\Controller\Result\Redirect
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function execute()
{
$couponCode = $this->getRequest()->getParam('remove') == 1 ? '' : trim($this->getRequest()->getParam('coupon_code'));
$cartQuote = $this->cart->getQuote();
$oldCouponCode = $cartQuote->getCouponCode();
$codeLength = strlen($couponCode);
if (!$codeLength && !strlen($oldCouponCode)) {
return $this->_goBack();
}
try {
$isCodeLengthValid = $codeLength && $codeLength <= \Magento\Checkout\Helper\Cart::COUPON_CODE_MAX_LENGTH;
$itemsCount = $cartQuote->getItemsCount();
if ($itemsCount) {
$cartQuote->getShippingAddress()->setCollectShippingRates(true);
$cartQuote->setCouponCode($isCodeLengthValid ? $couponCode : '')->collectTotals();
$this->quoteRepository->save($cartQuote);
}
if ($codeLength) {
$escaper = $this->_objectManager->get('Magento\\Framework\\Escaper');
if (!$itemsCount) {
if ($isCodeLengthValid) {
$coupon = $this->couponFactory->create();
$coupon->load($couponCode, 'code');
if ($coupon->getId()) {
$this->_checkoutSession->getQuote()->setCouponCode($couponCode)->save();
$this->messageManager->addSuccess(__('You used coupon code "%1".', $escaper->escapeHtml($couponCode)));
} else {
$this->messageManager->addError(__('The coupon code "%1" is not valid.', $escaper->escapeHtml($couponCode)));
}
} else {
$this->messageManager->addError(__('The coupon code "%1" is not valid.', $escaper->escapeHtml($couponCode)));
}
} else {
if ($isCodeLengthValid && $couponCode == $cartQuote->getCouponCode()) {
$this->messageManager->addSuccess(__('You used coupon code "%1".', $escaper->escapeHtml($couponCode)));
} else {
$this->messageManager->addError(__('The coupon code "%1" is not valid.', $escaper->escapeHtml($couponCode)));
$this->cart->save();
}
}
} else {
$this->messageManager->addSuccess(__('You canceled the coupon code.'));
}
} catch (\Magento\Framework\Exception\LocalizedException $e) {
$this->messageManager->addError($e->getMessage());
} catch (\Exception $e) {
$this->messageManager->addError(__('We cannot apply the coupon code.'));
$this->_objectManager->get('Psr\\Log\\LoggerInterface')->critical($e);
}
return $this->_goBack();
}
示例11: get
/**
* {@inheritDoc}
*
* @param int $cartId The cart ID.
* @return Totals Quote totals data.
*/
public function get($cartId)
{
/**
* Quote.
*
* @var \Magento\Quote\Model\Quote $quote
*/
$quote = $this->quoteRepository->getActive($cartId);
$shippingAddress = $quote->getShippingAddress();
$totalsData = array_merge($shippingAddress->getData(), $quote->getData());
$totals = $this->totalsFactory->create();
$this->dataObjectHelper->populateWithArray($totals, $totalsData, '\\Magento\\Quote\\Api\\Data\\TotalsInterface');
$totals->setItems($quote->getAllItems());
return $totals;
}
示例12: testSavePaymentInQuote
public function testSavePaymentInQuote()
{
$quoteId = 1;
$response = new DataObject();
$payment = $this->getMockBuilder('Magento\\Quote\\Model\\Quote\\Payment')->disableOriginalConstructor()->getMock();
$payment->expects($this->once())->method('setAdditionalInformation')->with('pnref');
$this->errorHandlerMock->expects($this->once())->method('handle')->with($payment, $response);
$quote = $this->getMock('Magento\\Quote\\Api\\Data\\CartInterface', [], [], '', false);
$quote->expects($this->exactly(2))->method('getId')->willReturn($quoteId);
$this->sessionTransparent->expects($this->once())->method('getQuoteId')->willReturn($quoteId);
$this->quoteRepository->expects($this->once())->method('get')->willReturn($quote);
$this->paymentMethodManagementInterface->expects($this->once())->method('get')->willReturn($payment);
$this->paymentMethodManagementInterface->expects($this->once())->method('set');
$this->model->savePaymentInQuote($response);
}
示例13: set
/**
* {@inheritDoc}
*
* @param int $cartId The shopping cart ID.
* @param string $carrierCode The carrier code.
* @param string $methodCode The shipping method code.
* @return bool
* @throws \Magento\Framework\Exception\InputException The shipping method is not valid for an empty cart.
* @throws \Magento\Framework\Exception\CouldNotSaveException The shipping method could not be saved.
* @throws \Magento\Framework\Exception\NoSuchEntityException The specified cart contains only virtual products and the shipping method is not applicable.
* @throws \Magento\Framework\Exception\StateException The billing or shipping address is not set.
*/
public function set($cartId, $carrierCode, $methodCode)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if (0 == $quote->getItemsCount()) {
throw new InputException(__('Shipping method is not applicable for empty cart'));
}
if ($quote->isVirtual()) {
throw new NoSuchEntityException(__('Cart contains virtual product(s) only. Shipping method is not applicable.'));
}
$shippingAddress = $quote->getShippingAddress();
if (!$shippingAddress->getCountryId()) {
throw new StateException(__('Shipping address is not set'));
}
$billingAddress = $quote->getBillingAddress();
if (!$billingAddress->getCountryId()) {
throw new StateException(__('Billing address is not set'));
}
$shippingAddress->setShippingMethod($carrierCode . '_' . $methodCode);
if (!$shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod())) {
throw new NoSuchEntityException(__('Carrier with such method not found: %1, %2', $carrierCode, $methodCode));
}
try {
$this->quoteRepository->save($quote->collectTotals());
} catch (\Exception $e) {
throw new CouldNotSaveException(__('Cannot set shipping method. %1', $e->getMessage()));
}
return true;
}
示例14: testGetCartForCustomer
public function testGetCartForCustomer()
{
$customerId = 100;
$cartMock = $this->getMock('\\Magento\\Quote\\Model\\Quote', [], [], '', false);
$this->quoteRepositoryMock->expects($this->once())->method('getActiveForCustomer')->with($customerId)->willReturn($cartMock);
$this->assertEquals($cartMock, $this->model->getCartForCustomer($customerId));
}
示例15: testGetListSuccess
/**
* @param int $direction
* @param string $expectedDirection
* @dataProvider getListSuccessDataProvider
*/
public function testGetListSuccess($direction, $expectedDirection)
{
$searchResult = $this->getMock('\\Magento\\Quote\\Api\\Data\\CartSearchResultsInterface', [], [], '', false);
$searchCriteriaMock = $this->getMock('\\Magento\\Framework\\Api\\SearchCriteria', [], [], '', false);
$cartMock = $this->getMock('Magento\\Payment\\Model\\Cart', [], [], '', false);
$filterMock = $this->getMock('\\Magento\\Framework\\Api\\Filter', [], [], '', false);
$pageSize = 10;
$this->searchResultsBuilderMock->expects($this->once())->method('setSearchCriteria');
$filterGroupMock = $this->getMock('\\Magento\\Framework\\Api\\Search\\FilterGroup', [], [], '', false);
$searchCriteriaMock->expects($this->any())->method('getFilterGroups')->will($this->returnValue([$filterGroupMock]));
//addFilterGroupToCollection() checks
$filterGroupMock->expects($this->any())->method('getFilters')->will($this->returnValue([$filterMock]));
$filterMock->expects($this->once())->method('getField')->will($this->returnValue('store_id'));
$filterMock->expects($this->any())->method('getConditionType')->will($this->returnValue('eq'));
$filterMock->expects($this->once())->method('getValue')->will($this->returnValue('filter_value'));
//back in getList()
$this->quoteCollectionMock->expects($this->once())->method('getSize')->willReturn($pageSize);
$this->searchResultsBuilderMock->expects($this->once())->method('setTotalCount')->with($pageSize);
$sortOrderMock = $this->getMockBuilder('Magento\\Framework\\Api\\SortOrder')->setMethods(['getField', 'getDirection'])->disableOriginalConstructor()->getMock();
//foreach cycle
$searchCriteriaMock->expects($this->once())->method('getSortOrders')->will($this->returnValue([$sortOrderMock]));
$sortOrderMock->expects($this->once())->method('getField')->will($this->returnValue('id'));
$sortOrderMock->expects($this->once())->method('getDirection')->will($this->returnValue($direction));
$this->quoteCollectionMock->expects($this->once())->method('addOrder')->with('id', $expectedDirection);
$searchCriteriaMock->expects($this->once())->method('getCurrentPage')->will($this->returnValue(1));
$searchCriteriaMock->expects($this->once())->method('getPageSize')->will($this->returnValue(10));
$this->quoteCollectionMock->expects($this->once())->method('setCurPage')->with(1);
$this->quoteCollectionMock->expects($this->once())->method('setPageSize')->with(10);
$this->quoteCollectionMock->expects($this->once())->method('getItems')->willReturn([$cartMock]);
$this->searchResultsBuilderMock->expects($this->once())->method('setItems')->with([$cartMock]);
$this->searchResultsBuilderMock->expects($this->once())->method('create')->will($this->returnValue($searchResult));
$this->assertEquals($searchResult, $this->model->getList($searchCriteriaMock));
}