Magento通过SOAP V2 API计算总计,但促销规则不适用?

时间:2013-10-12 07:13:35

标签: php magento-1.7

我正在使用Magento Comunity Edition 1.7.0.2。我必须通过Magento SOAP V2 API计算Totals,通过它传递lineitems,送货方式,付款方式,地址和促销(优惠券代码)信息。我需要计算lineitems总计(基本价格,税,扣除,行总数)和Subtoatl,运费,折扣税和总计。我试过这个虽然使用Mage的PHP脚本。我创建了一个引用对象,我得到了运输购物车规则所需的所有计算(使用优惠券/无优惠券)。

但问题在于,当我将该代码移动到Magento自定义SOAP Api模型方法时,它正在计算所有总计,包括Tax但折扣不适用于它。我也尝试过checkout / cart对象但没有效果。我已经阅读了一些Magento Apis不会启动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();

2 个答案:

答案 0 :(得分:0)

当您提到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

看看可能缺少哪些电话可能也是一项有用的练习。

答案 1 :(得分:0)

我遇到了这个问题,但也许是另外一种方式。

在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()调用之后,没有重新计算报价总额。