/    Sign up×
Community /Pin to ProfileBookmark

Magento – Category of Products with Observer condition Error?

Condition: I have restricted category of products [category id = 680, 894, 895], this category of products should be purchase alone, not mixed with other products.

Workout: Case 1: If cart had other products, if the customer tries to add restricted category products trigger observer like not eligible to add to cart and display a message like **If you want this product, Purchase alone not mixed with other Products**

case 2: If cart had a restricted category of products if customer try to add non-restricted products trigger observer like not eligible to add to cart and display a message like **Cart has Special Product you can not add another**
code :

**app/etc/modules/Gta_KolupadiRestrict.xml**

“`
<?xml version=”1.0″ encoding=”UTF-8″?>
<config>
<modules>
<Gta_KolupadiRestrict>
<active>true</active>
<codepool>local</codepool>
</Gta_KolupadiRestrict>
</modules>
</config>
“`

**app/code/local/Gta/KolupadiRestrict/etc/config.xml**

“`
<?xml version=”1.0″?>
<config>
<modules>
<Gta_KolupadiRestrict>
<version>1.0.0</version>
</Gta_KolupadiRestrict>
</modules>
<global>
<models>
<gta_kolupadirestrict>
<class>Gta_KolupadiRestrict_Model</class>
</gta_kolupadirestrict>
</models>
<events>
<checkout_cart_product_add_after>
<observers>
<Gta_KolupadiRestrict_Model_Observer>
<type>singleton</type>
<class>Kolupadi_Restrict_Model_Observer</class>
<method>cartevent</method>
</Gta_KolupadiRestrict_Model_Observer>
</observers>
</checkout_cart_product_add_after>
</events>
</global>
</config>
“`

**app/code/local/Gta/KolupadiRestrict/Model/Observer.php**

“`
<?php
Mage::log(‘fine dude’, null, ‘logfile.log’);
//create class
class Gta_KolupadiRestrict_Model_Observer
{

//create function
public function cartevent(Varien_Event_Observer $observer)
{
$category_id = array(680, 894, 895) ; //category ids

$category_products = Mage::getModel(‘catalog/category’)
->setWebsiteId(2); // load website id
->load($category_id); // load category

// check cart qty status
$cart_qty = (int) Mage::getModel(‘checkout/cart’)->getQuote()->getItemQty();

//logic
if($category_products && $cart_qty > 0 )
{
Mage::throwException(“If you want Kolu Padi, Purchase alone not mixed with other Products”);
}

//check if cart have products
$quote = Mage::getModel(‘checkout/cart’)->getQuote();
foreach($quote->getAllItems() as $item)
{
$productId = $item->getCategoryId();

if($productId == $category_id)
{
Mage::throwException(“Cart has Special Product you can not add another”);
}
}
}
}
?>
“`

Observer not trigger. Does anyone help me?

to post a comment
PHP

0Be the first to comment 😎

×

Success!

Help @Aveeva7 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...