0

I'm trying to add to cart a product with custom and base price. It's working file.

The problem is that Stripe checkout module has an update button that adds the address of a guest customer to billing address, and when that happens my product's price get backs to original.

Here is my controller where I set the custom price.

public function execute(){
        $request = $this->getRequest()->getPostValue();
        $product_id = $request['product_id'];
        $price = $request['grand_total'];
        $params = array(
            'formKey' => $this->formKey,
            'product_id' => $product_id,
            'qty' => 1
        );

        $productFactory = $this->product->create();
        try{
            $product = $productFactory->load($product_id);
           
            $product->setPrice($price);
            $product->setBasePrice($price);
            $product->setIsSuperMode(true);
            $this->cart->addProduct($product,$params);
            $this->cart->save();
        }
        catch(\Exception $e){
            return $request;
        }
    }

Is there any solution for this that does not involves modifying Stripe's module ?

Maybe changing product's price from database dinamically?

Thanks and greetings!

1

Step 1: Create Events/xml File

Firstly, you need to create events/xml in the folder Vendor/Module/etc/frontend and use event checkout_cart_product_add_after

<?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="checkout_cart_product_add_after">
        <observer name="customprice" instance="Vendor\Module\Observer\CustomPrice" />
    </event>
</config>

Step 2: Create CustomPrice.php File

Now, you have to create CustomPrice.php file that override your price in the Observer Folder.

<?php
    /**
     * Vendor Module CustomPrice Observer
     *
     * @category    Vendor
     * @package     Vendor_Module
     * @author      Vendor Module
     *
     */
    namespace Vendor\Module\Observer;
 
    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\App\RequestInterface;
 
    class CustomPrice implements ObserverInterface
    {
        public function execute(\Magento\Framework\Event\Observer $observer) {
            $item = $observer->getEvent()->getData('quote_item');         
            $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
            $price = 100; //set your price here
            $item->setCustomPrice($price);
            $item->setOriginalCustomPrice($price);
            $item->getProduct()->setIsSuperMode(true);
        }
 
    }

I think after that your stripe module will not change price. Note: According to your need for setting a custom price for one or more products, you can manipulate this by adding conditions.

accept solution if works for you.

2
  • Hi, it worked, but do you know how can I send $price value to the Observer? I don't know how to send paremeters to observer. Greetings! May 26 at 1:26
  • You can create a session variable and unset it once price set successfully.. May 26 at 3:11

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.