Get Last Added Product ID of Cart without using checkout session

I am using RESTful APIs of Magento and I want to get the last added product id of in the cart. All of my searches are using the Checkout Session method of Magento which is

$ this->checkoutSession->getLastAddedProductId();

But this is not applicable on my end since I am using the RESTful APIs. Can someone help me if there are any available functionality where I can put a one liner method and class to get this with Quote. My other solution is to fetch by Quote :: getAllVisibleItems() and get the last item’s product id but I believe it would affect the performance.

Thank you for your help folks.

Magento 1.9 add to cart button validation

I am new in Magento field. can you buddy help me to make understand how “add to cart button” validation HTML filed can find and works .? I reached




/catalog/product/view.phtml in this folder what should I analyze to understand about “add to cart” button validation please tell me to step by step with folder and directory name.

Add to cart before Magento 1 – Cancel adding

I am creating an observer to cancel the addition to the cart with add_to_cart_before.

Here is my config.xml


     <controller_action_predispatch>         <observers>            <controller_action_before>               <class>dispatcher/observer</class>               <method>hookToControllerActionPreDispatch</method>            </controller_action_before>         </observers>      </controller_action_predispatch>       <controller_action_postdispatch>         <observers>            <controller_action_after>               <class>dispatcher/observer</class>               <method>hookToControllerActionPostDispatch</method>            </controller_action_after>         </observers>      </controller_action_postdispatch>       <add_to_cart_before>         <observers>            <add_to_cart_before>               <class>dispatcher/observer</class>               <method>hookToAddToCartBefore</method>            </add_to_cart_before>         </observers>      </add_to_cart_before>       <add_to_cart_after>         <observers>            <add_to_cart_after>               <class>dispatcher/observer</class>               <method>hookToAddToCartAfter</method>            </add_to_cart_after>         </observers>      </add_to_cart_after>    </events> 

Here is my Observer.php

class Module_Stockespagne_Model_Observer {  public function hookToControllerActionPreDispatch($  observer) {     if($  observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add')      {         Mage::dispatchEvent("add_to_cart_before", array('request' => $  observer->getControllerAction()->getRequest()));     } }  public function hookToControllerActionPostDispatch($  observer) {     if($  observer->getEvent()->getControllerAction()->getFullActionName() == 'checkout_cart_add')      {         Mage::dispatchEvent("add_to_cart_after", array('request' => $  observer->getControllerAction()->getRequest()));     } }  public function hookToAddToCartBefore($  observer)  {        $  request = $  observer->getEvent()->getRequest()->getParams();     $  product = Mage::getModel('catalog/product')->load($  request['product']);      if($  product->getSku != "TEST"){         //Display message erroer     } } 


I check some conditions and if they are not validated I want to cancel the addition to the cart and I display an error message.

Thank you!

Apply discount amount on cart line items in Magento 2

I am trying to set discount amount on each cart item in the cart with some condition.

I have followed like this to achieve that.


<config xmlns:xsi="" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Sales:etc/sales.xsd"> <section name="quote">     <group name="totals">         <item name="testdiscount" instance="Vendor\Module\Model\Quote\Discount" sort_order="500"/>     </group> </section> 

then in Vendor\Module\Model\Quote\Discount.php

namespace Vendor\Module\Model\Quote; use Magento\Catalog\Model\ProductRepository; class Discount extends   \Magento\Quote\Model\Quote\Address\Total\AbstractTotal {  protected $  _priceCurrency;  protected $  _productRepository;  protected $  _resource;  public function __construct(    \Magento\Framework\Pricing\PriceCurrencyInterface $  priceCurrency,    \Magento\Framework\App\ResourceConnection $  resource,    ProductRepository $  productRepository   ){      $  this->_priceCurrency = $  priceCurrency;      $  this->_resource = $  resource;      $  this->_productRepository = $  productRepository;     }     public function collect(       \Magento\Quote\Model\Quote $  quote,       \Magento\Quote\Api\Data\ShippingAssignmentInterface $  shippingAssignment,       \Magento\Quote\Model\Quote\Address\Total $  total     )    {    $  data = $  quote->getItems();     if (!count($  data)) {         return $  this;     }    parent::collect($  quote, $  shippingAssignment, $  total);     foreach ($  data as $  item) {             $  productSku = $  item['sku'];             $  checkSkuExist  = $  this->checkProduct($  productSku);             if($  checkSkuExist){               $  item->setDiscountAmount(10);               $  item->setBaseDiscountAmount(10);               $  item->setTaxAmount(10);               $  item->setBaseTaxAmount(10);             }         }         return $  this;        }    public function checkProduct($  productSku){     $  connection = $  this->getConnection();     $  sql = "select * from custom_table where sku='".$  productSku."'";     $  resultProduct = $  connection->query($  sql);     $  resultQuery = $  resultProduct->fetchAll();     if(!empty($  resultQuery)){       $  parent_sku = $  resultQuery[0]['parent_sku'];     return true;    }   }else{   return false;    }    }   public function getConnection(){    $  connection = $  this->_resource->getConnection(\Magento\Framework\App\ResourceConnection::DEFAULT_CONNECTION);   return $  connection;     }  } 

Here When i am check the item SKU against the custom table, if the SKU present in the table then, applying the discount amount for that quote item.

Which is not applying any discount amount for the quote item.

Can anyone suggest me the better way how this can be achieve.


Magento 1.9 Cart Tax Vat

I have a client who wants to show the products without VAT, but the prices are charged with VAT in the administrator. he requires something like this: Price: $ 1000 calculates TAX VAT backwards so then show price: $ 840 tax vat: $ 160 (19%) total: $ 1000

Is this posible only working with magento configuration values? PS: the price in administrator is set in $ 1000 and includes tax vat. The versiĆ³n of magento is 1.9.3 Thank you

Unable to get cart item in custom phtml – Magento 2

I am trying to get cart items info in my module’s phtml file:


Here is my working url:

And this is my code:

<?php     $  cart = $  objectManager->get('\Magento\Checkout\Model\Cart');       // retrieve quote items collection     $  itemsCollection = $  cart->getQuote()->getItemsCollection();      // get array of all items what can be display directly     $  itemsVisible = $  cart->getQuote()->getAllVisibleItems();      // retrieve quote items array     $  items = $  cart->getQuote()->getAllItems();      foreach($  items as $  item) {         echo 'ID: '.$  item->getProductId().'<br />';         echo 'Name: '.$  item->getName().'<br />';         echo 'Sku: '.$  item->getSku().'<br />';         echo 'Quantity: '.$  item->getQty().'<br />';         echo 'Price: '.$  item->getPrice().'<br />';         echo "<br />";                 } ?> 

But I am getting nothing. What wrong I am doing?