I have a created a product with price: 0, Now i would like to add a product with custom price from my custom controller. since i am submitting a custom form in the customer dashboard to my controller. i have placed code as shown below. please suggest me how i can achieve this.


enter image description here

    namespace CompanyName\ModuleName\Controller\Customer;
    use Magento\Framework\Controller\ResultFactory;
    use Magento\Framework\App\Action\Action;
    use Magento\Framework\App\Action\Context;
    use Magento\Framework\View\Result\PageFactory;

class Paytradecommission extends \Magento\Customer\Controller\AbstractAccount

    protected $resultPageFactory;
    protected $formKey;
    protected $request;

    public function __construct(
    Context $context,
    \Magento\Framework\Data\Form\FormKey $formKey,
    PageFactory $resultPageFactory,
    array $data = []) 
        $this->formKey = $formKey;
        $this->resultPageFactory = $resultPageFactory;

     * @return \Magento\Framework\View\Result\Page
    public function execute()


        $resultPage = $this->resultPageFactory->create();
            $params = array(
                'form_key' => $this->formKey->getFormKey(),
                'product' =>2047,//product Id
                'qty'   =>1,//quantity of product
                'price' =>100 //product price

            $this->_redirect("checkout/cart/add/form_key/", $params);
        /** @var \Magento\Framework\View\Result\Page $resultPage */
        return $resultPage;



1. I am able to add a product to cart but it shows '0' but unable to add custom price.

2. After add a product it should redirect to checkout page. 
  • 1
    didi you get the solution – amith lal Sep 21 '17 at 11:34
  • thanks for u r comment @amith how r u? i got the solution for the above question. – Nagaraju K Sep 21 '17 at 11:59
  • I'm Good! Thats great .I also having a same kind of issue i will post the question and share with you. – amith lal Sep 21 '17 at 12:06
  • 1
    hi @manini please check this link github.com/NagarajuKasa/Nagmagento2/blob/master/… – Nagaraju K Nov 23 '17 at 10:25
  • 1
    sure actually while adding product via controller i was facing an issue with the adding custom price to cart. hence i kept 'price' in session variable and passed to observer checkout_cart_product_add_after – Nagaraju K Nov 23 '17 at 10:35

Please find the below solution. It will works.

     public function __construct(
         \Magento\Framework\App\Action\Context $context,
         \Magento\Framework\View\Result\PageFactory $resultPageFactory,
         \Magento\Catalog\Model\Product $product,
         \Magento\Framework\Data\Form\FormKey $formKey,
         \Magento\Checkout\Model\Cart $cart
     ) {
         $this->resultPageFactory = $resultPageFactory;
         $this->cart = $cart;
         $this->product = $product;
         $this->formKey = $formKey;
     public function execute()
         $data = $this->getRequest()->getParams();
        $currentQty = $data['qty'];
        $currentProId = $data['product'];
        $currCount = $data['id'];
        $CurrPrice = $data['price'];
             $params = array();
             $params['qty'] = $currentQty;//product quantity
             $params['price'] = $CurrPrice;//product price
              $pId = $currentProId;//productId
             $_product = $this->product->load($pId);
             if ($_product) {
                 $this->cart->addProduct($_product, $params);
  • its not working sathisraj it showing blank page – Nagaraju K Jun 22 '17 at 13:33
  • It is not working @sathishraj – Venkatesh Apr 6 '18 at 12:11

Can you try the following code?. May be solve your problem.

  • how can i get $item ? – Nagaraju K Jun 22 '17 at 12:15
  • you can load product using product id – Jjo Jun 22 '17 at 12:21
  • @Jjo This is code for the observer. Is there any way to add the custom price for a product through a controller? – manini Nov 23 '17 at 9:46

Working solution and really easy if you think about it:

$params = array(
  'form_key' => $this->_formKey->getFormKey(),
  'product' => $item->getProductId(),
  'qty'   => $item->getQtyOrdered()

$product = $this->_product->load($productId); 
$product->setPrice($customPrice); // without save this does the trick
$this->cart->addProduct($product, $params);

The missing pieces feel free to fill them in.

  • this doesn't work i tried using price and CustomPrice – open-ecommerce.org Nov 19 '19 at 12:30

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.