Question

I want to add a product to the cart on the click of a banner. I referred to the fourth answer on this link - Magento 1.9.0.1 add a product to the cart by URL?

But on following the steps as stated, I am getting this error - The requested URL /checkout/cart/add was not found on this server. Can't understand what's wrong.

Would also like to know if there is any alternative approach to this problem

Was it helpful?

Solution

I just tested this and it works perfectly. All you need are these couple of lines.

<?php $url = $this->helper('checkout/cart')->getAddUrl($_product); ?>
<script>
    setLocation('<?php echo $url; ?>');
</script>

Here is a very basic, yet fully functional example.

  1. Navigave to CMS > Pages in the admin panel and click on your homepage so that the edit screen opens up.
  2. Click on the 'Design' tab, and in the fieldset up top named 'Layout Update XML', add this snippet to it and click save:

    <reference name="before_body_end">
        <block type="core/template" name="custom_banner_js" template="custom/banner/js.phtml"/>
    </reference>
    
  3. Then create this file: app/design/frontend/rwd/astraboot‌/template/custom/banner/js.phtml

  4. In your newly created file, here is the full content to paste into it:

    <?php $_product = Mage::getModel('catalog/product')->load(39); ?>
    <?php $url = $this->helper('checkout/cart')->getAddUrl($_product); ?>
    <script>
        var myImage = $('subscription_product');
        myImage.observe('click',function(){
            setLocation('<?php echo $url; ?>');
        });
    </script>
    
  5. Save your file

  6. Clear your cache

  7. Refresh the page

  8. Test!

Keep in mind, that if you did not remove the <a> tag that was wrapped around the image, this wont work, so just be certain that you removed it.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top