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

时间:2022-11-27 16:37:16

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,

提前致谢,

2 个解决方案

#1


23  

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_attribute would translate into coolCustomAttribute, i.e. $product->getCoolCustomAttribute().

当你想要使用魔法吸气剂时,Magento将snake_case翻译成camelCase;即。属性代码为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 Magneto?

另外,你知道Magneto有专门的StackExchange网站吗?

#2


2  

Use this

$strProdSkuSup = $product->getSkuSupplier();

$ strProdSkuSup = $ product-> getSkuSupplier();

Instead of

$strProdSkuSup = $product->getSku_Supplier(); 

#1


23  

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_attribute would translate into coolCustomAttribute, i.e. $product->getCoolCustomAttribute().

当你想要使用魔法吸气剂时,Magento将snake_case翻译成camelCase;即。属性代码为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 Magneto?

另外,你知道Magneto有专门的StackExchange网站吗?

#2


2  

Use this

$strProdSkuSup = $product->getSkuSupplier();

$ strProdSkuSup = $ product-> getSkuSupplier();

Instead of

$strProdSkuSup = $product->getSku_Supplier();