我正在使用Magento Comunity Edition 1.7.0.2。我必须通过Magento SOAP V2 API计算总计,方法是通过传递行项目,运输方式,付款方式,地址和促销(优惠券代码)信息。我需要计算行项目总计(基本价格、税费、订单、行总计)和 Subtoatl、运费、折扣税和总计。我已经通过使用Mage的PHP脚本尝试过这个。我创建了一个报价对象,并在应用了运输购物车规则(使用优惠券/不带优惠券)的情况下获得了所需的所有计算。
但问题是,当我将该代码移动到Magento自定义SOAP Api模型方法时,它正在计算包括税在内的所有总计,但折扣不适用于它。我也尝试过结帐/购物车对象,但没有效果。我已经读过一些Magento API不启动Magento常规PHP会话的地方。可能是Magento会话无法通过API工作的情况吗?
以下是代码:
public function getTotals($data)
{
$shipping_method = $data->shipment_method;
$payment_method = $data->payment_method;
$total_qty = 0;
$quote = Mage::getModel('sales/quote');
if($data->customer->email!="")
{
$customer_email = $data->customer->email;
$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($customer_email); //load customer by email id
$quote->assignCustomer($customer);
}
foreach($data->lineitems as $p)
{
$product = Mage::getModel('catalog/product');
$product->load($product->getIdBySku($p->SKU));
$qty = $p->QTY;
$buyInfo = array('qty' => $qty);
$total_qty += $p->QTY;
$quote->addProduct($product, new Varien_Object($buyInfo));
}
$billingAddress = array(
'firstname' => $data->billing_address->FirstName,
'lastname' => $data->billing_address->LastName,
'company' => $data->billing_address->CompanyName,
'email' => $data->billing_address->Email,
'street' => array(
$data->billing_address->Address1,
$data->billing_address->Address2
),
'city' => $data->billing_address->City,
'region_id' => $data->billing_address->FirstName,
'region' => $data->billing_address->Region,
'postcode' => $data->billing_address->PostalCode,
'country_id' => $data->billing_address->CountryId,
'telephone' => $data->billing_address->Telephone,
'fax' => $data->billing_address->Fax,
'customer_password' => '',
'confirm_password' => '',
'save_in_address_book' => '0',
'use_for_shipping' => '0',
);
$shippingAddress = array(
'firstname' => $data->shipping_address->FirstName,
'lastname' => $data->shipping_address->LastName,
'company' => $data->shipping_address->CompanyName,
'email' => $data->shipping_address->Email,
'street' => array(
$data->shipping_address->Address1,
$data->shipping_address->Address2
),
'city' => $data->shipping_address->City,
'region_id' => $data->shipping_address->RegionId,
'region' => $data->shipping_address->Region,
'postcode' => $data->shipping_address->PostalCode,
'country_id' => $data->shipping_address->CountryId,
'telephone' => $data->shipping_address->Telephone,
'fax' => $data->shipping_address->Fax,
'customer_password' => '',
'confirm_password' => '',
'save_in_address_book' => '0',
'use_for_shipping' => '0',
);
$quote->getBillingAddress()
->addData($billingAddress);
//$quote->setCouponCode($data->coupons[0]->coupanCode);
$quote->getShippingAddress()
->addData($shippingAddress)
->setShippingMethod($shipping_method)
->setPaymentMethod($payment_method);
Mage::getSingleton('checkout/cart')
->setQuote($quote);
Mage::getSingleton('checkout/cart')
->getQuote()
->getShippingAddress()
->setCollectShippingRates(true);
Mage::getSingleton('checkout/cart')
->getQuote()
->setCouponCode(strlen($data->coupons[0]->coupanCode) ? $data->coupons[0]->coupanCode : '')
->collectTotals()
->save();
$items = Mage::getSingleton('checkout/cart')->getItems();
当我尝试获得折扣并应用规则 ID 时,我什么也没得到:(
$discount = $item->getDiscountAmount();
$apply_rule_id = $item->getAppliedRuleIds();
当你提到 V2 API 时,我对你的问题有点困惑,但你的代码示例都没有包含 SOAP 请求。
也就是说,我的第一个猜测是,您没有使用 cart_store_id
字段为 API 请求设置存储 ID。
如果这没有显示任何内容,我会通过 API 创建一个报价,然后通过本机 PHP,然后在数据库中比较结果以查看本机 PHP 报价/订单具有哪些额外的字段,而 API 创建的报价/订单没有
此外,跟踪 中的 API 请求
app/code/core/Mage/Checkout/Model/Cart/Api.php
app/code/core/Mage/Checkout/Model/Cart/Api/V2.php
查看可能缺少哪些调用也可能是一个有用的练习。
我遇到了这个问题,但可能以不同的方式。
在 SOAP V2 API 调用shoppingCartCouponAdd()
之后,调用shoppingCartTotals()
不包括折扣。
为了解决这个问题,我不得不用以下方法重写Mage_Checkout_Model_Cart_Api::totals()
:
/**
* Overwritten to include cart price rules/discounts in totals call
*
* @param $quoteId
* @param $store
* @return void
*/
public function totals($quoteId, $store = null)
{
$quote = $this->_getQuote($quoteId, $store);
/**
* This line was added compared to the core method.
* If missing, discounts were not calculated until createOrder-call was executed
*/
$quote->collectTotals();
$totals = $quote->getTotals();
$totalsResult = array();
foreach ($totals as $total) {
$totalsResult[] = array(
"title" => $total->getTitle(),
"amount" => $total->getValue()
);
}
return $totalsResult;
}
原因似乎是在常规肥皂shoppingCartCouponAdd()
调用后没有重新计算报价总额。