Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
Overriding Models
 
Branin
Jr. Member
 
Total Posts:  12
Joined:  2009-07-14
 

I am trying to modify the createOrder() function in app/code/core/Mage/Adminhtml/Model/Sales/Order/Create.php (adding some data to a database table when the order is created).  When I modify the file directly, everything works.  When I remove my changes from the file in core and copy my file (with the changes) to app/code/local/Mage/Adminhtml/Model/Sales/Order/Create.php, everything works fine.

However, I’m trying to do the “correct” thing and only override the createOrder() function, leaving all the other functions in Create.php alone (to ensure easier upgrades in the future and for other reasons).  I believe I’ve set up everything correctly, but it doesn’t seem to be working.  When I create an order, my changes are not fired and I don’t see the name of my module in System - Configuration - Advanced - Advanced.

Can anybody tell me what I’m missing?

app/etc/modules/Bel_Adminhtml.xml

<?xml version="1.0"?>
<config>
     <
modules>
        <
Bel_Adminhtml>
            <
active>true</active>
            <
codePool>local</codePool>
        </
Bel_Adminhtml>
     </
modules>
</
config>

app/code/local/Bel/Adminhtml/etc/config.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
Bel_Adminhtml>
            <
version>0.1.0</version>
        </
Bel_Adminhtml>
    </
modules>
    <global>
        <
models>
            <
adminhtml>
                <
rewrite>
                        <
adminhtml_model_sales_order_create>Bel_Adminhtml_Model_Sales_Order_Create</adminhtml_model_sales_order_create>
                </
rewrite>
            </
adminhtml>
        </
models>
    </global>
</
config>

app/code/local/Bel/Adminhtml/Model/Sales/Order/Create.php

<?php
class Bel_Adminhtml_Model_Sales_Order_Create extends Mage_Adminhtml_Model_Sales_Order_Create
{
    
public function createOrder()
    
{
... full createOrder() function, including my desired changes
}
}

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ambimax
Jr. Member
 
Avatar
Total Posts:  17
Joined:  2008-11-06
Saarbrücken, Germany
 

try

<sales_order_create>Bel_Adminhtml_Model_Sales_Order_Create</sales_order_create>
 
Magento Community Magento Community
Magento Community
Magento Community
 
Branin
Jr. Member
 
Total Posts:  12
Joined:  2009-07-14
 

Thanks for the response.  Unfortunately, that doesn’t seem to work either.  The order is successfully created; my code is just never run (meaning, Magento is using the version of the file in Core and not my version in Local).  Any other ideas?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Sharkbait
Jr. Member
 
Total Posts:  11
Joined:  2009-02-23
 

<adminhtml>
    <
rewrite>
        <
sales_order_create>Bel_Adminhtml_Model_Sales_Order_Create</sales_order_create>
    </
rewrite>
</
adminhtml>

Should work. I have successfully tested a rewrite with the above.

There appears to be a bug in Mage_Adminhtml_Model_Sales_Order_Create.
It’s constructor __construct() does not call the parent (Varien_Object) constructor at any point. Thus the function _construct() which is normally available to be overridden in most Magento classes is not exposed. So if that was the function you are overriding, it isn’t going to be called.

To override the constructor for this class you will have to do something like this:

public function __construct() {
    parent
::__construct();
    
// your code here
}

Hope this helps and works for you.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Branin
Jr. Member
 
Total Posts:  12
Joined:  2009-07-14
 

That is exactly how I have my config.xml file set up, yet it isn’t firing my changes.  The function I’m trying to override is createOrder().  As shown in the initial post, I’ve kept everything the same in the function, except I’ve added the following lines to the end of the function (which works when I add it to the end of the function in the core heirarchy or just copy the entire file into the local/Mage heirarchy):

$belconnection Mage::getSingleton('core/resource')->getConnection('core_write');
$beluser Mage::getSingleton('admin/session')->getUser();
$sql  "insert into beltemp values (?, ?)";
$belconnection->query($sql, array($order->getIncrementId(),$beluser->getUsername()));

Any more ideas, or should I just chalk this up to a bug?  I’m doing all my development and testing on Windows under XAMPP, if that changes anything.

Thank you for all your help.

 
Magento Community Magento Community
Magento Community
Magento Community
 
ambimax
Jr. Member
 
Avatar
Total Posts:  17
Joined:  2008-11-06
Saarbrücken, Germany
 

Try to put a php error into your changed class and when you get a fatal error you’ll see that the file is loaded correctly. That’s what I always do.

If the file is loaded and it ends in an error then please post the complete class Bel_Adminhtml_Model_Sales_Order_Create - it might be a tricky error in there like

