问题描述
|
我正在尝试使用Magento Enterprise 1.10 XML-RPC API来处理Magento安装之外的购物车/目录功能。我遇到的问题是当我添加到购物车时。我可以很好地连接到API端点,登录并检索数据。以下是我用来发现Magento API工作原理的代码。
<?PHP
require $_SERVER[\'DOCUMENT_ROOT\'].\'/Zend/XmlRpc/Client.PHP\';
$url = \'http://mymagento.com/api/xmlrpc\';
$user = \'apiuser\';
$pass = \'apipass\';
$proxy = new Zend_XmlRpc_Client( $url );
$sess = $proxy->call( \'login\',array( $user,$pass ) );
$cartId = $proxy->call( \'call\',array( $sess,\'cart.create\',array( 1 ) ) );
$pList = $proxy->call( \'call\',\'product.list\',array() ) );
$cList = $proxy->call( \'call\',\'customer.list\',array() ) );
$cList[0][\'mode\'] = \'customer\';
$setCart = $proxy->call( \'call\',\'cart_customer.set\',array( $cartId,$cList[0] ) ) );
foreach( $pList as $prod)
{
if( $prod[\'product_id\'] == 5 )
{
$prod[\'qty\'] = 5;
$addCart = $proxy->call( \'call\',\'cart_product.add\',$pAdd ) ) );
}
}
$cList = $proxy->call( \'call\',\'cart.info\',array( $cartId ) ) );
print_r( $cList );
输出:
[store_id] => 1
[created_at] => 2011-05-27 13:30:57
[updated_at] => 2011-05-27 13:31:00
[converted_at] => 0000-00-00 00:00:00
[is_active] => 0
[is_virtual] => 0
[is_multi_shipping] => 0
[items_count] => 1
[items_qty] => 5.0000
[orig_order_id] => 0
[store_to_base_rate] => 1.0000
[store_to_quote_rate] => 1.0000
[base_currency_code] => USD
[store_currency_code] => USD
[quote_currency_code] => USD
[grand_total] => 0.0000
[base_grand_total] => 0.0000
[checkout_method] => customer
...
[items] => Array
(
[0] => Array
(
[item_id] => 93
[quote_id] => 119
[created_at] => 2011-05-27 13:31:00
[updated_at] => 2011-05-27 13:31:00
[product_id] => 5
[store_id] => 1
[parent_item_id] =>
[is_virtual] => 1
[sku] => product1
[name] => product
[description] =>
[applied_rule_ids] =>
[additional_data] =>
[free_shipping] => 0
[is_qty_decimal] => 0
[no_discount] => 0
[weight] =>
[qty] => 5
[price] => 0.0000
[base_price] => 0.0000
[custom_price] =>
[discount_percent] => 0.0000
[discount_amount] => 0.0000
[base_discount_amount] => 0.0000
但是,我只是使用相同的上述会话来调用以下内容
<?PHP
$pInfo = $proxy->call( \'call\',\'catalog_product.info\',\'5\' ) );
print_r( $pInfo );
我得到有关该产品的以下信息:
[product_id] => 5
[sku] => product1
[set] => 9
[type] => virtual
[categories] => Array
(
)
[websites] => Array
(
[0] => 1
)
[type_id] => virtual
[name] => product
[description] => Test
[short_description] => Test
[news_from_date] =>
[old_id] =>
[news_to_date] =>
[status] => 1
[visibility] => 4
...
[created_at] => 2011-05-25 15:11:34
[updated_at] => 2011-05-25 15:11:34
...
[price] => 10.0000
最后,API看到该商品的价格实际上是$ 10.00,但是当通过API添加到购物车时,价格无法正确反映。
解决方法
正好可以成为一个正式回答的问题,这是找到的解决方案,http://magentocommerce.com/boards/viewthread/227044我花了两天时间进行搜索,今天想出一个晦涩的搜索词来尝试查找解决方案
, 我已经研究了这个问题几天了。对我来说,如果您通过普通的Web界面将产品添加到购物车中是没有意义的(例如
Mage_Checkout_CartController::addAction()
)。它无需您提供地址即可知道价格。我终于找到了两者之间的区别。在addAction()
中,他们创建了ѭ6instance的实例,将产品添加到其中,然后保存。在api中,他们改用Mage_Sales_Model_Quote
。如果查看Mage_Checkout_Model_Cart::save()
,您会看到以下两行:
$this->getQuote()->getBillingAddress();
$this->getQuote()->getShippingAddress();
这两行实际上创建了空的Mage_Sales_Model_Quote_Address
对象,这些对象已保存到数据库中。
如果您愿意/能够修改magento的代码,则可以修改ѭ11并在$quote->save()
之前添加对这两个方法的调用,并且api和网络界面将以相同的方式工作。
我只测试了一下,但是我真的认为这是一个错误而不是功能。我将在实际的magento开发人员面前看到它,也许他们会在下一个版本中包含它。
, Magento前端和API都不同。在注册客户后,它会在前端为客户创建报价,并使用创建的报价ID设置地址。但是在API中,它仅使用shoppingCartCreate
服务创建报价。为了适当,我们需要定制创建服务。我做到了,它为我工作。
这里提供解决方案:
在文件中编辑功能-Mage/Checkout/Model/Cart/Api.php
public function create($store = null)
{
$storeId = $this->_getStoreId($store);
try {
/*@var $quote Mage_Sales_Model_Quote*/
$quote = Mage::getModel(\'sales/quote\');
$quote->setStoreId($storeId)
->setIsActive(false)
->setIsMultiShipping(false)
->save();
/* Customized this for saving default address for quote and it will show price in cart info*/
$quote->getBillingAddress();
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->collectTotals();
$quote->save();
/* End cart here */
} catch (Mage_Core_Exception $e) {
$this->_fault(\'create_quote_fault\',$e->getMessage());
}
return (int) $quote->getId();
}