1

I am trying to override Sales/Model/Order/Api.php file. but it going to core file. and i am getting that sales_order_api in rewrite collection with below script

 <?php
    $folders = array('app/code/local/', 'app/code/community/');//folders to parse
    $configFiles = array();
    foreach ($folders as $folder){
        $files = glob($folder.'*/*/etc/config.xml');//get all config.xml files in the specified folder
        $configFiles = array_merge($configFiles, $files);//merge with the rest of the config files
    }
    $rewrites = array();//list of all rewrites

    foreach ($configFiles as $file){
        $dom = new DOMDocument;
        $dom->loadXML(file_get_contents($file));
        $xpath = new DOMXPath($dom);
            $path = '//rewrite/*';//search for tags named 'rewrite'
            $text = $xpath->query($path);
            foreach ($text as $rewriteElement){
                $type = $rewriteElement->parentNode->parentNode->parentNode->tagName;//what is overwritten (model, block, helper)
                $parent = $rewriteElement->parentNode->parentNode->tagName;//module identifier that is being rewritten (core, catalog, sales, ...)
                $name = $rewriteElement->tagName;//element that is rewritten (layout, product, category, order)
                foreach ($rewriteElement->childNodes as $element){
                    $rewrites[$type][$parent.'/'.$name][] = $element->textContent;//class that rewrites it
                }
            }
    }
    echo "<pre>";print_r($rewrites);
  ?>

Output is

Array
  (
   [models] => Array
    (
        [cataloginventory/stock_item_api_v2] => Array
            (
                [0] => Example_Webservices_Model_Cataloginventory_Api_V2
            )

        [sales/order_api] => Array
            (
                [0] => Example_Webservices_Model_Order_Api
            )

        [core_mysql4/session] => Array
            (
                [0] => Cm_RedisSession_Model_Session
            )

    )

)

The script is:

config.xml:

<?xml version="1.0"?>
<config>
<modules>
    <Example_Webservices>
        <version>0.2.0</version>
    </Example_Webservices>
</modules>

<frontend>
    <routers>
      <connector>
        <use>standard</use>
          <args>
            <module>Example_Webservices</module>
            <frontName>test</frontName>
          </args>
      </connector>
    </routers>
 </frontend>

<global>
    <models>
         <cataloginventory>
             <rewrite>
                 <stock_item_api_v2>Example_Webservices_Model_Cataloginventory_Api_V2</stock_item_api_v2>
             </rewrite>
         </cataloginventory>
        <sales>
            <rewrite>
                <order_api>Example_Webservices_Model_Order_Api</order_api>
            </rewrite>
        </sales>
    </models>
</global>

Example/Webservices/Model/Order/Api.php file:

<?php

  class Example_Webservices_Model_Order_Api extends Mage_Sales_Model_Order_Api {
  protected function _initOrder($orderIncrementId) {
    $order = Mage::getModel('sales/order');

    if(is_string($orderIncrementId)) {
        $order->loadByIncrementId($orderIncrementId);
        if (!$order->getId()) {
            $this->_fault('not_exists');
        }
    } else {
        $order->load($orderIncrementId);
        if (!$order->getId()) {
            $this->_fault('not_exists');
        }
    }
    return $order;
}

public function cancel($orderIncrementId)
{
    Mage::log("Canel Order",null,'cancel-order.log');
    $order = $this->_initOrder($orderIncrementId);

    if (Mage_Sales_Model_Order::STATE_CANCELED == $order->getState()) {
        $this->_fault('status_not_changed');
    }
    try {
        $order->cancel();
        $order->save();
    } catch (Mage_Core_Exception $e) {
        $this->_fault('status_not_changed', $e->getMessage());
    }
    if (Mage_Sales_Model_Order::STATE_CANCELED != $order->getState()) {
        $this->_fault('status_not_changed');
    }
    return true;
  }
}
1

I'm just speculating here but I think you are using the Api V2 version and that's why you end up in the original order api class.
For v2 api the class that is used is Mage_Sales_Model_Order_Api_V2 that extends Mage_Sales_Model_Order_Api.
So your new class is never used. And the V2 class is still extending the old class.

What you need to do is to rewrite the v2 class also.
In your config.xml file, inside the sales->rewrite tag add this also

<order_api_v2>Example_Webservices_Model_Order_Api_V2</order_api_v2>

and create the file Example/Webservices/Model/Order/Api/V2.php with this content

<?php
class Example_Webservices_Model_Order_Api_V2 extends Example_Webservices_Model_Order_Api
{
}

Now you should be covered for both api v1 and v2.

  • Thanks Marius its working but can you please confirm extends class is 't core file – Ramesh Apr 28 '17 at 9:36
  • I don't understand your question. Let me explain again. You initially rewritten the Mage_Sales_Model_Order_Api. This class is used by the Api V1. And your rewrite should work. But for Api V2 an other class is used (that extends Mage_Sales_Model_Order_Api). This means your rewrite will have no effect on V2. You need to rewrite the V2 class also and make it extend the class that rewrites the V1 class in order to make it work for both api versions. – Marius Apr 28 '17 at 9:39
0

Why dont you just copy the file in this location. You dont even need a rewrite.

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

Now you can override any function.

  • Actually i approached that process, one of my client posted its not a standard procedure. thats why i want do in standard way – Ramesh Apr 28 '17 at 9:19
  • can you please explain where i done mistake in above script – Ramesh Apr 28 '17 at 9:20
  • No. Just no. Never do that if what you are trying to change is not an abstract class. – Marius Apr 28 '17 at 9:20
  • @Marius you are connected after long time. can you please explain briefly – Ramesh Apr 28 '17 at 9:23
  • @Marius - is there any explanation for the same? Seems its very much used approach. – Kingshuk Deb Apr 28 '17 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.