Get simple product sku and qty using salesOrderInfo of SOAP API in magento
Asked Answered
S

2

3

I have added following code in the

/app/code/core/Mage/Sales/Model/Order/Api.php

File.

       public function info($orderIncrementId)
        {

------
-------
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
    // get order total value
$orderValue = number_format ($order->getGrandTotal(), 2, '.' , $thousands_sep = '');
    // get order item collection
$orderItems = $order->getItemsCollection();

    $skuQtyArray = array();
    foreach ($orderItems as $item)
    {
        $product_id = $item->product_id;
        $product_sku = $item->sku;
        $product_name = $item->getName();
        $product_qty = $item->getQtyOrdered();
        $_product = Mage::getModel('catalog/product')->load($product_id);
        $cats = $_product->getCategoryIds();
        $category_id = $cats[0]; // just grab the first id
        $category = Mage::getModel('catalog/category')->load($category_id);
        $category_name = $category->getName();

        $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $product_sku);  

        $productType=$product->getTypeID();
        if($productType=='simple')
        {                                                                   
                $skuQtVal = $product_sku."=".$product_qty;                                      
                $skuQtyArray[] = $skuQtVal;                                 
        }

    }

$result['simple_product_skus'] = $skuQtyArray;
    Mage::log($skuQtyArray,null,"logTest.txt",true);

return $result; 
}

But when I run following code in the application root

<?php
$client = new SoapClient('localhost/magento/index.php/api/v2_soap/index?wsdl=1');
$session = $client->login('testuser', 'testuser');

$result = $client ->salesOrderInfo($session, '100000026'); 

print_r($result);
?>

I am not getting the changes which I did.

Please suggest some solution.

edited:

My directory structure to override the core code is as following.

I my Overridden Api.php, I am using like this.

class Sigma_Sales_Model_Order_Api extends Mage_Sales_Model_Order_Api

Got it: I need to override like this

class Sigma_Sales_Model_Order_Api_V2 extends Mage_Sales_Model_Order_Api_V2

Because:- Mage_Sales_Model_Order_Api_V2 extends Mage_Sales_Model_Order_Api

Semiyearly answered 6/11, 2013 at 11:46 Comment(1)
hi, did you write on core? and what the value of $result? any warnings or exceptions?Tipsy
C
4

Muk, you have to go to app\code\core\Mage\Sales\etc and modify wsdl.xml and wsi.xml and add the element for sku or whatever you want as per your requirement.

<element name="sku" type="xsd:string" minOccurs="0" /> //in wsdl.xml
<xsd:element name="sku" type="xsd:string" minOccurs="0" /> //wsi.xml

if you don't want to modify core file than you have to override it.

Cecilacecile answered 6/11, 2013 at 12:17 Comment(4)
I am passing the values like $result['custom'] = $skuQtyArray; $skuQtyArray is an array. So do I need to give specific element name in the wsdl.xml and wsi.xml. I am new with theses api please guide.Semiyearly
if you want to make the element as array instead of string than change string to array. I will suggest you to go through with wsdl.xml and wsi.xml. These both files has string, array etc.Cecilacecile
I was asking about element name. Can I give the element name as "custom_simple" ie <element name="custom_simple" type="typens:ArrayOfString" minOccurs="0"/>Semiyearly
Muk, I am just about to leave office. please go through with this link #9201536Cecilacecile
T
1

First correction is on

/app/code/core/Mage/Sales/Model/Order/Api.php

from:

$orderValue = number_format ($order->getGrandTotal(), 2, '.' , $thousands_sep = '');

try to remove the space after number_format

to:

$orderValue = number_format($order->getGrandTotal(), 2, '.' , $thousands_sep = '');

Second is check the Order Increment ID, is it exist?


Third is check on var/log/logTest.txt, what's the value $skuQtyArray?


if you are only add that function, it must be correct now.

My suggestion is do not overwrite core, because Magento is often to upgrade the version, you'll be confused if you want to upgrade it.

Tipsy answered 7/11, 2013 at 2:20 Comment(6)
thank you very much for the help, with your and other comments help I got it.If I override the info function of Api.php , Do I need to write Api.xml for the same? Please suggest.Semiyearly
Nice !! If the method are the same then no need to write api.xml, just override the class. on your custom module. don't forget to clear var/cache.Tipsy
Thank you for your valuable adviceSemiyearly
How can I return $result['custom'] as [0] => stdClass Object ?Semiyearly
it means on your array the value is an class object. try to echo get_class($result['custom'][0]);Tipsy
Pls see my question here for more details #19922523Semiyearly

© 2022 - 2024 — McMap. All rights reserved.