0

I need to add an attribute to a product as soon as I add it to the shopping cart.

I have seen that you can add a custom attribute via the admin panel, but it just applies to the product itself. Is this even possible?

1 Answer 1

1

Attributes are properties of entities. Entities would be (as examples) products, and customers.

So, considering that titbit of information, to answer your questions: No. You cannot assign an attribute to a product, just because it is in the cart. If you do, that would be a property of the product, and not of the item in the cart.

However, what you may be looking for is to attach a value to the product, as added to the cart. You can then read that value back at different stages, even as part of the sales order item (sold item)

You can do this via an event: checkout_cart_product_add_after Observer that event, and do some kind as follows:

public function checkout_cart_product_add_after(Varien_Event_Observer $observer)
    {
        try {
            $product = $observer->getEvent()->getProduct();
            $quoteItem = $observer->getEvent()->getQuoteItem();
                // update the qty's just to make sure all is as it shoudl be.
                $option = Mage::getModel('sales/quote_item_option')
                    ->setProductId($product->getId())
                    ->setCode('YOUR CUSTOM VALUE CODE')
                    ->setProduct($product)// needed for EE only ?
                    ->setValue(VALUE YOU WANT TO STORE);
                $quoteItem->addOption($option);
        } catch (Exception $e) {
            // log any issues, but allow system to continue.
            Mage::logException($e);
            if (Mage::getIsDeveloperMode()) {
                die($e->getMessage());
            }
        }

        return $this;
    }

You can read this value back as follows. Note that that the product options can be read back slightly different, depending if the option is set against the product object, or the item object of the cart.

I use the following routine as a common routine in my modules to get the correct value. In this example, I am pulling back the value of 'Info_Buyrequest'

public function getBuyRequestData($item)
    {
        if ($item instanceof Mage_Sales_Model_Quote_Item) {
            $infoBuyRequest = $item->getOptionByCode('info_buyRequest');
            if (!is_object($infoBuyRequest)) {
                return false;
            }
            $buyRequest = new Varien_Object(unserialize($infoBuyRequest->getValue()));
        } else {
            if ($item instanceof Mage_Sales_Model_Order_Item) {
                $buyRequest = new Varien_Object($item->getProductOptions());
            } else {
                if ($item instanceof Mage_Catalog_Model_Product) {
                    $infoBuyRequest = $item->getCustomOption('info_buyRequest');
                    if (!is_object($infoBuyRequest)) {
                        return false;
                    }
                    $buyRequest = new Varien_Object(unserialize($infoBuyRequest->getValue()));
                } else {
                    if (!is_object($item)) {
                        return false;
                    }
                    $infoBuyRequest = $item->getCustomOption('info_buyRequest');
                    if (!is_object($infoBuyRequest)) {
                        return false;
                    }
                    $buyRequest = new Varien_Object(unserialize($infoBuyRequest->getValue()));
                }
            }
        }
        if (is_object($buyRequest)) {
                return $buyRequest;
        }

        return false;
    }

Hope this helps

3
  • I used $infoBuyRequest = $item->getCustomOption('info_buyRequest'); bit (where $item, in my case is a Mage_Catalog_Model_Product ), with my custom attribute, but it's not being returned. It was in fact saved in the db, though. Is there anything else I could try?
    – Ramses
    Mar 1, 2016 at 4:06
  • Simply just try $item->getCustomOption() which shoudl return all custom options. Possible it is not in info_buyRequest itself.
    – ProxiBlue
    Mar 1, 2016 at 8:32
  • Tried getCustomOptions(), but it returns null...
    – Ramses
    Mar 2, 2016 at 3:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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