public function createOrder()
{
       
return parent::createOrder();
       
$belconnection Mage::getSingleton('core/resource')->getConnection('core_write');
       
// ...
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
Branin
Jr. Member
 
Total Posts:  12
Joined:  2009-07-14
 

I just tried putting garbage in my “replacement” create.php file, but everything continued to work, without error (or my original changes being run), making me think the file just isn’t being called.

Here is the entire replacement create.php file:

<?php
/**
 * @category   Mage
 * @package    Mage_Adminhtml
 * @copyright  Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Order create model
 *
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Bel_Adminhtml_Model_Sales_Order_Create extends Mage_Adminhtml_Model_Sales_Order_Create
{
    
public function createOrder()
    
{
        $this
->_validate();

        if (!
$this->getQuote()->getCustomerIsGuest()) {
            $this
->_putCustomerIntoQuote();
        
}

        $quoteConvert 
Mage::getModel('sales/convert_quote');

        
/* @var $quoteConvert Mage_Sales_Model_Convert_Quote */

        
$quote $this->getQuote();
        if (!
$this->getSession()->getOrder()->getId()) {
            $quote
->reserveOrderId();
        
}

        
if ($this->getQuote()->getIsVirtual()) {
            $order 
$quoteConvert->addressToOrder($quote->getBillingAddress());
        
}
        
else {
            $order 
$quoteConvert->addressToOrder($quote->getShippingAddress());
        
}
        $order
->setBillingAddress($quoteConvert->addressToOrderAddress($quote->getBillingAddress()))
            ->
setPayment($quoteConvert->paymentToOrderPayment($quote->getPayment()));
        if (!
$this->getQuote()->getIsVirtual()) {
            $order
->setShippingAddress($quoteConvert->addressToOrderAddress($quote->getShippingAddress()));
        
}

        
if (!$this->getQuote()->getIsVirtual()) {
            
foreach ($quote->getShippingAddress()->getAllItems() as $item{
                
/* @var $item Mage_Sales_Model_Quote_Item */
                
$orderItem $quoteConvert->itemToOrderItem($item);
                
$options = array();
                if (
$productOptions $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct())) {
                    $productOptions[
'info_buyRequest']['options'$this->_prepareOptionsForRequest($item);
                    
$options $productOptions;
                
}
                
if ($addOptions $item->getOptionByCode('additional_options')) {
                    $options[
'additional_options'unserialize($addOptions->getValue());
                
}
                
if ($options{
                    $orderItem
->setProductOptions($options);
                
}

                
if ($item->getParentItem()) {
                    $orderItem
->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
                
}

                $order
->addItem($orderItem);
            
}
        }
        
if ($this->getQuote()->hasVirtualItems()) {
            
foreach ($quote->getBillingAddress()->getAllItems() as $item{
                
/* @var $item Mage_Sales_Model_Quote_Item */
                
$orderItem $quoteConvert->itemToOrderItem($item);
                
$options = array();
                if (
$productOptions $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct())) {
                    $productOptions[
'info_buyRequest']['options'$this->_prepareOptionsForRequest($item);
                    
$options $productOptions;
                
}
                
if ($addOptions $item->getOptionByCode('additional_options')) {
                    $options[
'additional_options'unserialize($addOptions->getValue());
                
}
                
if ($options{
                    $orderItem
->setProductOptions($options);
                
}

                
if ($item->getParentItem()) {
                    $orderItem
->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
                
}

                $order
->addItem($orderItem);
            
}
        }

        
if ($this->getSendConfirmation()) {
            $order
->setEmailSent(true);
        
}

        
if ($this->getSession()->getOrder()->getId()) {
            $oldOrder 
$this->getSession()->getOrder();

            
$originalId $oldOrder->getOriginalIncrementId() ? $oldOrder->getOriginalIncrementId() : $oldOrder->getIncrementId();
            
$order->setOriginalIncrementId($originalId);
            
$order->setRelationParentId($oldOrder->getId());
            
$order->setRelationParentRealId($oldOrder->getIncrementId());
            
$order->setEditIncrement($oldOrder->getEditIncrement()+1);
            
$order->setIncrementId($originalId.'-'.$order->getEditIncrement());
        
}

        $order
->place();
        
$this->_saveCustomerAfterOrder($order);
        
$order->save();

        if (
$this->getSession()->getOrder()->getId()) {
            $oldOrder 
$this->getSession()->getOrder();

            
$this->getSession()->getOrder()->setRelationChildId($order->getId());
            
$this->getSession()->getOrder()->setRelationChildRealId($order->getIncrementId());
            
$this->getSession()->getOrder()->cancel()
                ->
save();
            
$order->save();
        
}

        
if ($this->getSendConfirmation()) {
            $order
->sendNewOrderEmail();
        
}

        $belconnection 
Mage::getSingleton('core/resource')->getConnection('core_write');
        
$beluser Mage::getSingleton('admin/session')->getUser();
        
$sql  "insert into branintemp values (?, ?)";
        
$belconnection->query($sql, array($order->getIncrementId(),$beluser->getUsername()));
        
        return 
$order;
    
}
}
 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Please post your config.xml file as well.  If you made the class file invalid and everything still ran fine then you have a syntax issue in the config.xml file.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Branin
Jr. Member
 
Total Posts:  12
Joined:  2009-07-14
 

The config.xml file is the exact same as in the initial post of this thread).

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Well, the one you posted at the head of the thread is incorrect.  I’m assuming you made changes after that was pointed out.  If you would like help figuring it out, please post the EXACT config.xml you are trying to use.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

I would also point out, your class file could be much better, assuming your changes are limited the the block added at the end.

<?php
class Bel_Adminhtml_Model_Sales_Order_Create extends Mage_Adminhtml_Model_Sales_Order_Create
{
    
public function createOrder()
    
{
        $order 
parent::createOrder();

        
$belconnection Mage::getSingleton('core/resource')->getConnection('core_write');
        
$beluser Mage::getSingleton('admin/session')->getUser();
        
$sql  "insert into branintemp values (?, ?)";
        
$belconnection->query($sql, array($order->getIncrementId(),$beluser->getUsername()));

        return 
$order;
    
}
}

EDIT:
@Tobias Schifftner : Sorry, missed you making the same suggestion earlier

 
Magento Community Magento Community
Magento Community
Magento Community
 
Branin
Jr. Member
 
Total Posts:  12
Joined:  2009-07-14
 

Good point.  grin

The config.xml file I am currently using is:

<?xml version="1.0"?>

<config>
    <
modules>
        <
Bel_Adminhtml>
            <
version>0.1.0</version>
        </
Bel_Adminhtml>
    </
modules>
    
    <global>
        <
models>
            <
adminhtml>
                <
rewrite>
                        <
sales_order_create>Bel_Adminhtml_Model_Sales_Order_Create</sales_order_create>
                </
rewrite>
            </
adminhtml>
        </
models>
    </global>
</
config>
 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Are you sure your module is even loading? 

Check ‘System > Configuration > Advanced > disable modules output’, is your module listed there?

In either case, also post the full /app/etc/modules/Bel_Adminhtml.xml file please.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Also, please post your Magento version, and what type of OS you are using (UNIX, Win, Mac, etc).  All of this diagnostic information is crucial to solving problems.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Branin
Jr. Member
 
Total Posts:  12
Joined:  2009-07-14
 

Good call on parent::createOrder();.  I’ve modified the file now.

My module is not loading (it does not show up in System - Configuration - Advanced - Disable modules output).

Magento 1.3.2.3 running under XAMPP on Windows (for development).

My Bel_Adminhtml.xml file is as follows:

<?xml version="1.0"?>
<config>
    <
modules>
        <
Bel_Adminhtml>
            <
active>true</active>
            <
codePool>local</codePool>
        </
Bel_Adminhtml>
    </
modules>
</
config>

Thanks.

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Well, for what I see, you module should be loading properly.  I’m assuming you have checked all the file paths and made sure it’s all correct.  What do you log files say?

File list (as I understand it)

/app/code/local/Bel/Adminhtml/Model/Sales/Order/Create.php
/app/code/local/Bel/Adminhtml/etc/config.xml
/app/etc/modules/Bel_Adminhtml.xml

And the file contents:

/app/code/local/Bel/Adminhtml/Model/Sales/Order/Create.php

<?php
class Bel_Adminhtml_Model_Sales_Order_Create extends Mage_Adminhtml_Model_Sales_Order_Create
{
    
public function createOrder()
    
{
        $order 
parent::createOrder();

        
$belconnection Mage::getSingleton('core/resource')->getConnection('core_write');
        
$beluser Mage::getSingleton('admin/session')->getUser();
        
$sql  "insert into branintemp values (?, ?)";
        
$belconnection->query($sql, array($order->getIncrementId(),$beluser->getUsername()));

        return 
$order;
    
}
}

/app/code/local/Bel/Adminhtml/etc/config.xml

<?xml version="1.0"?>

<config>
    <
modules>
        <
Bel_Adminhtml>
            <
version>0.1.0</version>
        </
Bel_Adminhtml>
    </
modules>
    
    <global>
        <
models>
            <
adminhtml>
                <
rewrite>
                        <
sales_order_create>Bel_Adminhtml_Model_Sales_Order_Create</sales_order_create>
                </
rewrite>
            </
adminhtml>
        </
models>
    </global>
</
config>

/app/etc/modules/Bel_Adminhtml.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
Bel_Adminhtml>
            <
active>true</active>
            <
codePool>local</codePool>
        </
Bel_Adminhtml>
    </
modules>
</
config>

Dumb question, have you disabled or refreshed the cache lately?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2