magento 2.2.2您需要为您的项目选择选项

时间:2019-03-05 07:36:09

标签: php attributes cart magento2.2

我想通过ajax将可配置的产品添加到购物车,为此我尝试了以下代码,但始终会引发错误

  

“您需要选择项目的选项”。

代码:

public function execute() {
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $quote = $objectManager->get('\Magento\Checkout\Model\Cart');
        $productId = 20318;
        $product = $objectManager->create('\Magento\Catalog\Model\Product')->load($productId);

        $cart = $objectManager->create('Magento\Checkout\Model\Cart');
        $params = array();

        $options = array(
            "194" => "445",
            "202" => "447",
            "193" => "416",
            "201" => "439",
            "197" => "430",
            "196" => "427",
        );
        $params['qty'] = 1;
        $params['product'] = 20318;

        $params['super_attribute'] = $options;


        $cart->addProduct($product, $params);
        $cart->save();
    }

有人可以让我知道是什么问题吗?

我也尝试了下面的代码,但是它引发了相同的错误。 https://magento.stackexchange.com/a/198818/38685 https://magento.stackexchange.com/a/183836/38685

0 个答案:

没有答案