0

我正在使用 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();
4

2 回答 2

0

当您提到 V2 API 时,我对您的问题感到有些困惑,但是您的代码示例都没有包含 SOAP 请求。

也就是说,我的第一个猜测是您没有使用该cart_store_id字段为 API 请求设置商店 ID。

如果这没有显示任何内容,我将通过 API 创建报价,然后通过本机 PHP,然后在数据库中比较结果以查看 API 创建报价/订单的本机 PHP 报价/订单有哪些额外字段没有

此外,跟踪 API 请求

app/code/core/Mage/Checkout/Model/Cart/Api.php
app/code/core/Mage/Checkout/Model/Cart/Api/V2.php

查看可能缺少的呼叫也可能是一个有用的练习。

于 2013-10-12T16:35:05.347 回答
0

我有这个问题,但可能以不同的方式。

在 SOAP V2 Api-Call 之后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()电话之后没有重新计算报价总额。

于 2020-11-25T10:56:20.597 回答