4

i want to change product name to custom name when added to cart:

i wrote below code to change price ,but unable to change product name why so ,what to do for that

<checkout_cart_product_add_after>
    <observers>
        <setadiscountprice>
            <class>Mour_Customgroup_Model_Observer</class>
            <method>modifyPrice</method>
        </setadiscountprice>
    </observers>
</checkout_cart_product_add_after>

observer

public function modifyPrice(Varien_Event_Observer $obs ){

    $item = $obs->getEvent()->getQuoteItem();
    $product_id=$item->getProductId();
    $_product=Mage::getModel('catalog/product')->load($product_id);
    $price=$_product->getPrice();

    $specialPrice = $price+10;

    // Make sure we don't have a negative
    if ($specialPrice > 0) {
        $item->setCustomPrice($specialPrice);
        $item->setOriginalCustomPrice($specialPrice);
        $item->setName('helllloo');
        $item->getProduct()->setIsSuperMode(true);
    }

}
  • you want to change particular product name only or for all – saravanavelu Jan 10 '15 at 6:04
  • Please try with $item->getProduct()->setName('helllloo');. – michael Jan 11 '15 at 11:35
2

The problem is, that inside the cart checkout/cart/render/simple.phtml the default to get the name is:

<?php echo $this->escapeHtml($this->getProductName()) ?>

And this gets the name from the product, rather then from the item.

public function getProductName()
{
    if ($this->hasProductName()) {
        return $this->getData('product_name');
    }
    return $this->getProduct()->getName();
}

So you need to change this to $_item->getName() then you get the set name.

| improve this answer | |
1

Why change the name of the product on cart add? Just add a secondary product attribute and then display that in the cart item render rather than the product name.

| improve this answer | |
  • 1
    And review step and order item renderer and invoice item renderer and credit memo item renderer and email templates and PDFs... I think changing the item name is actually simpler ;) – Fabian Schmengler Sep 8 '15 at 5:06
  • Wonderfully thought out Fabian :) – philwinkle Sep 8 '15 at 5:42

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.