Creating a custom price for a specific product and customer in Magento 2 involves price rules and customer group prices. Magento 2 offers flexible pricing options, allowing you to set custom prices for specific products and customers. You can also define special prices customized to individual customers or customer groups by leveraging customer group pricing.

Steps to Create a Custom Price For a Product in Magento 2

We can set a special price for a customer when they put the product in their shopping cart which will rise automatically.

To create a custom price for a specific product and customers follow the below steps:

Step 1: Create a “registration.php”

app/code/Dckap/EventsObservers/registration.php

<?php


\Magento\Framework\Component\ComponentRegistrar::register(
   \Magento\Framework\Component\ComponentRegistrar::MODULE,
   'Dckap_EventsObservers',
   __DIR__
);

Step 2: Create a “Module.xml” file 

The File Path: 

app/code/Dckap/EventsObservers/etc/module.xml

<?xml version="1.0" ?>
   <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
   <module name="Dckap_EventsObservers" setup_version="1.0.0"/>
</config>

Step 3: Create one more file “events.xml” 

The file path: 

app/code/Dckap/EventsObservers/etc/frontend/events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
   <event name="catalog_controller_product_view">
       <observer name="DckapProductData" instance="Dckap\EventsObservers\Observer\Product\Data" />
   </event>
   <event name="checkout_cart_product_add_after">
       <observer name="customprice" instance="Dckap\EventsObservers\Observer\CustomPrice" />
   </event>
</config>

Step 4: Create “Data.php”

app/code/Dckap/EventsObservers/Observer/Product/Data.php

<?php


namespace Dckap\EventsObservers\Observer\Product;


use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;


/**
* Class Data
* @package Dckap\EventsObservers\Observer\Product
*/
class Data implements ObserverInterface
{
   /**
    * @param Observer $observer
    */


   public function execute(Observer $observer)
   {
       $product = $observer->getProduct();
       $originalName = $product->getName();
       $modifiedName = $originalName . ' - Modified by Magento 2 Events and Observers';
       $product->setName($modifiedName);
   }
}

Step 5: Create “CustomPrice.php”

app/code/Dckap/EventsObservers/Observer/CustomPrice.php

<?php


namespace Dckap\EventsObservers\Observer;


use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;


/**
* Class CustomPrice
* @package Dckap\EventsObservers\Observer
*/
class CustomPrice implements ObserverInterface
{
   /**
    * @param \Magento\Framework\Event\Observer $observer
    */


   public function execute(\Magento\Framework\Event\Observer $observer)
   {
       $item = $observer->getEvent()->getData('quote_item');
       $product = $observer->getEvent()->getData('product');


       //here you add your logic


       $price = $item->getProduct()->getFinalPrice() + 25;


       $item->setCustomPrice($price);
       $item->setOriginalCustomPrice($price);
       $item->getProduct()->setIsSuperMode(true);
   }


}

Now, let’s discuss how you can use the custom price functionality in Magento 2 after implementing the module with event observers.

Read Also: Create Custom Magento 2 CMS Directives

Module Installation

  • Install the custom price module by placing the files in the specified directory structure within your Magento 2 instance.
  • Run the necessary commands to enable the module, such as setup:upgrade and cache:clean.
  • Set Custom Price Logic:

Open the CustomPrice.php 

Observer file located at app/code/Dckap/AlphanumericTax/Observer/CustomPrice.php.

  • Within the execute method of this file, define the logic for calculating the custom price based on your requirements. In the provided example, a fixed amount of 25 is added to the product’s final price.

Recommended Read: How to Create Tax for a Specific Region in Magento 2

Configure Event Observers in your Module

  • Event observers are configured in the sales.xml file located at app/code/Dckap/AlphanumericTax/etc/sales.xml. Here, two events are observed:
  • catalog_controller_product_view: This event is triggered when a product page is viewed. The Data observer modifies the product name.
  • checkout_cart_product_add_after: This event is triggered when a product is added to the shopping cart. The CustomPrice observer adjusts the product price.

Final Testing the Price Logic

  • Add a product to the shopping cart from the front end of your Magento 2 store.
  • Verify that the product’s price is increased automatically by the custom amount defined in the observer logic.
  • Additionally, navigate to a product page and ensure that the product name is modified as per the logic defined in the Data observer.

Output for Custom Price

Customize Prices for Products and Customers in Magento 2 with Klizer 

Customizing prices for specific products and customers in Magento 2 allows you to offer personalized pricing options, cater to different customer segments, and implement targeted promotional strategies. 

By following the steps outlined above, you can create custom prices tailored to individual customers or customer groups, enhancing the shopping experience and driving customer satisfaction and loyalty. Leveraging Magento 2’s flexible pricing capabilities empowers you to optimize your pricing strategy and maximize revenue opportunities.

Get in touch with us to get further inquiries and explore how our Magento development solutions can benefit your business.

FAQs

How to Use a Custom Price Template in Magento 2

  1. Set Up a Sample Module: Start by creating a sample module to work with.
  2. Create a Block: Develop a block that will handle the custom logic.
  3. Add a New Price Render Handle: Implement a new handle for rendering prices.
  4. Create a Price Render Class: Build the class responsible for custom price rendering.
  5. Override the Product List Block: Modify the relevant product list block to incorporate your custom template.
  6. Create the Custom Template File: Finally, design and add your custom template file.

How to Set Special Prices in Magento 2 Programmatically

To get a product’s special price by its ID, use the getSpecialPriceByProId($proId) function, passing the product ID as a parameter. If you need to get the special price using the product’s SKU, call the getSpecialPriceByProSku($proSku) function, passing the product SKU as the parameter.

How to Get Custom Customer Attribute Value in Magento 2

In some cases, like when integrating with third-party APIs, you might need to retrieve custom customer attribute values in your Magento 2 store. These APIs may return customer attribute values that aren’t part of the default Magento 2 setup, and you’ll need to access these custom attributes for further use.

Rathina Pranesh C

Rathina Pranesh C is a Software Engineer at Klizer, specializing in Magento ecommerce solutions. With over 1.5 years of hands-on experience in the Magento platform, Rathina is dedicated to creating seamless online shopping experiences. His passion for coding and commitment to excellence drives them to continuously innovate and deliver top-notch solutions to enhance the field of ecommerce.

Get in Touch

Get Expert Advice

At Klizer, we bring over 18 years of specialized expertise in ecommerce website development and design.

© Copyright 2024 Klizer. All Rights Reserved