1

Using magento 2.2, API. I created a web service for add multi-product in to cart, but not calculate subtotal. Help please!

CODE

/**
 *
 * {@inheritdoc}
 */
public function addMultiItemToCart($email, $storeId, $items)
{
    try {
        // Init store information
        $store = $this->storeFactory->create()->load($storeId);
        $websiteId = $this->_storeManager->getStore()->getWebsiteId();

        // init cutomer information
        $customer = $this->customerFactory->create();
        $customer->setWebsiteId($websiteId);
        $customer->loadByEmail($email); // load customet by email address

        // Create Quote
        $quote = $this->quote->create(); // Create object of quote
        $quote->setStore($store); // set store for which you create quote

        // if you have allready buyer id then you can load customer directly
        $customer = $this->customerRepository->getById($customer->getEntityId());
        $quote->setCurrency();
        $quote->setSubtotal(0);
        $quote->assignCustomer($customer); // Assign quote to customer

        // Add items in quote
        $validItem = array();
        foreach ($items as $item) {
            $product = $this->_productFactory->create()->load($item->getId());
            $quote->addProduct($product, intval($item['qty']));
        }

        $quote->save();
        $items = $quote->getAllItems();
        return $items;
    } catch (\Exception $e) {
        return [
            "MESSAGE" => "Add multi item to cart",
            "ERROR" => $e
        ];
    }
}
1
  • Help to correct my code. Commented Jul 10, 2018 at 3:49

2 Answers 2

1

You can calculate subtotal like below code:

// Add items in quote
$validItem = array();
foreach ($items as $item) {
    $product = $this->_productFactory->create()->load($item->getId());
    $quote->addProduct($product, intval($item['qty']));
    $quoteItemQty = $item['qty'];
    $quoteItemUnitPrice = $item['price'];
    $subTotal += ($quoteItemQty * $quoteItemUnitPrice);
}
$quote->setSubtotal($subTotal);
$quote->setBaseSubtotal($subTotal);
$quote->save();
6
  • When i use {{url}}/V1/carts/mine/items will return price always 0. Commented Jul 10, 2018 at 7:44
  • Need I to add any code? Commented Jul 10, 2018 at 7:45
  • can't you get product item price?
    – P_U
    Commented Jul 10, 2018 at 8:33
  • respond JSON from {{url}}/V1/carts/mine/items >[ { "item_id": 1869, "sku": "Simple Food", "qty": 2, "name": "Simple Food", "price": 0, "product_type": "simple", "quote_id": "1013" } ] Commented Jul 10, 2018 at 8:37
  • ` "price": 0` i got price alway 0. Commented Jul 10, 2018 at 8:38
2

All you have to do is, after you save your items to cart, you have calculate their subtotal, only if when the subtotal is not affected as per the quantity and number of items. To do this you have to use pricehelper(it is preferable to use pricehelper with your construct function and not with objectmanager), and after that save total for all the items added into the cart. Hope it might helps.

1.
    $priceHelper = $objectManager->create('Magento\Framework\Pricing\Helper\Data');

//this is how you can calculate subtotal for all the items in cart
$priceHelper->currency($item->getPrice()*$item->getQty(), true, false);

2.
//use this after you save your items
$quote->setTotalsCollectedFlag(false)->collectTotals()->save();

First try with 2 only, if not working then use 1.

2
  • How to set price for quote? Commented Jul 10, 2018 at 7:43
  • you don't have to set price, while adding product to cart, as this will be automatically as soon as you add a product. Also, it's not a good practice/error prone if you try to set price manually, if in case it doesn't change the price of cart, all you need to do is to $quote->setTotalsCollectedFlag(false)->collectTotals()->save(); use this, However if you still wants to set price you can use this $base_subtotal = $priceHelper->currency($item->getPrice()*$item->getQty(), true, false);
    – Akash
    Commented Jul 11, 2018 at 10:10

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.