2

"I Want to add multiple product using checkbox" but when i am doing that thing then it'll add to cart successfully but it sets product price as $0.00. I am using simple product.

  1. Request.php- Controller

namespace Deep\Module\Controller\Index;

use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Quote\Model\QuoteRepository;

class Request extends \Magento\Framework\App\Action\Action
{
protected $formKey;
protected $_productFactory;
protected $_cart;   
protected $messageManager;
public function __construct(
    Context $context,
    \Magento\Framework\Data\Form\FormKey $formKey,
    \Magento\Framework\Message\ManagerInterface $managerInterface,
    \Magento\Catalog\Model\ProductFactory $productFactory,
    \Magento\Checkout\Model\Cart $cart,
    Session $session,
    QuoteRepository $quoteRepository
) {
    parent::__construct($context);
    $this->formKey = $formKey;
    $this->_productFactory = $productFactory;
    $this->_cart = $cart;
    $this->messageManager = $managerInterface;
    $this->session = $session;
    $this->quoteRepository = $quoteRepository;
}

/** * Booking action * * @return void */ public function execute() { // 1. POST request : Get booking data $post = (array) $this->getRequest()->getPost();

if (!empty($post)) {
        // Retrieve your form data
    $firstname   = $post['fname'];
    $lastname    = $post['lname'];
    $email       = $post['email'];
    $phone       = $post['phone'];
    $comment     = $post['comment'];
    $product    = $post['chk'];
       // $newsletter  = $post['newsletter'];


    if($product){

        foreach ($product as $value) {


            if (isset($value)) {

             $this->addCartProduct($value);
         }



         $this->_cart->save();

     }
        // Doing-something with...
     $quoteId = $this->session->getQuote()->getId();
    //    echo $quoteId;exit(); 

     if ($quoteId) {
        $quote = $this->quoteRepository->get($quoteId);
        if (!$quote->getIsActive()) {
            return;
        }

// $product = $observer->getEvent()->getDataByKey('add_data');

        /** @var \Magento\Quote\Model\Quote\Item $item */
    $item = $this->session->getQuote();//->getItemByProduct($product);
    $quote->setFname($firstname);
    $quote->setLname($lastname);
    $quote->setEmail($email);
    $quote->setPhone($phone);
    $quote->setComment($comment);

    $quote->save();
}



        // Display the succes form validation message

$this->messageManager->addSuccessMessage('Quote Request Accepted...!!!');

        // Redirect to your form page (or anywhere you want...)
$resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
$resultRedirect->setPath('module/index/request');

return $resultRedirect;

} } // 2. GET request : Render the booking page $this->_view->loadLayout(); $this->_view->renderLayout();

}

public function addCartProduct($productID) {

$product = $this->_productFactory->create()->load($productID);
$info = new \Magento\Framework\DataObject(
    [
        'form_key' => $this->formKey->getFormKey(),
        'product_id' => $productID,
        'price' =>$product->getPrice()
    ]
);
return $this->_cart->addProduct($product, $info);

}

}

  • have you print_r($product->getData()) there . Just check are you getting price attribute in product data else load product and add loadded product to cart – Mukesh Prajapati Sep 28 '18 at 11:15
  • This is my code please give splution of this problem – Deep Shah Sep 28 '18 at 11:27
  • have you tried below code ? – Mukesh Prajapati Sep 28 '18 at 11:49
0

Try this code

 $product = $this->_productFactory->create()->load($productID);
    $info = new \Magento\Framework\DataObject(
        [
            'form_key' => $this->formKey->getFormKey(),
            'product' => $productID
        ]
    );
    return $this->_cart->addProduct($product, $info);
  • i am already using this code but it's not wirking, because i want to get price of priduct whose current id is $productId and i want to add proce of rhat perticuler producr in mini cart ehile add to cart. – Deep Shah Sep 28 '18 at 12:09
0

Hello @Deep You should get all selected checkbox's product id then load each product and perform add to cart.

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.