1

I have a few different attributes that can be configured for a product. When that product is added to the cart, theoretically the cart would check those attributes, and if they don't match, it creates a new line item.

Currently, it is just increasing the quantity, basically adding the item using whatever attributes were used the first time someone added the product to the cart.

These attributes are created via modules, not within the custom options of the product.

I've tried all of the different combinations of suggestions throughout this site and others, and nothing seems to fix the problem.

Setting up the columns worked just fine. They were added to both tables:

$setup = new Mage_Sales_Model_Mysql4_Setup('core_setup');
$setup->startSetup();
$setup->addAttribute('quote_item', 'exclude_protein', array('type' => 'varchar', 'visible' => true, 'required' => false));
$setup->addAttribute('order_item', 'exclude_protein', array('type' => 'varchar', 'visible' => true, 'required' => false));
$setup->addAttribute('quote_item', 'sub_protein', array('type' => 'varchar', 'visible' => true, 'required' => false));
$setup->addAttribute('order_item', 'sub_protein', array('type' => 'varchar', 'visible' => true, 'required' => false));
$setup->addAttribute('quote_item', 'exclude_sides', array('type' => 'varchar', 'visible' => true, 'required' => false));
$setup->addAttribute('order_item', 'exclude_sides', array('type' => 'varchar', 'visible' => true, 'required' => false));
$setup->addAttribute('quote_item', 'sub_sides', array('type' => 'varchar', 'visible' => true, 'required' => false));
$setup->addAttribute('order_item', 'sub_sides', array('type' => 'varchar', 'visible' => true, 'required' => false));
$this->endSetup();

The fieldsets and sales groups in the config file were added because that was the fix that was suggested the most. My cart acts the same whether that section is there or not.

<global>
    <fieldsets>
        <sales_convert_quote_item>
            <exclude_protein>
                <to_order_item>*</to_order_item>
            </exclude_protein>
            <sub_protein>
                <to_order_item>*</to_order_item>
            </sub_protein>
            <exclude_sides>
                <to_order_item>*</to_order_item>
            </exclude_sides>
            <sub_sides>
                <to_order_item>*</to_order_item>
            </sub_sides>
        </sales_convert_quote_item>
    </fieldsets>
    <sales>
        <quote>
            <item>
                <product_attributes>
                    <exclude_protein />
                    <sub_protein />
                    <exclude_sides />
                    <sub_sides />
                </product_attributes>
            </item>
        </quote>
    </sales>
    <models>
        <ecb_modifymeal>
            <class>ECB_ModifyMeal_Model</class>
        </ecb_modifymeal>
        <sales>
            <rewrite>
                <quote_item>ECB_ModifyMeal_Model_Sales_Quote_Item</quote_item>
            </rewrite>
        </sales>
    </models>
    <events>
        <sales_quote_add_item>
            <observers>
                <ecb_modifymeal_observer>
                    <class>ECB_ModifyMeal_Model_Observer</class>
                    <method>modifyPrice</method>
                </ecb_modifymeal_observer>
            </observers>
        </sales_quote_add_item>
    </events>
    <resources>
        <ecb_modifymeal_setup>
            <setup>
                <module>ECB_ModifyMeal</module>
                <class>Mage_Catalog_Model_Resource_Setup</class>
            </setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </ecb_modifymeal_setup>
    </resources>
</global>

In my observer, I am adjusting the price. That logic is removed since it is working just fine. It is represented by the "..." and is where I am defining the $increase_amount variable.

class ECB_ModifyMeal_Model_Observer {
    public function modifyPrice(Varien_Event_Observer $observer) {
        $item = $observer->getQuoteItem();
        $post_vals = Mage::app()->getRequest()->getParams();
        ...
        $final_price = $item->getProduct()->getPrice() + $increase_amount;

        $item->setData('exclude_protein', rtrim($exclude_protein, ','));
        $item->setData('sub_protein', rtrim($sub_protein, ','));
        $item->setData('exclude_sides', rtrim($exclude_sides, ','));
        $item->setData('sub_sides', rtrim($sub_sides, ','));

        $item->setCustomPrice($final_price);
        $item->setOriginalCustomPrice($final_price);
        $item->getProduct()->setIsSuperMode(true);
    }
}

This is what I've come up with so far for the ECB > ModifyMeal > Model > Sales > Quote > Item.php file. It adds the first item, and doesn't add any other of that product, even if it's the same:

class ECB_ModifyMeal_Model_Sales_Quote_Item
    extends Mage_Sales_Model_Quote_Item
{
    public function representProduct($product) {
        $parentResult = parent::representProduct($product);
        if ($parentResult === false) {
            return $parentResult;
        }
        $itemProduct = $this->getQuoteItem();

        $productExP = $product->getData('exclude_protein');
        $productSubP = $product->getData('sub_protein');
        $productExS = $product->getData('exclude_sides');
        $productSubS = $product->getData('sub_sides');

        $itemExP = $itemProduct->getData('exclude_protein');
        $itemSubP = $itemProduct->getData('sub_protein');
        $itemExS = $itemProduct->getData('exclude_sides');
        $itemSubS = $itemProduct->getData('sub_sides');

        if($productExP == $itemExP && $productSubP == $itemSubP && $productExS == $itemExS && $productSubS == $itemSubS){
            return true; //same product
        } else {
            return false; //different product
        }
    }
}
  • I'm not sure what you mean by "attribute" in this part : ` theoretically the cart would check those attributes, and if they don't match` – Julien Lachal Nov 19 '15 at 15:17
  • Unfortunately, it's not checking these particular values on its own. I have to compare them myself to see if the products are the same or not. I'm not sure how to get those values correctly. – PBwebD Nov 19 '15 at 15:22
0

you need to rewrite the method Mage_Sales_Model_Quote_Item::representProduct($product).
This one checks if you already have a product in the cart with the current configuration of the product you want to add.
By default it checks the custom options only.
Make it check your new fields also and return false if your values do not match.

  • I've added my update to config.xml, but I'm unsure of where to put the new method. I'm fairly new to Magento. Do I place this new class in: Model > Sales > Quote > Item.php ? – PBwebD Nov 19 '15 at 14:33
  • check here now to rewrite models in magento: inchoo.net/magento/how_to_override_magento_model_classes. – Marius Nov 19 '15 at 14:33
  • Perhaps I'm not getting the attribute values correctly? How do I get them inside of the class? What I have so far makes it so I am unable to add any products to the cart. That link doesn't show how to actually compare attributes. – PBwebD Nov 19 '15 at 15:10
  • not really sure about that. You can log the product model and check where your values are – Marius Nov 19 '15 at 15:21
  • Ok, it might be getting the attributes. It currently adds the product the first time, and then doesn't add any after that even if it's the same. – PBwebD Nov 19 '15 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.