Skip to content
Advertisement

Magento calculating totals through SOAP V2 API but Promotion Rules are not applying?

I’m using Magento Comunity Edition 1.7.0.2. I have to calculate Totals through Magento SOAP V2 API by passing it lineitems, shipping method , payment method, Addresses and Promotions (coupon code) info. I need to calculate the lineitems totals (Base price, Tax, disount , row total) and Subtoatl, shipping, discount Tax and grand total. I have tried this though a PHP Script using Mage. I created a quote object and I got all calculations I need with Shipping cart rules applied (with coupon/without coupon ).

But the issue is that when I moved that code to the Magento custom SOAP Api model method, it is calculating all totals including Tax but Discount is not applying to it. I have also tried checkout/cart object but no effect. I have read some where that Magento Apis dont start the Magento regular PHP session. can it be the case that magento sessions are not working through Api?

Following is the code:

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();

and when i tried to get the Discount and applied rule ids, I got nothing 🙁

 $discount = $item->getDiscountAmount();
$apply_rule_id = $item->getAppliedRuleIds();

Advertisement

Answer

I’m a little confused by your question, as you mention the V2 API, but none of your code samples include a SOAP request.

That said, my first guess would be you’re not setting a store ID for the API request with the cart_store_id field.

If that doesn’t reveal anything I’d create a quote via the API, and then via native PHP, and then diff the results in the database to see what extra fields the native PHP quote/order has that the API created quote/order doesn’t

Also, tracing the API requests in

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

to see what calls may be missing might be a useful exercise as well.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement