How to create custom messages in magento when a user adds an item to a cart?

How to create custom messages in magento when a user adds an item to a cart?

July 3, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to create custom messages in magento when a user adds an item to a cart?

Creating Custom Cart Messages in Magento

Creating custom messages that appear when users add items to their shopping cart is a common requirement for e-commerce sites using the Magento platform. In this guide, we’ll walk you through the process of creating a new product attribute and then displaying a custom message based on this attribute whenever an item is added to the cart.

Step 1: Create a New Product Attribute

First, let’s create a new attribute called “Price Factor” that will allow us to adjust prices in real-time. Here are the steps:

  1. Log in to your Magento admin panel and navigate to Catalog > Attributes.
  2. Click on the “Add New Attribute” button.
  3. Fill out the following fields:
    • Attribute Code: price_factor
    • Scope: Global
    • Input Type: Text Field
  4. Click on the “Save Attribute” button.

Now, you can set integer values for this attribute from the product page in the admin panel.

Step 2: Set Up the Custom Message Logic

To display a custom message when an item is added to the cart, we need to modify the core Magento code. Here’s how:

  1. Locate the file app/code/core/Mage/Checkout/controllers/CartController.php and open it for editing.
  2. Find the addAction() method within this controller.
  3. Inside this method, after the $quote->addProduct($product); line, add the following PHP code:
$priceFactor = $product->getData('price_factor');
if ($priceFactor) {
    $oldPrice = $product->getOriginalFinalPrice();
    $newPrice = $product->getFinalPrice();

    if ($newPrice < $oldPrice) {
        Mage::getSingleton('core/session')->addSuccess("Your old price was {$oldPrice} and your new price is {$newPrice}");
    }
}

This code retrieves the “price_factor” attribute value for the added product. If it’s set, it calculates the original and final prices, compares them, and displays a success message if there’s a discount.

Step 3: Test Your Custom Message

Now, let’s test our custom shopping cart message:

  1. Add a product to your Magento site.
  2. Set a “price_factor” value for this product in the admin panel.
  3. When you add this product to the shopping cart on the frontend, check if the success message appears, indicating the change in price.

Best Practices and Further Reading

For best practices in Magento customization:

  • Always use Magento’s built-in mechanisms for changing the core code, such as creating an extension or overriding the core files via themes.
  • Regularly backup your site before making significant changes to prevent data loss.
  • Test thoroughly on a staging environment before deploying changes to production.

To learn more about Magento customizations and extensions, check out our comprehensive guide at Laravel Company: Mastering Magento Custom Development.

Conclusion

In this post, we demonstrated how to create a new product attribute and display a custom message when an item with this attribute is added to the shopping cart in Magento. By following these steps and best practices, you can easily implement this feature on your e-commerce site.

Remember that Magento offers numerous ways to customize its behavior, from adding attributes to overriding controllers. With some PHP knowledge and careful testing, you can make your store stand out with tailored customer experiences like our custom shopping cart message example.

Happy coding!

Checkout Catalog Admin Pricing

Our network