Magento: add the product twice to the cart, with different attributes!

I have been working with this all day, but I cannot find a solution:

I have a product (lenses) that has the same attributes, but the user can select one attribute set for one eye and another attribute set for the other.

In the interface, I got it ok, see here .

Thus, the user can select the attributes for the left or right eye, but this is the same product.

I create a function that should take the product to the basket (before saving), add another set of attributes, so there should be two products in the basket. What happens, there are two products, but with the same set of attributes ???

Here is a snippet of the function:

$req = Mage::app()->getRequest(); $request['qty'] = 1; $request['product'] = 15; $request['uenc'] = $req->get('uenc'); $request['options'][1] = 1; $request['options'][3] = 5; $request['options'][2] = 3; $reqo = new Varien_Object($request); $newitem = $quote->addProduct($founditem->getProduct(), $reqo); //add another one ------------------------------------------ $request['qty'] = 1; $request['product'] = 15; $request['uenc'] = $req->get('uenc'); $request['options'][1] = 2; $request['options'][3] = 6; $request['options'][2] = 4; $reqo = new Varien_Object($request); $newitem = $quote->addProduct($founditem->getProduct(), $reqo); 

Or another test, with some other functions (again, with the addition of a product, with 2 quantities, but with the same attributes ...):

 $req = Mage::app()->getRequest(); $request['qty'] = 1; $request['product'] = 15; $request['uenc'] = $req->get('uenc'); $request['options'][1] = 2; $request['options'][3] = 6; $request['options'][2] = 4; $product = $founditem->getProduct(); $cart = Mage::getSingleton('checkout/cart'); //delete all first… $cart->getItems()->clear()->save(); $reqo = new Varien_Object($request); $cart->addProduct($founditem->getProduct(), $reqo); $cart->getItems()->save(); $request['options'][1] = 1; $request['options'][3] = 5; $request['options'][2] = 3; $reqo = new Varien_Object($request); $cart->addProduct($founditem->getProduct(), $reqo); $cart->getItems()->save(); 

I really don't know what else needs to be done, please, any advice, this is my first module in Magento ...

Thanks Peter

+4
source share
3 answers

Go to the Mage_Sales_Model_Quote class and find the getItemByProduct($product) method and make it return false; all the time. Be careful, this may interfere with the minimum and maximum allowed in the food basket.

+3
source

I think your main problem is how you set up the products, the attribute should be something like a right eye strength lens, a left eye strength lens, a right eye thickness lens, a left eye lens thickness. one set of attributes may contain different values ​​that you need

If you want to use separate sets of attributes, you can use related products, that is, all products are delivered as a package, and you need to add lenses to your purchase, lenses are a separate product with different sets of attributes and price (which can be set to no )

maybe none of them is the perfect fix, but with some CSS and templates it should look professional enough

+1
source

You can create an observer

 <catalog_product_load_after> <observers> <subscription> <type>singleton</type> <class>Itdelight_Subscription_Model_Observer</class> <method>catalogProductLoadAfter</method> </subscription> </observers> </catalog_product_load_after> 

And then add your unique attribute to the product.

 public function catalogProductLoadAfter(Varien_Event_Observer $observer){ $action = Mage::app()->getFrontController()->getAction(); if ($action->getFullActionName() == 'checkout_cart_add') { if ($options = $action->getRequest()->getParam('spy')) { $product = $observer->getProduct(); $product->addCustomOption('product_attribute','subscription'); $a=$product->getCustomOption('product_attribute'); } 
0
source

Source: https://habr.com/ru/post/1313093/


All Articles