الماجنتو 2 :إضافة اختلافات متعددة في المنتجات القابلة للتكوين في عربة التسوق

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

  •  29-09-2020
  •  | 
  •  

سؤال

أحاول إضافة أشكال متعددة لمنتج قابل للتكوين إلى عربة التسوق في وقت واحد ، وقد قمت بتجميع الكود معا ، لكنه حاليا يضيف الكمية الصحيحة من المنتجات ، ولكن فقط باستخدام الشكل الأول.

وبعبارة أخرى ، إذا حاولت إضافة 2 القمصان الخضراء و 4 القمصان البيضاء ، فإنه يضيف 6 القمصان الخضراء في العربة.

هذا هو رمز لقد حصلت داخل إضافة وحدة تحكم:

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;
    }

ومن تلك المطبوعة ، فإنه يؤكد أن الخيارات صحيحة:

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

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

لكن في العربة ، أرى 6 من أول سمة فائقة (6 تي شيرت أخضر بدلا من 2 تي شيرت أخضر و 4 تي شيرت أبيض).

هل هناك شيء أحتاج إلى القيام به ل 'إعادة' العربة بعد إضافة كل عنصر أو شيء?

شكرا.

هل كانت مفيدة؟

المحلول

لقد حصلت على إجابة, المشكلة هي تجاوز كائن المنتج الحلقة التالية, في المراقب المالي,

لدينا لخلق كل مرة كائن منتج جديد والآن عملها بعد القيام أدناه التغييرات.

بدلا من $product = $this->initProduct($params['product']);لقد وضعنا أدناه خط,

$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']);

باستخدام \الماجنتو\كتالوج\نموذج\ProductFactory (sou سوف تحتاج إلى إضافة جديدة التبعية إلى منشئ فئة) سوف تنتج نفس النتيجة...

$product = $this->_productFactory->create()->setStoreId($storeId)->load($params['product']);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى magento.stackexchange
scroll top