1

What is the Difference between

$this->quoteRepository->save($quote);

&

$quote->save();

In below code

public function aroundApply_shipping(
        \StripeIntegration\Payments\Api\Service $subject,
        callable $proceed,
        $address,
        $shipping_id = null
    ) {
        if (empty($this->checkoutSession) || empty($this->checkoutSession->getQuote()) || empty($this->checkoutSession->getQuote()->getId())) {
            $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/methodcallcheck.log');
            $logger = new \Zend\Log\Logger();
            $logger->addWriter($writer);
            $logger->info('Your text message');
            $data = $this->request->getBodyParams();
            $token = "";
            $response['error'] = true;
            $response['message'] = "";
            if (isset($data['cart']) && !empty($data['cart'])) {
                $token = $data['cart'];
                $cartId = $this->maskedQuoteIdToQuoteId->execute($token);
                $quote = $this->quoteRepository->get($cartId);
                $quote->setRemoteIp($this->_remoteIp->getRemoteAddress());
                $this->quoteRepository->save($quote);
                try {
                    if (count($address) === 0) {
                        $address = $this->expressHelper->getDefaultShippingAddress();
                    }
                    if (!$quote->isVirtual()) {
                        $shippingAddress = $this->helper->getShippingAddress($address);
                        $shipping = $quote->getShippingAddress()->addData($shippingAddress);
                        if ($shipping_id) {
                            $shipping->setShippingMethod($shipping_id)
                                ->setCollectShippingRates(true)
                                ->collectShippingRates();
                            $parts = explode('_', $shipping_id);
                            $carrierCode = array_shift($parts);
                            $methodCode = implode("_", $parts);
                            /** @var \Magento\Quote\Api\Data\AddressInterface $ba */
                            $shippingAddress = $this->inputProcessor->convertValue($shippingAddress, 'Magento\Quote\Api\Data\AddressInterface');
                            /** @var \Magento\Checkout\Api\Data\ShippingInformationInterface $shippingInformation */
                            $shippingInformation = $this->shippingInformationFactory->create();
                            $shippingInformation
                            //->setBillingAddress($shippingAddress)
                            ->setShippingAddress($shippingAddress)
                                ->setShippingCarrierCode($carrierCode)
                                ->setShippingMethodCode($methodCode);
                            $this->shippingInformationManagement->saveAddressInformation($quote->getId(), $shippingInformation);
                            $quote->setTotalsCollectedFlag(false);
                            $quote->collectTotals();
                            $this->quoteRepository->save($quote);
                        }
                    }
10
  • any reason why its like this ?
    – devhs
    Commented Jul 5, 2021 at 8:04
  • any thoughts on this ?
    – devhs
    Commented Jul 5, 2021 at 9:57
  • both should work. I checked as well and both line of code working, could you please add whole code ?
    – Pawan
    Commented Jul 5, 2021 at 9:58
  • Added code @Pawan
    – devhs
    Commented Jul 5, 2021 at 9:59
  • I doubt that "$this->quoteRepository->save($quote);" used twice can cause issue but its not.
    – devhs
    Commented Jul 5, 2021 at 10:00

1 Answer 1

0
  1. $this->quoteRepository->save($quote);

quoteRepository

The quote Repository class implements CartRepositoryInterface is used to follow Repository Pattern i.e. Edit/Update Or Delete existing entity. https://www.magentoextensions.org/documentation/class_magento_1_1_quote_1_1_model_1_1_quote_repository.html

  1. $quote

Quote Factory

This object is initialized from $this->quote->create();

It is following factory method which is directly manipulated with database entities and it's commonly used to create new records.

2
  • What is the basic difference between both methods ? I am just updating & saving quote, which should be ideally used ?
    – devhs
    Commented Jul 5, 2021 at 10:41
  • Ideally you can use the repository, Its recommended for updating the entity. Commented Jul 5, 2021 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.