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

后端 未结 7 1972
伪装坚强ぢ
伪装坚强ぢ 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:33

    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');
        }
    }
    
    0 讨论(0)
提交回复
热议问题