【问题标题】:Magento2 : How to add multiple product in cart using single form submission?Magento2:如何使用单一表单提交在购物车中添加多个产品?
【发布时间】:2016-09-14 11:14:36
【问题描述】:

我是 magento2 的新手,使用的是 2.1 版。 我必须在购物车中添加多个产品,产品可以是任何类型,并且我想添加 ajax 验证以及此功能。

谁能想到实现这一目标?

谢谢, 钱丹

【问题讨论】:

  • 最后我得到了在 productRepository 函数中通过参数使用下面的代码行在购物车中添加多个产品的解决方案
  • 易于使用此扩展程序Magento 2 Add Multiple Products to Cart,这将节省您的时间和金钱

标签: magento2


【解决方案1】:

您好,我得到了在 for 循环中在购物车中添加多个产品的解决方案: 请参考以下代码在 MAGENTO 2 中实现此功能

   if ($product_id) {
                    $storeId = $objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId();
                    try {


                        if(isset($params['super_attribute'][$product_id])){
                            $params_post = array();
                            $params_post['form_key'] = $this->formKey->getFormKey();
                            $params_post['qty'] = 1;
                            $params_post['super_attribute'] =  $params['super_attribute'][$product_id];
                            $finalproduct = $this->productRepository->getById($product_id, false, $storeId);
                            $this->cart->addProduct($finalproduct, $params_post);
                        } else {
                            $params_post = array();
                            $params_post['form_key'] = $this->formKey->getFormKey();
                            $params_post['qty'] = 1;

                            $finalproduct = $this->productRepository->getById($product_id, false, $storeId);
                            $this->cart->addProduct($finalproduct, $params_post);
                        }

                    } catch (NoSuchEntityException $e) {

                    }
                }

【讨论】:

  • 这将添加可配置、简单和选项产品。您可以根据需要进行修改。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多