0

How do I add some functionality to a existing method?

I created a custom module now and need to add some code to Create.php file. This is the original:

public function initFromOrderItem(Mage_Sales_Model_Order_Item $orderItem, $qty = null)
    {
        if (!$orderItem->getId()) {
            return $this;
        }

        $product = Mage::getModel('catalog/product')
            ->setStoreId($this->getSession()->getStoreId())
            ->load($orderItem->getProductId());

        if ($product->getId()) {
            $product->setSkipCheckRequiredOption(true);
            $buyRequest = $orderItem->getBuyRequest();
            if (is_numeric($qty)) {
                $buyRequest->setQty($qty);
            }
            $item = $this->getQuote()->addProduct($product, $buyRequest);
            if (is_string($item)) {
                return $item;
            }

            if ($additionalOptions = $orderItem->getProductOptionByCode('additional_options')) {
                $item->addOption(new Varien_Object(
                    array(
                        'product' => $item->getProduct(),
                        'code' => 'additional_options',
                        'value' => serialize($additionalOptions)
                    )
                ));
            }

            Mage::dispatchEvent('sales_convert_order_item_to_quote_item', array(
                'order_item' => $orderItem,
                'quote_item' => $item
            ));
            return $item;
        }

        return $this;
    }

All I need to add is:

if (
    !$this->getSession()->getReordered()
    && $orderItem->getOriginalPrice() != $orderItem->getPrice()
) {
    $item->setCustomPrice($orderItem->getPrice());
    $item->setOriginalCustomPrice($orderItem->getPrice());
}

Above the line if ($additionalOptions = $orderItem->getProductOptionByCode(‘additional_options’)) {

How do I do it in my custom module:

<?php

class MyModule_CustomPriceRevert_Model_Sales_Order_Create extends Mage_Adminhtml_Model_Sales_Order_Create {

}
4
  • If your module is configured correctly, you copy the entire function from the Mage/Adminhtml/Model/Sales/Order/Create.php file and insert your if statement where you need it.
    – mtr.web
    Nov 1, 2017 at 18:27
  • Okey, so I paste my updated function in my custom model. What happens if there are a lot more functions in the original Create.php file? Do I have to copy them to my custom model or just this one function that I edited?
    – Greg
    Nov 20, 2017 at 16:18
  • 1
    You only have to copy the function(s) that you want to modify. The rest are pulled in from the parent class extends Mage_Adminhtml_Model_Sales_Order_Create
    – mtr.web
    Nov 20, 2017 at 18:34
  • Great, that's all I wanted to know! If you put that as a answer, I can accept it if you wish :)
    – Greg
    Nov 21, 2017 at 12:42

1 Answer 1

0

As discussed in comments, a properly configured class that extends the one in Mage/Adminhtml/Model/Sales/Order/Create.php will only need the function you wish to modify (in this case initFromOrderItem). So your final file would look something like this:

<?php
class MyModule_CustomPriceRevert_Model_Sales_Order_Create extends Mage_Adminhtml_Model_Sales_Order_Create {

    public function initFromOrderItem(Mage_Sales_Model_Order_Item $orderItem, $qty = null)
    {
        if (!$orderItem->getId()) {
            return $this;
        }

        $product = Mage::getModel('catalog/product')
            ->setStoreId($this->getSession()->getStoreId())
            ->load($orderItem->getProductId());

        if ($product->getId()) {
            $product->setSkipCheckRequiredOption(true);
            $buyRequest = $orderItem->getBuyRequest();
            if (is_numeric($qty)) {
                $buyRequest->setQty($qty);
            }
            $item = $this->getQuote()->addProduct($product, $buyRequest);
            if (is_string($item)) {
                return $item;
            }
// Custom-code
            if (
                !$this->getSession()->getReordered()
                && $orderItem->getOriginalPrice() != $orderItem->getPrice()
            ) {
                $item->setCustomPrice($orderItem->getPrice());
                $item->setOriginalCustomPrice($orderItem->getPrice());
            }
// end Custom-code
            if ($additionalOptions = $orderItem->getProductOptionByCode('additional_options')) {
                $item->addOption(new Varien_Object(
                    array(
                        'product' => $item->getProduct(),
                        'code' => 'additional_options',
                        'value' => serialize($additionalOptions)
                    )
                ));
            }

            Mage::dispatchEvent('sales_convert_order_item_to_quote_item', array(
                'order_item' => $orderItem,
                'quote_item' => $item
            ));
            return $item;
        }

        return $this;
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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