Magento无法从外部文件将产品添加到购物车 [英] Magento add product to cart from a external file doesn't work

查看:79
本文介绍了Magento无法从外部文件将产品添加到购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Magento的addProduct()函数有疑问.我有以下代码:

i have a problem with Magento's addProduct() function. I have the following code:

<?php
// Mage init
include_once '../app/Mage.php'; 
umask(0);  
Mage::init('default');
Mage::getSingleton('core/session', array('name' => 'frontend'));
// Get customer session
$session = Mage::getSingleton('customer/session');     
// Get cart instance
$cart = Mage::getSingleton('checkout/cart'); 
$cart->init();    
// Add a product with custom options
$productId = 11348;
$productInstance = Mage::getModel('catalog/product')->load($productId);
$param = array(
    'product' => $productInstance->getId(),
    'qty' => 1,
    'options' => array(
        528 => '1756',   // Custom option with id: 528
        527 => '1753',   // Custom option with id: 527
        526 => '1751'   // Custom option with id: 526
    )
);
$request = new Varien_Object();
$request->setData($param);
$cart->addProduct($productInstance, $request);    
// update session
$session->setCartWasUpdated(true);    
// save the cart
$cart->save();     
?>

昨天它起作用了,所以include和$ param是很严格的,但是现在不起作用了. 您也可以将此产品添加到商店内的购物车中,以便该产品存在并且有库存. 该代码似乎没有任何错误,但没有将产品添加到购物车中.

It worked yesterday so include and $param are rigth but now it doesn't work. You also can add this product to cart inside the shop so the product exist and it's in stock. This code doesn't seems to have any error but it doesn't add product to cart.

感谢您的帮助.

推荐答案

<?php
require_once('app/Mage.php');    
umask(0);
Mage::app('admin');
$product_model = Mage::getModel('catalog/product');
$my_product_sku = 'test';        
$my_product_id  = $product_model->getIdBySku($my_product_sku);
$my_product     = $product_model->load($my_product_id);
$qty_value = 13;
$cart = Mage::getModel('checkout/cart');
$cart->init();
$cart->addProduct($my_product, array('qty' => $qty_value));
$cart->save();

Mage::getSingleton('checkout/session')->setCartWasUpdated(true); 
?>

这篇关于Magento无法从外部文件将产品添加到购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