Magento: add product twice to cart, with different attributes!
I have been working with this for a whole day but i cannot find any solution:
I have a product (lenses), which has identical attributes, but user can choose one attribute set for one eye and another attribute set for another.
On the frontend I got it ok, see it here.
So the user can select attributes for left or right eye, but it is the same product.
I build a function, which should take a product in a cart (before save), add other set of attributes, so there should be two products in the cart. What happens is there are two products, but with the same set of attributes???
Here is the 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 ----------------开发者_StackOverflow中文版--------------------------
$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, product added, with 2 quantity , but 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 more to do, please any advice, this is my first module in Magento…
Thank you, Peter
Go to class Mage_Sales_Model_Quote
and search for the method getItemByProduct($product)
and make it return false;
all the time.
Be careful, this might interfere with the minimum
& maximum
allowed in cart for products.
i think your main problem is how you set up the products the attribute should be something like lens strength right eye, lens strength left eye, lens thickness right eye, lens thickness left eye. one attribute set can hold the different values you need
if you want to use separate attribute sets you could use bundled products ie all products come as a bundle and you need to add lenses to your purchase, lenses are a separate product with different attribute sets and pricing (which could be set to none)
maybe neither of these is a perfect fix but with some css and templating it should be able to look professional enough
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 in 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');
}
精彩评论