简体   繁体   中英

Magento change attribute inside order product row

I want to change the default SKU attribute (product number) inside the orderview, at the items block, to a custom attribute.

See:

在此处输入图片说明

Currently by default the SKU is displayed, I want to change this into the DPN (my custom created attribute).

The attribute code for this is: dpn.

How can I achieve that?

I edit the file:

/app/code/core/Mage/Adminhtml/Block/Sales/Items/Column/Default.php

From default code:

public function getSku()
{
    /*if ($this->getItem()->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
        return $this->getItem()->getProductOptionByCode('simple_sku');
    }*/
    return $this->getItem()->getSku();
}

To:

public function getSku()
{
    /*if ($this->getItem()->getProductType() == Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE) {
        return $this->getItem()->getProductOptionByCode('simple_sku');
    }*/
    return $this->getItem()->getDpn();
}

But that does not work. When I change the value to return $this->getItem()->getProductId(); Then I get the Product Id, so that does work.

How can I get the data from the attribute?

Order item ( sales/order_item ) is not the same object as Product ( catalog/product ). Instead, order item is more like a snapshot of a product when order was created.

This allows order items to be accessible (from customer order history page or admin panel) and unchanged even after their related product has been deleted or changed.

Magento, first creates Quote item ( sales/quote_item ) by 'snapshotting' product added to cart. Eventually Quote item gets converted to Order item.

In order to complete your task, you need to create new Quote item attribute, and convert the attribute to Order item (optionally, you can further convert it to sales/invoice_item and sales/creditmemo_item ).

To convert attribute form Quote item to Order item, read about Magento <fieldset> config element.

Alternative, less optimal, solution: You can use your order item to fetch product and use product's attribute value on order view page:

public function getSku()
{
   return $this->getItem()->getProduct()->getDpn();
}

Keep in mind that if you change attribute value for your product, value will be change on all order view pages as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM