我正在使用 Magento 社区版 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();