Magento 2:カートでの複数の設定可能製品のバリエーションを追加する

magento.stackexchange https://magento.stackexchange.com/questions/110315

  •  29-09-2020
  •  | 
  •  

質問

カートに一度にカートに複数のバリエーションを追加しようとしていますが、コードをまとめていますが、現在それは製品の正しい数量を追加していますが、最初のバリエーションを使用しています。

言い換えれば、2つの緑のTシャツと4つの白いTシャツを追加しようとすると、それはカートに6つの緑のTシャツを追加しています。

これは私が追加コントローラの中にあるコードです:

public function execute()
    {
        $paramsData = $this->getRequest()->getParams();
        try {
            $msg = array();
            $errorMsg = array();
            foreach($paramsData['qty'] as $pId=>$param){ 

                if (isset($param)) {
                    $filter = new \Zend_Filter_LocalizedToNormalized(
                        ['locale' => $this->_objectManager->get('Magento\Framework\Locale\ResolverInterface')->getLocale()]
                    );
                    $params['qty'] = $filter->filter($param);
                }

                $params['product'] = $paramsData['product'][$pId];
                $product = $this->initProduct($params['product']);   
                $params['super_attribute'] = $paramsData['super'][$pId]; 
                /**
                 * Check product availability
                 */
                if (!$product) {
                    return $this->goBack();
                }            

                $this->cart->addProduct($product, $params);
                $msg[] = $product->getName(); 
            }             

            $this->cart->save();

        }         
        $resultRedirect->setPath('checkout/cart');
        return $resultRedirect;
    }
.

とそのprint_rから、オプションが正しいことを確認します。

 Array ( [super_attribute] => Array ( [90] => 5 ) [qty] => 2 ) 

 Array ( [super_attribute] => Array ( [90] => 7 ) [qty] => 4 ) 
.

カートでは、最初のSuper_attributeの6つの6つが見えています(2緑と4つの白いTシャツの代わりに6つの緑のTシャツ)。

各アイテムか何かを追加した後にカートを「リセット」するために必要なものはありますか?

ありがとう。

役に立ちましたか?

解決

私は答えた、 問題は次のループをオーバーライドする製品オブジェクトです。 コントローラでは、

新しい製品オブジェクトを作成するたびに作成し、その後の変更後に機能する必要があります。

$product = $this->initProduct($params['product']);weの代わりに行の下に設定されています、

$storeId = $this->_objectManager->get('Magento\Store\Model\StoreManagerInterface')->getStore()->getId();
$product = $this->_objectManager->create('Magento\Catalog\Model\Product')->setStoreId($storeId)->load($params['product']);
.

その作品

他のヒント

古い質問、同じように良い方法を報告するためだけに。 objectManager ...

を使用する代わりに...
$product = $this->_objectManager->create('Magento\Catalog\Model\Product')->setStoreId($storeId)->load($params['product']);
.

\ magento \ catalog \ model \ productfactory (あなたのクラスコンストラクタに新しい依存関係を追加する必要がある)は同じ結果を生み出すでしょう...

$product = $this->_productFactory->create()->setStoreId($storeId)->load($params['product']);
.

ライセンス: CC-BY-SA帰属
所属していません magento.stackexchange
scroll top