1

when submit form Only first product price was display and other product price shows 0 sections.xml created

<?xml version="1.0"?>
     <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
          <action name="frontname/controller/action">
             <section name="cart"/>
          </action>
      </config>

controller file

 foreach ($pid as $value) {

            if (isset($value)) {
           $this->addCartProduct($value);
        }
         $this->_cart->save();

        }

public function addCartProduct($productID){
$product = $this->_productFactory->create()->load($productID);
$info = new \Magento\Framework\DataObject(
    [
        'form_key' => $this->formKey->getFormKey(),
        'product_id' => $productID,
        // 'price' => $product->setPrice($price),
         //'qty' => 1,
        // 'price' => 100,

    ]
);
return $this->_cart->addProduct($product, $info);
}

Note : I also tried to pass price and qty in function.

  • Can you please upload your controller code here ? – Rohan Hapani Sep 28 '18 at 6:58
1

Just replace this below code in your controller :

You should add foreach of product collection instead of product ids array.

protected $formKey;
protected $_productFactory;
protected $_cart;
protected $messageManager;
protected $collectionFactory;
public function __construct(
    Context $context,
    \Magento\Framework\Data\Form\FormKey $formKey,
    \Magento\Framework\Message\ManagerInterface $managerInterface,
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Catalog\Model\ResourceModel\Product\Collection $collectionFactory,
    \Magento\Checkout\Model\Cart $cart
) {
    parent::__construct($context);
    $this->formKey = $formKey;
    $this->_productFactory = $productFactory;
    $this->_cart = $cart;
    $this->messageManager = $managerInterface;
    $this->collectionFactory = $collectionFactory;
}

public function execute()
{
    $pid = "Your product ids array";
    $cart_product = $this->collectionFactory->getCollection()->addAttributeToFilter('entity_id',array('in' => $pid));
    if ($cart_product) {
        foreach ($cart_product as $key => $value) {
            $custom_optinons_value = '';
            if (isset($value['super_attribute']) || !empty($value['super_attribute'])) {
                $custom_optinons_value = $value['super_attribute'];
            }
            $this->addCartProduct($value['id'], $value['qty'], $custom_optinons_value);
        }
        $this->_cart->save();
    }
    $this->messageManager->addSuccess('Shopping cart updated succesfully.');
}

public function addCartProduct($productID, $productQty, $config_options)
{
    $product = $this->_productFactory->create()->load($productID);
    $info = new \Magento\Framework\DataObject(
        [
            'form_key' => $this->formKey->getFormKey(),
            'product_id' => $productID,
            'qty' => $productQty,
            'super_attribute' => $config_options,
        ]
    );
    return $this->_cart->addProduct($product, $info);
}

Remove var and generated folder.

  • it shows error undefined Call to undefined method Magento\Catalog\Model\ProductFactory::getCollection() – Tirth Patel Sep 28 '18 at 10:18
  • $this->_productFactory->create()->getCollection() write like this. – Rohan Hapani Sep 28 '18 at 12:59
  • i tried already this but getting wired error Warning: PDO::quote() expects parameter 1 to be string – Tirth Patel Sep 28 '18 at 13:24
  • See my updated answer. Remove var and generated – Rohan Hapani Sep 29 '18 at 4:14
  • same error :: Call to undefined method Magento\Catalog\Model\ResourceModel\Product\Collection\Interceptor::getCollection() – Tirth Patel Sep 29 '18 at 7:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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