2

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.

Screenshot:

enter image description here

<?php
    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;
        parent::__construct($context);
    }

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

        $selectedOrdersList=$this->getRequest()->getParam('orderinc');

        $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;

    }
}

Conditions:

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. 
Murtuza Zabuawala
  • 14,814
  • 9
  • 44
  • 75
Nagaraju Kasa
  • 5,856
  • 6
  • 53
  • 113
  • 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 Kasa 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
  • Before that can you tell me how can i call a controller in a custom module my file is Here in my module "/app/code/Multiple/Addtocart/Controller/Index/addcart.php" and i have try to call this like example.com//multiple/addtocart/index/cartadd.php" but its showing 404 error – amith lal Sep 21 '17 at 12:09
  • here is my question https://magento.stackexchange.com/questions/194142/add-products-through-custom-module-in-magento2 – amith lal Sep 21 '17 at 12:18
  • @NagarajuKasa can you please share the code how did you manage to achieve this? – manini Nov 23 '17 at 09:48
  • 1
    hi @manini please check this link https://github.com/NagarajuKasa/Nagmagento2/blob/master/Paytradecommission.php – Nagaraju Kasa Nov 23 '17 at 10:25
  • @NagarajuKasa Thanks for the link. However, I cannot understand where are they applying custom price for the cart products. Can u please explain? – manini Nov 23 '17 at 10:28
  • 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 Kasa Nov 23 '17 at 10:35
  • @NagarajuKasa How are we passing it to the observer here? – manini Nov 23 '17 at 11:23
  • https://magento.stackexchange.com/questions/94265/how-to-set-retrieve-and-unset-session-variables-in-magento-2 – Nagaraju Kasa Nov 23 '17 at 11:52
  • @NagarajuKasa It kind of helped me and gave me some direction on how should I proceed. Please post ur answer here and I will be happy to upvote it: https://magento.stackexchange.com/questions/201995/adding-products-to-cart-programmatically-with-custom-price-in-controller-magent?noredirect=1#comment280849_201995 – manini Nov 25 '17 at 09:14
  • could you post me what exact you want to do in the github – Nagaraju Kasa Nov 25 '17 at 11:34
  • please confirm whether you are able to add to cart a product with the single item? – Nagaraju Kasa Nov 25 '17 at 11:34
  • @NagarajuKasa Yes I am able to add to cart a single product. – manini Nov 29 '17 at 07:02
  • Try this https://webkul.com/blog/magento2-create-customer-quote-add-products-quote/ – Saravanan DS Dec 19 '18 at 14:45

3 Answers3

1

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;
         parent::__construct($context);
     }
     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);
                 $this->cart->save();
             }
SathishrajRaju
  • 376
  • 1
  • 5
0

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

$item->setCustomPrice(20);
$item->setOriginalCustomPrice(20);
$item->getProduct()->setIsSuperMode(true);
$item->setQty(0);
$item->save();
Jjo
  • 1,158
  • 8
  • 17
0

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);
$this->cart->save();

The missing pieces feel free to fill them in.

Chiriac Victor
  • 306
  • 2
  • 12