简体   繁体   中英

Getting product attributes in event checkout_cart_product_add_after

I have observer for Magento event checkout_cart_product_add_after. Now I need to check if for example t-shirt size is same what user had given to Magento in my custom module. How can I get those product attributes in my observer?

class Company_ModuleSizes_Model_Sizes_Observer extends Mage_Core_Model_Abstract
{

    public function check_sizes($observer)
    {       
        // Get quote item
        $event = $observer->getEvent();
        $quoteItem = $event->getQuoteItem();

        // How can I get product attributes from $quoteItem  ?

        return $this;
    }

}

尝试这个:

$_options = $quoteItem->getProduct()->getData('your-attribute');

I am using this code to get the product attributes in the Observer.php . Hope this helps someone

$product->getResource()->getAttribute('selling_type')->getFrontend()->getValue($product);
<?php
class Company_ModuleSizes_Model_Sizes_Observer extends Mage_Core_Model_Abstract
{
    public function check_sizes($observer)
    {       
        // Get Quote Item
        $event = $observer->getEvent();
        $quoteItem = $event->getQuoteItem();
        $product = $event->getProduct();

        // The options provided by the customer is available using the following statement
        $_optionsQuoteItem = $quoteItem->getProduct()->getData('your-attribute');

        // The options which are available for the product in general is available using the following statement
        $_optionsProduct = $product->getData('your-attribute');

        // Now you can process your required logic in here, with the above two variables

        return $this;
    }
}

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM