#StackBounty: #magento2.3 #addtocart Increase the success/exception message time span in Magento 2

Bounty: 50

When product add to cart the success/exception message is hiding rapidly, So I just tried below way to show/increase the time span of the message. But it’s not worked for me.

I have modified the messages.js file as below from vendor/magento/Module-Ui/view/frontend/web/js/view/messages.js

/**
         * @param {Boolean} isHidden
         */
        onHiddenChange: function (isHidden) {
            var self = this; 
            // Hide message block if needed
             if (isHidden) {
                setTimeout(function () {
                    $(self.selector).hide('blind', {}, 500);
                }, 50000);
            }
        }

The above code is not working for me. Please suggest me increase the time span of the message.

enter image description here


Get this bounty!!!

#StackBounty: #cart #addtocart #shopping-cart-price-rules #magento1.9.2.3 #extrafee Magento 1.9 – How to apply handling fees if selecte…

Bounty: 100

If the cart contains any one of the selected products along with other products that should be applied handling fees and cart contains only the selected products no need to apply handling fees.

Here I am using Extra fees module

Using the shopping cart price rule,

enter image description here

Here how to add a condition like if a customer tries to add other products to the cart but cart have already selected products then should be applied to handling fees.

Now, if more than one special products (all special listed in https://i.stack.imgur.com/MRGD9.png ) exist in cart handling charge applied, i need this for other products.

How to do that? thanks


Get this bounty!!!

#StackBounty: #magento2 #price #addtocart #tax How to set custom tax price during add to cart in Magento 2

Bounty: 50

I am adding product to cart using ajax call.

Here is my controller code.

use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
class AddItems extends MagentoFrameworkAppActionAction
{

 protected $formKey;   
 protected $cart;
 protected $product;
 protected $_resource;
 protected $checkoutSession;
 protected $_taxCalculationService;
 protected $quoteRepository;

public function __construct(
MagentoFrameworkAppActionContext $context,
MagentoFrameworkDataFormFormKey $formKey,
MagentoCheckoutModelCart $cart,
MagentoCatalogModelProductFactory $product,
MagentoFrameworkAppResourceConnection $resource,
MagentoCheckoutModelSession $checkoutSession,
MagentoTaxApiTaxCalculationInterface $taxCalculationService,
MagentoQuoteApiCartRepositoryInterface $quoteRepository,
  array $data = []
) {
   $this->formKey = $formKey;
   $this->cart = $cart;
   $this->product = $product;     
   $this->_resource = $resource;
   $this->checkoutSession = $checkoutSession;  
   $this->_taxCalculationService = $taxCalculationService; 
   $this->quoteRepository = $quoteRepository;
    parent::__construct($context);
 }

 public function execute()
{ 

 try{
   $productId = 10;
    $params = array(
        'form_key' => $this->formKey->getFormKey(),
        'product_id' => $productId, //product Id
        'qty'   =>1 //quantity of product                
    );
    $_product = $this->product->create()->load($productId);       
     $item = $this->cart->getQuote()->getItemByProduct($_product );
    if($item){          
        $quote = $this->cart->getQuote();                   
        $quote->updateItem($item->getId(), array( 'qty' => 1));
        $quote->save();
    }else{                  
        $this->cart->addProduct($_product , $params);
    }
  $customPrice = 1000;
  $grossAmt = 550;

  $productTaxClassId = $child_product->getTaxClassId();
  $customerId = $this->getCustomerId();
  $storeId = $this->_storeManager->getStore()->getId();
  $productRates = $this->_taxCalculationService->getCalculatedRate($productTaxClassId, $customerId, $storeId);

  $taxAmount = ((1 * $grossAmt * $productRates) / 100);

  $quote = $this->cart->getQuote();
  $quoteId = $quote->getId();   
  $productItem = $this->getProductQuote($_product );                
  $productItem->setCustomPrice($customPrice);
  $productItem->setOriginalCustomPrice($customPrice);
  $productItem->getProduct()->setIsSuperMode(true);   
  $this->cart->save(); 
   $productItem->setTaxAmount($taxAmount)->setBaseTaxAmount($taxAmount)->save(); 
    $quote = $this->checkoutSession->getQuote();
    $quote->collectTotals();
    //$quote->save(); 
    $this->quoteRepository->save($quote);
   }
 }

public function getProductQuote($product) {
    $quote = $this->checkoutSession->getQuote();        
    $cartItems = $quote->getItemByProduct($product);        
    return $cartItems;
}
}     

Using above code, i am able to add product to cart with custom price.

I need to add custom tax price also upon adding the product to cart. Right now tax is not getting applied.

How can i set custom tax price for the product added to cart.

Can anyone help me please, custom tax price i have got in variable, $taxAmount

This code save the data to “tax_amount” and “base_tax_amount” fields in quote_item table but when navigated to cart page, tax is showing as 0 and in table values are restored to 0.

$productItem->setTaxAmount($taxAmount)->setBaseTaxAmount($taxAmount)->save(); 
             $quote = $this->checkoutSession->getQuote();
             $quote->collectTotals();
             $this->quoteRepository->save($quote);
            // $quote->save();

Please share the ideas where i am doing wrong here. Thanks in advance


Get this bounty!!!

#StackBounty: #magento2 #addtocart #ajax #query Quote id is empty when inserting multiple records into cart in magento2

Bounty: 50

I am adding product to cart from ajax by passing product id

Here is my controller code.

use MagentoFrameworkControllerResultFactory;
use MagentoFrameworkAppActionAction;
use MagentoFrameworkAppActionContext;
class AddItems extends MagentoFrameworkAppActionAction
{

protected $formKey;   
protected $cart;
protected $product;
protected $_resource;
protected $checkoutSession;

public function __construct(
    MagentoFrameworkAppActionContext $context,
    MagentoFrameworkDataFormFormKey $formKey,
    MagentoCheckoutModelCart $cart,
    MagentoCatalogModelProductFactory $product,
    MagentoFrameworkAppResourceConnection $resource,
    MagentoCheckoutModelSession $checkoutSession,
    array $data = []
) {
    $this->formKey = $formKey;
    $this->cart = $cart;
    $this->product = $product;     
    $this->_resource = $resource;
    $this->checkoutSession = $checkoutSession;   
    parent::__construct($context);
}

public function execute()
{ 
   $selectedItems = $this->getRequest()->getPost('selectedItems');      
    $selectedItems = explode(",",$selectedItems);
    try{
    $connection = $this->getConnection();
    foreach ($selectedItems as $key => $selectedItem) {

        $params = array(
            'form_key' => $this->formKey->getFormKey(),
            'product_id' => $selectedItem, //product Id
            'qty'   =>1 //quantity of product                
        );
        $_product = $this->product->create()->load($selectedItem);       
         $item = $this->cart->getQuote()->getItemByProduct($_product );
        if($item){          
            $quote = $this->cart->getQuote();                   
            $quote->updateItem($item->getId(), array( 'qty' => 1));
            $quote->save();
        }else{                  
            $this->cart->addProduct($_product , $params);
        }
      $customPrice = 1000;
      $quote = $this->cart->getQuote();
      $quoteId = $quote->getId();   
      $productItem = $this->getProductQuote($_product );                
      $productItem->setCustomPrice($customPrice);
      $productItem->setOriginalCustomPrice($customPrice);
      $productItem->getProduct()->setIsSuperMode(true);     
     $insertProductSql = "INSERT INTO custom_table (quote_id, product_id) VALUES ('$quoteId', '$_product->getId()')";
        $connection->query($insertProductSql);
    }
        $this->cart->save();
        $status = 1;
    } catch (MagentoFrameworkExceptionLocalizedException $e) {
        $this->messageManager->addException($e,__('%1', $e->getMessage()));
        $status = 0;
    } catch (Exception $e) {
        $this->messageManager->addException($e, __('error.'));
        $status = 0;
    }
    $result = array();
    $result['status'] = $status;
    $resultJson = $this->resultFactory->create(ResultFactory::TYPE_JSON);
    $resultJson->setData($result);
    return $resultJson;
    }
   public function getConnection()
   {
    $connection = $this->_resource->getConnection(MagentoFrameworkAppResourceConnection::DEFAULT_CONNECTION);
    return $connection;
    }
  public function getProductQuote($product) {
    $quote = $this->checkoutSession->getQuote();        
    $cartItems = $quote->getItemByProduct($product);        
    return $cartItems;
   }
 }

Quote id is not getting retrieved for me, always it is empty.

I need to add both the items to the cart with insert query for custom table,

products are adding to the cart successfully,but quote id storing as null inside my custom table

Note: I am setting custom price for each product.

Can anyone look into this, where i am wrong please?

Any help would be appreciated. Thanks


Get this bounty!!!