简体   繁体   English

从 Magento 1.9 中的产品获取自定义属性

[英]Get custom attribute from product in Magento 1.9

I'm trying to fetch some product data based on the products SKU.我正在尝试根据产品 SKU 获取一些产品数据。 This is working, but I also need to get a custom added attribute at the same time, named 'sku_supplier'.这是有效的,但我还需要同时获得一个自定义添加的属性,名为“sku_supplier”。 Is this possible?这可能吗?

This is what I got:这是我得到的:

require $_SERVER['DOCUMENT_ROOT'] . "/app/Mage.php";
Mage::app();

$sku = '748547';

$products = Mage::getResourceModel('catalog/product_collection');
$products->addAttributeToSelect('*');
$products->addAttributeToFilter('visibility', array('neq' => 1));
$products->addAttributeToFilter('status', 1);
$products->addAttributeToFilter('sku', $sku);
$products->setCurPage(1)->setPageSize(1);
$products->load();

if ($products->getFirstItem()) {
    $product       = $products->getFirstItem();
    $strProdName   = $product->getName();
    $strProdSku    = $product->getSku();
    $strProdSkuSup = $product->getSku_Supplier(); // <= I want to show this
}else{
    $addError          = 'true';
    $addErrorMessage[] = 'Error...';    
}

Thanks in advance,提前致谢,

Just remove the underscore:只需删除下划线:

$strProdSkuSup = $product->getSkuSupplier();  
$strProdSkuSup = $product->getData('sku_supplier'); //alternative 

Magento translates snake_case into camelCase when you want to use the magic getters;当你想使用魔法 getter 时,Magento 将 snake_case 转换为 camelCase; ie. IE。 an attribute with the attribute code cool_custom_attribute would translate into coolCustomAttribute , ie $product->getCoolCustomAttribute() .属性代码为cool_custom_attribute的属性将转换为coolCustomAttribute ,即$product->getCoolCustomAttribute()

Edit:编辑:

You might need to load a product model as sometimes I've experienced that not all custom attributes are attached when you pull it out of a collection (intended for performance reasons I guess).可能需要加载产品模型,因为有时我遇到过,当您将其从集合中拉出时,并非所有自定义属性都会附加(我猜是出于性能原因)。 Something like:就像是:

$_product = Mage::getModel('catalog/product')->load($product->getId());
$strProdSkuSup = $_product->getSkuSupplier();  

Also, did you know that there's a dedicated StackExchange site for Magento ?另外,您是否知道Magento有一个专门的 StackExchange 站点

Use this用这个

$strProdSkuSup = $product->getSkuSupplier();

Instead of代替

$strProdSkuSup = $product->getSku_Supplier(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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