Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento 2, Magento Development, Customization, Extension Development and Integration, Optimization, SEO and Responsive Design

Magento Add Product to Cart With Custom Price

You want to change the price of product while it’s being added to the cart? Here’s the simple example of how it can be done.

We need to create a simple magento module with one observer.

Download Module

1. Directories and files needed for the module

app/code/community/HK/PriceChange
app/code/community/HK/PriceChange/etc
app/code/community/HK/PriceChange/etc/config.xml
app/code/community/HK/PriceChange/Model
app/code/community/HK/PriceChange/Model/Observer.php
app/etc/modules/HK_PriceChange.xml

2. Content of HK_PriceChange.xml

<?xml version="1.0"?>
<config>
    <modules>
        <HK_PriceChange>
            <active>true</active>
            <codePool>local</codePool>
        </HK_PriceChange>
    </modules>
</config> 

3. config.xml
In config.xml we must define an observer which looks for this event: ‘checkout_cart_product_add_after’.

<?xml version="1.0"?>
<config>
    <modules>
        <HK_PriceChange>
            <version>0.1.0</version>
        </HK_PriceChange>
    </modules>
       
    <global>
        <models>
            <hk_pricechange>
                <class>HK_PriceChange_Model</class>
            </hk_pricechange>
        </models>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <hk_pricechange_observer>
                        <class>hk_pricechange/observer</class>
                        <method>change_price</method>
                    </hk_pricechange_observer>
                </observers>
            </checkout_cart_product_add_after>
        </events>
    </global>
</config>

4. Observer.php

<?php
class HK_PriceChange_Model_Observer
{
    public function change_price(Varient_Event_Observer $observer) {

        $newPrice = 50;
        $item = $observer->getQuoteItem();
        $item->setCustomPrice($newPrice);
        $item->setOriginalCustomPrice($newPrice);
        $item->getProduct()->setIsSuperMode(true);
        
        /*To get all custom option*/
        /*$_customOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
        foreach($_customOptions['options'] as $_option){
            echo $_option['label'] .'=>'. $_option['value']."<br/>";
        }*/
        
        //echo $item->getProduct()->getId();
    }
}

That's it. Flush your cache and try to add product to the cart.

You will notice that every product will now cost $50 after being added to the cart.

You can add, for example, product id’s check, so only certain products can be changed, or you can write the logic for some kind of price discount. The possibilities are endless.

Please support us, Like us on Facebook.

  1. Great article! I think this article will support and complement your point
    Florida Web Development

    ReplyDelete

 

Copyright @ 2017 HKBlog.