1
votes

On a custom page within Magento, I have a simple AJAX Post which passes a product ID to a php script:

jQuery.ajax({
  url: 'https://www.mywebsite.com/test/add_to_basket.php',
  type: "POST",
  data: data,
  success: function (data) {
  ,
  error: function (data) {
  }
});

Here is the add_to_basket php script:

$i = $_POST['i'];

require_once '../app/Mage.php';
umask(0);
Mage::app();

Mage::init('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));  

$session = Mage::getSingleton('customer/session'); 

$cart = Mage::getSingleton('checkout/cart'); 
$cart->init();

$cart->addProduct($i, 1);

$session->setCartWasUpdated(true);

$cart->save(); 

This works perfectly, however the mini cart doesn't update. I've read that I need to create a sections.xml file within etc/frontend like so:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Customer:etc/sections.xsd">
    <action name="[frontName]/[ActionPath]/[ActionName]">
        <section name="cart"/>
    </action>
</config>

However I'm not sure what the [frontName]/[ActionPath]/[ActionName] would be in my example. What is the best course of action?

1

1 Answers

0
votes

the most important thing – ajax.php :

require_once('/var/www/clients/client0/web1/web/app/Mage.php'); // ABSOLUTH PATH TO MAGE
umask(0);
Mage::app ();

Mage::getSingleton('core/session', array('name'=>'frontend'));   // GET THE SESSION

$simbol= Mage::app()->getLocale()->currency(Mage::app()->getStore()->getCurrentCurrencyCode())->getSymbol();  // GET THE CURRENCY SIMBOL
$store=Mage::app()->getStore()->getCode();  // GET THE STORE CODE

$cart = Mage::getSingleton('checkout/cart'); //->getItemsCount();   

$ajtem=$_POST['item'];    // THIS IS THE ITEM ID
$items = $cart->getItems();
foreach ($items as $item) {   // LOOP
   if($item->getId()==$ajtem){  // IS THIS THE ITEM WE ARE CHANGING? IF IT IS:
       $item->setQty($_POST['qty']); // UPDATE ONLY THE QTY, NOTHING ELSE!
       $cart->save();  // SAVE
       Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
       echo '<span>';   
       if($store=='en')  echo $simbol;
       echo number_format($item->getPriceInclTax() * $_POST['qty'],2);
       if($store=='hr')  echo ' '.$simbol;
       echo '</span>';
       break;
   }

}

// THE REST IS updatTotalG FUNCTION WHICH IS CALLED AFTER AJAX IS COMPLETED 
// (UPDATE THE TOTALS)
echo '<script type="text/javascript">';
echo 'function updateTotalG(){';
echo 'jQuery("#sveUkupno").html(\'';
echo '<strong><span>';
//echo 'JQuery(\'#sveUkupno\').html("<strong><span>';
if($store=='en')  echo $simbol;
echo number_format(Mage::getSingleton('checkout/session')->getQuote()->getGrandTotal(),2);
//echo $simbol . ' </span></strong>");';
if($store=='hr')  echo ' '.$simbol;
echo " </span></strong>');";
echo '}   </script>';

You can see that we detect the currency symbol in the script and the store that is in use. At the end of the script it generates updateTotalG script that we use for listing cart quantity value. The value comes from Magento.