Use Magento’s Core Shopping Cart Model to store/access a list of products
-
16-10-2019 - |
Question
I’m looking for an elegant way to use Magento’s Core Shopping Cart Model to store/access a list of products on a visitor/customer session WITHOUT displaying the products in the basket and WITHOUT modifying magento's core files.
Solution
Easy! Just add a new quote_item attribute and filter for it in the quote_item_collection. If you need more details, tell me.
You implement your own module. You add a new attribute to the quote_item.
Specifiy a setup script
<?xml version="1.0"?>
<config>
<modules>
<Namespace_Module>
<version>1.0.0</version> <!-- this is the version for the install script -->
</Namespace_Module>
</modules>
<global>
<resources>
<namespace_module_setup> <!-- this is the name of the directory in sql -->
<setup>
<module>Namespace_Module</module>
<class>Mage_Sales_Model_Entity_Setup</class> <!-- this is important, because in newer versions (magento >1.6?) all sales entity (quote, quote_item, order, order_item, ...) are flat tables -->
</setup>
</namespace_module_setup>
</resources>
</global>
</config>
add a install script:
app/code/(local|community)/namespace/module/sql/namespace_module_setup/install-1.0.0.php
<?php
/* @var $this Mage_Eav_Model_Entity_Setup */
$this->startSetup();
$this->addAttribute(
'quote_item',
'hide',
array(
'visible_on_front' => 0,
'type' => 'int',
'label' => 'Hide in cart',
'required' => 0,
'user_defined' => 1
// other values and config settings can be seen here: /app/code/core/Mage/Eav/Model/Entity/Setup.php:592
)
);
$this->endSetup();
Then you set the new attribute on the quote_item after you added it to the cart $quoteItem->setHide(1); it is saved automatically (clean the cache!).
And then you can use any event, e.g. eav_collection_abstract_load_before
:
<frontend>
<events>
<eav_collection_abstract_load_before>
<observers>
<your_name>
<type>singleton</type>
<class>namespace_module/observer</class>
<method>eavCollectionAbstractLoadBefore</method>
</your_name>
</observers>
</eav_collection_abstract_load_before>
</events>
</frontend>
In this method you check whether it is a Mage_Sales_Model_Entity_Quote_Item_Collection
and then you add a addAttributeToFilter('hide', 1)
then the items are not loaded.