Load Category or Product By Attribute (SKU, Name etc) In Magento
piaoling 2011-06-14 15:56:25
Loading a Product/Category by an ID
This is an easy one which we all know how to do, however, I will write it here anyway.
1
2
|
$_category = Mage::getModel( 'catalog/category' )->load(4); $_product = Mage::getModel( 'catalog/product' )->load(12); |
While loading by ID usually is all that’s needed, sometimes you will want to load a product or category by SKU or name, this is where loadByAttribute comes in.
loadByAttribute($attribute, $value)
1
2
3
4
5
6
|
// Load by name $_category = Mage::getModel( 'catalog/category' )->loadByAttribute( 'name' , 'Acoustic Guitars' ); $_product = Mage::getModel( 'catalog/product' )->loadByAttribute( 'name' , 'Cordoba Classic 6-String Guitar' ); // Load by SKU $_product = Mage::getModel( 'catalog/product' )->loadByAttribute( 'sku' , 'cordoba-classic-6-String-guitar' ); |
This will work for all attributes, however, it should be pointed out that if more than one record matches the attribute value, only the first record will be returned. If you want to return multiple records then you need to research collections in Magento.
另外一种方式:
$order = Mage::getModel('sales/order_item')->getCollection()->addAttributeToSelect('*')
->addAttributeToFilter('order_id', $orderId)->getData();
发表评论(评论将通过邮件发给作者):