0

I need to add to cart products with custom attribute price

Ex i am having a custom price as booking amount as 1000 for a product and original amount as 10000

when customer chooses booking amount option 1000 should be added to cart and if user chooses full payment 10000 should be added to cart ..

How to acheive this in magento

2 Answers 2

0

I would suggest using product options. It could allow you to have 2 different options for your product with a different price for each options without having to do something out of the box.

https://docs.magento.com/user-guide/catalog/settings-advanced-custom-options.html

0

Try below code :- It can be helpful for you to update price in cart.

Obviously, you create your action(Controller) class for booking button so add below code in your controller.

<?php

namespace Vender\Module\Controller;

class ActionController extends \Magento\Framework\App\Action\Action {

    /**
     * @var \Magento\Checkout\Model\Cart
     */
    protected $cart;

    /**
     * @var \Magento\Framework\Data\Form\FormKey
     */
    protected $formKey;

    /**
     * @var \Magento\Catalog\Model\Product
     */
    protected $product;

    /**
     * @var \Magento\Framework\App\RequestInterface
     */
    protected $request;

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @var \Magento\Framework\UrlInterface
     */
    private $urlInterface;

    /**
     * 
     * @param \Magento\Framework\App\Helper\Context $context
     * @param \Magento\Checkout\Model\Cart $cart
     * @param \Magento\Framework\Data\Form\FormKey $formKey
     * @param \Magento\Catalog\Model\Product $product
     * @param \Magento\Framework\App\RequestInterface $request
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     * @param \Magento\Framework\UrlInterface $urlInterface
     */
    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Framework\Data\Form\FormKey $formKey,
        \Magento\Catalog\Model\Product $product,
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Framework\UrlInterface $urlInterface
    ) {
        parent::__construct($context);
        $this->cart = $cart;
        $this->formKey = $formKey;
        $this->product = $product;
        $this->request = $request;
        $this->resultPageFactory = $resultPageFactory;
        $this->urlInterface = $urlInterface;
    }

    /**
     * Execute view action
     *
     * @return ResultInterface
     */
    public function execute() {
        $resultRedirect = $this->resultRedirectFactory->create();
        $params = $this->request->getParams(); // send product_id in request and here get it
        if (isset($params['product_id']) && $params['product_id']) {
            $customPrice = 1000;
            $params = [
                         'form_key' => $this->formKey->getFormKey(),
                         'product' => $params['product_id'],             
                         'qty'   => $params['qty'] // 1,2,3
                 ];
        
            /* Load the product based on productID */  
            $_product = $this->product->load($params['product_id']);
            $this->cart->addProduct($_product, $params);
            /* get quote items */
            $items = $this->cart->getQuote()->getAllItems();
            foreach($items as $item) {
                if ($item->getProductId() && $item->getProductId() == $params['product_id']) {
                    $item->setCustomPrice(round($customPrice,2));
                    $item->setOriginalCustomPrice(round($customPrice,2));
                    $item->getProduct()->setIsSuperMode(true);
                }
            }
            $this->cart->save();
    
            $resultRedirect->setPath($this->urlInterface->getUrl('checkout/cart', array('_secure' => true)));
            return $resultRedirect;
        }
    }
}

Thank You!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.