本文整理匯總了PHP中Magento\Quote\Api\CartRepositoryInterface::save方法的典型用法代碼示例。如果您正苦於以下問題:PHP CartRepositoryInterface::save方法的具體用法?PHP CartRepositoryInterface::save怎麽用?PHP CartRepositoryInterface::save使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Magento\Quote\Api\CartRepositoryInterface
的用法示例。
在下文中一共展示了CartRepositoryInterface::save方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: 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();
}
示例2: execute
/**
* Fetch coupon info
*
* Controller Action
*/
public function execute()
{
$total = $this->getRequest()->getParam('cost');
$quote = $this->_checkoutSession->getQuote();
//save value to DiscountCoupon collect
$this->_registry->register('mercadopago_total_amount', $total);
$this->quoteRepository->save($quote->collectTotals());
return;
}
示例3: updateQuote
/**
* Update quote data
*
* @param Quote $quote
* @param array $details
* @return void
*/
private function updateQuote(Quote $quote, array $details)
{
$quote->setMayEditShippingAddress(false);
$quote->setMayEditShippingMethod(true);
$this->updateQuoteAddress($quote, $details);
$this->disabledQuoteAddressValidation($quote);
$quote->collectTotals();
$this->quoteRepository->save($quote);
}
示例4: 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();
}
示例5: assign
/**
* {@inheritDoc}
* @SuppressWarnings(PHPMD.NPathComplexity)
*/
public function assign($cartId, \Magento\Quote\Api\Data\AddressInterface $address, $useForShipping = false)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
$quote->removeAddress($quote->getBillingAddress()->getId());
$quote->setBillingAddress($address);
try {
$quote->setDataChanges(true);
$this->quoteRepository->save($quote);
} catch (\Exception $e) {
$this->logger->critical($e);
throw new InputException(__('Unable to save address. Please check input data.'));
}
return $quote->getBillingAddress()->getId();
}
示例6: beforeDispatch
/**
* @param \Magento\Checkout\Controller\Cart $subject
* @param \Magento\Framework\App\RequestInterface $request
* @return void
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
*/
public function beforeDispatch(\Magento\Checkout\Controller\Cart $subject, \Magento\Framework\App\RequestInterface $request)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->checkoutSession->getQuote();
// Clear shipping addresses and item assignments after Multishipping flow
if ($quote->isMultipleShippingAddresses()) {
foreach ($quote->getAllShippingAddresses() as $address) {
$quote->removeAddress($address->getId());
}
$quote->getShippingAddress();
$quote->setIsMultiShipping(false);
$quote->collectTotals();
$this->cartRepository->save($quote);
}
}
示例7: saveAddressInformation
/**
* {@inheritDoc}
*/
public function saveAddressInformation($cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation)
{
$address = $addressInformation->getShippingAddress();
$billingAddress = $addressInformation->getBillingAddress();
$carrierCode = $addressInformation->getShippingCarrierCode();
$methodCode = $addressInformation->getShippingMethodCode();
if (!$address->getCountryId()) {
throw new StateException(__('Shipping address is not set'));
}
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
$quote = $this->prepareShippingAssignment($quote, $address, $carrierCode . '_' . $methodCode);
$this->validateQuote($quote);
$quote->setIsMultiShipping(false);
if ($billingAddress) {
$quote->setBillingAddress($billingAddress);
}
try {
$this->quoteRepository->save($quote);
} catch (\Exception $e) {
$this->logger->critical($e);
throw new InputException(__('Unable to save shipping information. Please check input data.'));
}
$shippingAddress = $quote->getShippingAddress();
if (!$shippingAddress->getShippingRateByCode($shippingAddress->getShippingMethod())) {
throw new NoSuchEntityException(__('Carrier with such method not found: %1, %2', $carrierCode, $methodCode));
}
/** @var \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails */
$paymentDetails = $this->paymentDetailsFactory->create();
$paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId));
$paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
return $paymentDetails;
}
示例8: testSave
public function testSave()
{
$this->quoteMock->expects($this->once())->method('save');
$this->quoteMock->expects($this->exactly(1))->method('getId')->willReturn(1);
$this->quoteMock->expects($this->exactly(1))->method('getCustomerId')->willReturn(2);
$this->model->save($this->quoteMock);
}
示例9: 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 InputException The shipping method is not valid for an empty cart.
* @throws CouldNotSaveException The shipping method could not be saved.
* @throws NoSuchEntityException Cart contains only virtual products. Shipping method is not applicable.
* @throws 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'));
}
$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;
}
示例10: getQuote
/**
* Retrieve quote model object
*
* @return \Magento\Quote\Model\Quote
*/
public function getQuote()
{
if ($this->_quote === null) {
$this->_quote = $this->quoteFactory->create();
if ($this->getStoreId()) {
if (!$this->getQuoteId()) {
$this->_quote->setCustomerGroupId($this->groupManagement->getDefaultGroup()->getId());
$this->_quote->setIsActive(false);
$this->_quote->setStoreId($this->getStoreId());
$this->quoteRepository->save($this->_quote);
$this->setQuoteId($this->_quote->getId());
$this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]);
} else {
$this->_quote = $this->quoteRepository->get($this->getQuoteId(), [$this->getStoreId()]);
$this->_quote->setStoreId($this->getStoreId());
}
if ($this->getCustomerId() && $this->getCustomerId() != $this->_quote->getCustomerId()) {
$customer = $this->customerRepository->getById($this->getCustomerId());
$this->_quote->assignCustomer($customer);
$this->quoteRepository->save($this->_quote);
}
}
$this->_quote->setIgnoreOldQty(true);
$this->_quote->setIsSuperMode(true);
}
return $this->_quote;
}
示例11: execute
/**
* Execute operation
*
* @param string $shippingMethod
* @param Quote $quote
* @return void
* @throws \InvalidArgumentException
*/
public function execute($shippingMethod, Quote $quote)
{
if (empty($shippingMethod)) {
throw new \InvalidArgumentException('The "shippingMethod" field does not exists.');
}
if (!$quote->getIsVirtual()) {
$shippingAddress = $quote->getShippingAddress();
if ($shippingMethod !== $shippingAddress->getShippingMethod()) {
$this->disabledQuoteAddressValidation($quote);
$shippingAddress->setShippingMethod($shippingMethod);
$shippingAddress->setCollectShippingRates(true);
$quote->collectTotals();
$this->quoteRepository->save($quote);
}
}
}
示例12: reOrder
/**
* @param $quoteId
*
* @return Quote
*/
public function reOrder($quoteId)
{
$quote = $this->_quoteRepository->get($quoteId);
$quote->setIsActive(1)->setReservedOrderId(null);
$this->_quoteRepository->save($quote);
return $quote;
}
示例13: saveAddressInformation
/**
* {@inheritDoc}
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
public function saveAddressInformation($cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation)
{
$address = $addressInformation->getShippingAddress();
$carrierCode = $addressInformation->getShippingCarrierCode();
$methodCode = $addressInformation->getShippingMethodCode();
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
$this->validateQuote($quote);
$quote->setIsMultiShipping(false);
$saveInAddressBook = $address->getSaveInAddressBook() ? 1 : 0;
$sameAsBilling = $address->getSameAsBilling() ? 1 : 0;
$customerAddressId = $address->getCustomerAddressId();
$this->addressValidator->validate($address);
$quote->setShippingAddress($address);
$address = $quote->getShippingAddress();
if ($customerAddressId) {
$addressData = $this->addressRepository->getById($customerAddressId);
$address = $quote->getShippingAddress()->importCustomerAddressData($addressData);
}
$billingAddress = $addressInformation->getBillingAddress();
if ($billingAddress) {
$quote->setBillingAddress($billingAddress);
}
$address->setSaveInAddressBook($saveInAddressBook);
$address->setSameAsBilling($sameAsBilling);
$address->setCollectShippingRates(true);
if (!$address->getCountryId()) {
throw new StateException(__('Shipping address is not set'));
}
$address->setShippingMethod($carrierCode . '_' . $methodCode);
try {
$this->totalsCollector->collectAddressTotals($quote, $address);
} catch (\Exception $e) {
$this->logger->critical($e);
throw new InputException(__('Unable to save address. Please check input data.'));
}
if (!$address->getShippingRateByCode($address->getShippingMethod())) {
throw new NoSuchEntityException(__('Carrier with such method not found: %1, %2', $carrierCode, $methodCode));
}
if (!$quote->validateMinimumAmount($quote->getIsMultiShipping())) {
throw new InputException($this->scopeConfig->getValue('sales/minimum_order/error_message', \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $quote->getStoreId()));
}
try {
$address->save();
$quote->collectTotals();
$this->quoteRepository->save($quote);
} catch (\Exception $e) {
$this->logger->critical($e);
throw new InputException(__('Unable to save shipping information. Please check input data.'));
}
/** @var \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails */
$paymentDetails = $this->paymentDetailsFactory->create();
$paymentDetails->setPaymentMethods($this->paymentMethodManagement->getList($cartId));
$paymentDetails->setTotals($this->cartTotalsRepository->get($cartId));
return $paymentDetails;
}
示例14: remove
/**
* {@inheritdoc}
*/
public function remove($cartId)
{
/** @var \Magento\Quote\Model\Quote $quote */
$quote = $this->quoteRepository->getActive($cartId);
if (!$quote->getItemsCount()) {
throw new NoSuchEntityException(__('Cart %1 doesn\'t contain products', $cartId));
}
$quote->getShippingAddress()->setCollectShippingRates(true);
try {
$quote->setCouponCode('');
$this->quoteRepository->save($quote->collectTotals());
} catch (\Exception $e) {
throw new CouldNotDeleteException(__('Could not delete coupon code'));
}
if ($quote->getCouponCode() != '') {
throw new CouldNotDeleteException(__('Could not delete coupon code'));
}
return true;
}
示例15: collect
/**
* @param \Magento\Quote\Model\Quote $quote
* @return void
*/
public function collect(\Magento\Quote\Model\Quote $quote)
{
if ($this->customerSession->isLoggedIn()) {
$customer = $this->customerRepository->getById($this->customerSession->getCustomerId());
if ($defaultShipping = $customer->getDefaultShipping()) {
$address = $this->addressRepository->getById($defaultShipping);
if ($address) {
/** @var \Magento\Quote\Api\Data\EstimateAddressInterface $estimatedAddress */
$estimatedAddress = $this->estimatedAddressFactory->create();
$estimatedAddress->setCountryId($address->getCountryId());
$estimatedAddress->setPostcode($address->getPostcode());
$estimatedAddress->setRegion((string) $address->getRegion()->getRegion());
$estimatedAddress->setRegionId($address->getRegionId());
$this->shippingMethodManager->estimateByAddress($quote->getId(), $estimatedAddress);
$this->quoteRepository->save($quote);
}
}
}
}