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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27959415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:38:44  来源:igfitidea点击:

Get custom attribute from product in Magento 1.9

phpmagento

提问by Alldo

I'm trying to fetch some product data based on the products SKU. This is working, but I also need to get a custom added attribute at the same time, named 'sku_supplier'. Is this possible?

我正在尝试根据产品 SKU 获取一些产品数据。这是有效的,但我还需要同时获得一个自定义添加的属性,名为“sku_supplier”。这可能吗?

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,

提前致谢,

回答by rdiz

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; ie. an attribute with the attribute code cool_custom_attributewould translate into coolCustomAttribute, i.e. $product->getCoolCustomAttribute().

当你想使用魔法 getter 时,Magento 将 snake_case 转换为 camelCase;IE。具有属性代码的属性cool_custom_attribute将转换为coolCustomAttribute,即$product->getCoolCustomAttribute()

Edit:

编辑:

You mightneed 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 Magneto?

另外,您是否知道Magneto有一个专用的 StackExchange 站点

回答by mjdevloper

Use this

用这个

$strProdSkuSup = $product->getSkuSupplier();

$strProdSkuSup = $product->getSkuSupplier();

Instead of

代替

$strProdSkuSup = $product->getSku_Supplier();