Magento - Add a product to the cart via query stri

2020-02-04 06:22发布

问题:

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

System -> Configuration -> Sales -> Checkout -> Checkout Options
Enable Onepage Checkout: Yes
Allow Guest Checkout: Yes

I'm trying to add a product to the cart using query string method.
According to all resources I've found, these are correct ways to do it:

[store]/checkout/cart/add/product/1/
[store]/checkout/cart/add?product=1&qty=1

but they're not working... the cart remains empty.

After quite some time, I found the working solution:

[store]/checkout/cart/add/product/1/form_key/yu6b5VEzwSU2V7YE/

However, I'd like not to put form_key parameter in the url.
This security feature is not needed in my case.

For example product comparison works fine without the form_key:

[store]/catalog/product_compare/add/product/1/

The idea is to put a static link on some other websites (so dynamically generated form_key is not known), so if a customer clicks on it he is redirected to the store with a filled cart straight away.

Is it possible to get rid of form_key parameter and still be able to add a product to the cart? And if so, then how?

回答1:

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.



回答2:

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>


回答3:

This step is not very complicated! Hope this help.

//Namespace need to change with your namespace
//AddProduct need to change with your module name
class Namespace_AddProduct_AddController extends Mage_Core_Controller_Front_Action {
    public function indexAction() {
            $product_id = $this->getRequest()->getParam('products');
            $qty = $this->getRequest()->getParam('qty');  //used if your qty is not hard coded
            $cart = Mage::getModel('checkout/cart');
            $cart->init();
            if ($product_id == '') {
                continue;
            }
            $productModel = Mage::getModel('catalog/product')->load($product_id);

            //I added only Virtual product here. If no need, remove this condtion
            if ($productModel->getTypeId() == Mage_Catalog_Model_Product_Type::TYPE_VIRTUAL) {
                try
                {
                   $cart->addProduct($productModel, array('qty' => '1'));  //qty is hard coded
                }
                catch (Exception $e) {
                   continue;
                }
            }
            $cart->save();
            if ($this->getRequest()->isXmlHttpRequest()) {
               exit('1');
            }
             $this->_redirect('checkout/cart');
    }
}


回答4:

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



回答5:

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



回答6:

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



回答7:

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));


标签: magento