Magento - Add a product to the cart via query string without form_key parameter

后端 未结 7 1969
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 06:08

I\'ve just installed Magento Community Edition ver 1.8.0.0 (default settings).

System -> Configuration -> Sales -> Checkout -> Checkout Options

相关标签:
7条回答
  • 2020-12-09 06:09

    What I actually did in the end was compare the new changes with the old code and I discovered the add to cart button was set to type="button" and was not submitting.

    Changing the button to simply type="submit" and no other changes actually made it work this is in [theme]/template/catalog/product/view/addtocart

    may have to move from base folder if non-existent

    0 讨论(0)
  • 2020-12-09 06:16

    This is how I am doing it in Magento 1.8.1

    <a href="<?= $this->getAddtoCartUrl($_product, array('qty' => $_price['price_qty'])) ?>">

    See http://docs.magentocommerce.com/Mage_Catalog/Mage_Catalog_Block_Product_Abstract.html#getAddToCartUrl

    0 讨论(0)
  • 2020-12-09 06:17

    Use "Add To Cart" link for your product any where on a Magento website::

    The following code may helpful:

    $product = Mage::getModel('catalog/product')->load($YourProductID);
    
    echo Mage::helper('checkout/cart')->getAddUrl($product);
    

    From magento 1.8 need to add form key $formKey = Mage::getSingleton('core/session')->getFormKey();?> to url

    0 讨论(0)
  • 2020-12-09 06:18

    This works best for me in Magento C 1.8

    <?php
     
    $formKey = Mage::getSingleton('core/session')->getFormKey();?>
    
    <form action="/checkout/cart/add/product/<?php echo $productid; ?>" method="post">
        <input type="hidden" name="form_key" value="<?php echo $formKey; ?>" />
    
        <input type="text" name="qty"> QTY
    
        <input type="submit" value="Add to basket" />
    </form>
    
    0 讨论(0)
  • 2020-12-09 06:22

    What I did is to override the Magento CartController with a custom module. I created a file inside: \app\code\local\Namespace\AddProductFromUrl\controllers\Checkout\CartController.php

    <?php
        require_once 'Mage/Checkout/controllers/CartController.php';
        class Namespace_AddProductFromUrl_Checkout_CartController extends Mage_Checkout_CartController {
            # overloaded addAction
            public function addAction() {        
                // generate form_key if missing or invalid
                if (!($formKey = $this->getRequest()->getParam('form_key', null)) || $formKey != Mage::getSingleton('core/session')->getFormKey()) {
                    $this->getRequest()->setParams(array('form_key' =>Mage::getSingleton('core/session')->getFormKey()));
                }        
    
                // do parent actions
                parent::addAction();
            }
        }
    ?>
    

    (The module need to have config.xml and enabled under etc/modules/ as for every magento custom module. Change Namespace with the one you use.)

    That works perfectly just like magento 1.7 and with minimal impact; the form_key is generated if missing and that's it.

    0 讨论(0)
  • 2020-12-09 06:25

    The following can be used with qty set:

    $product = Mage::getModel('catalog/product')->load($getProductID);
    
    echo Mage::helper('checkout/cart')->getAddUrl($product, array('qty'=>$getQty));
    
    0 讨论(0)
提交回复
热议问题