Magento - Indicate on credit memo whether items we

2019-08-25 05:44发布

问题:

I need to customize the Credit Memo page to store whether or not an item was returned to stock.

I've identified the Observer in:

app\code\core\Mage\CatalogInventory\Model\Observer.php
refundOrderInventory()

which gets triggered when the admin submits the credit memo with the 'Return to Stock' checkbox ticked. So I know I can add my own observer to write/save something.

But I cant figure out how to add an extra attribute to the Credit Memo product items.

Could anyone give me some pointers?

UPDATE: I can also add the extra Returned to Stock table cell by editing:

app\design\adminhtml\default\default\template\sales\order\creditmemo\view\items.phtml

and

app\design\adminhtml\default\default\template\sales\order\creditmemo\view\items\renderer\default.phtml

To give me this:

I've hardcoded the "YES" value you see in there. I need to find some way of making this a writeable/readable credit-memo product attribute.

回答1:

You will want to add the attribute and column to your creditmemo item entity, in an install script. Make sure to have your setup class to be Mage_Eav_Model_Entity_Setup as there is no addAttribute() function in Mage_Core_Model_Resource_Setup.

$installer->addAttribute('creditmemo_item', 'returned_to_stock', array('type' => 'int', 'grid' => true, 'source' => 'adminhtml/system_config_source_yesno'));
$installer->getConnection()->addColumn($installer->getTable('sales/creditmemo_item'), 'returned_to_stock', 'TINYINT(1) unsigned DEFAULT 0');

Then, in your observer (please don't modify the observer you listed there), set the value to be true, like so (I just copied the function you listed, and modified it slightly to demonstrate my point):

public function refundOrderInventory($observer)
{
    $creditmemo = $observer->getEvent()->getCreditmemo();
    $items = array();
    foreach ($creditmemo->getAllItems() as $item) {
        $return = false;
        if ($item->hasBackToStock()) {
            if ($item->getBackToStock() && $item->getQty()) {
                $return = true;
            }
        } elseif (Mage::helper('cataloginventory')->isAutoReturnEnabled()) {
            $return = true;
        }
        if ($return) {
            $item->setReturnedToStock(1);
        }
    }
}


标签: magento