简体   繁体   English

magento 1.9将产品添加到购物车时无法通过外部网址运行

[英]magento 1.9 add product to cart not working from an external URL

I'm trying to add a product to cart from an external php script. 我正在尝试从外部php脚本中将产品添加到购物车。 following is the code, but it's not adding the product to the cart. 以下是代码,但未将产品添加到购物车中。

require_once '../app/Mage.php';
Mage::init();

$id = '2'; // product id
$qty = '1'; // qty

$_product = Mage::getModel('catalog/product')->load($id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($_product, array('qty' => $qty));
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);

any suggestions will be appreciated. 任何建议将不胜感激。

Thanks 谢谢

Please try the below code 请尝试以下代码

 <?php include '../app/Mage.php'; Mage::app(); // Need for start the session Mage::getSingleton('core/session', array('name' => 'frontend')); try { $product_id = '1'; // Replace id with your product id $qty = '1'; // Replace qty with your qty $product = Mage::getModel('catalog/product')->load($product_id); $cart = Mage::getSingleton('checkout/cart'); $cart->init(); $cart->addProduct($product, array('qty' => $qty)); $cart->save(); Mage::getSingleton('checkout/session')->setCartWasUpdated(true); Mage::getSingleton('core/session')->addSuccess('Product added successfully'); header('Location: ' . '../index.php/checkout/cart/'); } catch (Exception $e) { echo $e->getMessage(); } ?> 

All so make sure that you have product with id 1 exists and also enough quantity. 因此,请确保您拥有ID为1的产品并且数量足够。

I had try above code it work perfectly for me. 我已经尝试过上面的代码,它对我来说非常合适。

My folder structure is magento/test/test.php 我的文件夹结构是magento / test / test.php

Let me know if you have any query. 让我知道您是否有任何疑问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Magento 1.9通过网址将可配置产品添加到购物车 - Magento 1.9 add configurable product to cart via url Magento 1.9添加参数添加到购物车网址 - Magento 1.9 add parameter to add to cart url Magento 1.9-通过购物车中的交叉销售将可配置产品直接添加到购物车 - Magento 1.9 - add a configurable product directly to cart from cross-sells in shopping cart Magento:通过外部站点的ajax请求将产品添加到购物车 - Magento: add product to cart by ajax request from external site 如何从magento中的外部php文件在购物车中添加产品? - how to add a product in cart from external php file in magento? Magento从外部文件将产品添加到购物车不起作用 - Magento add product to cart from a external file doesn't work Magento 1.9 - 如何在添加到购物车时获得最终产品价格? - Magento 1.9 - How to get final product price on add to cart? 将产品添加到magento之外的购物车(外部脚本) - Add product to cart outside of magento (external script) Magento-如何添加诸如“添加到购物车”之类的额外按钮,以及如何从网格视图,列表视图和产品页面视图重定向到外部URL。 - Magento - How to add extra button like “Add to cart” and Redirect to external url from grid view, list view and product page view. Magento 1.9购物车在IE中不起作用 - Magento 1.9 cart not working in IE
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM