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:
<?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.