Show product attributes in list.phtml - Magento

2020-02-29 06:24发布

Hello I have read many posts about this, and while it works its not complete.

For example; Attribute 1= shoesize and attribute 2 = shoe color. Both are in a dropdown and I would like to list all of the possible attribute colors per product within the category pages.

Problem: When I test the code it will only display the first shoe color, instead of all posibilites. What am I doing wrong here?

Here are 3 examples of what I have. All code work, but only shows the first attribute color. Example 1:

<!-- Find the following loop -->
<?php foreach ($_productCollection as $_product): ?>
<!-- Inside it insert one of the following codes as needed -->
<!-- Use this for regular text attributes -->
<?php echo $_product->getMyAttribute() ?>
<?php echo $_product->getAnotherCustomAttribute() ?>

<!-- Use this for dropdown attributes -->
<?php echo $_product->getAttributeText('shoecolor') ?>
<?php endforeach?>
<!-- ... --> 

Example 2

<?php echo $_product->getResource()->getAttribute('shoecolor')->getFrontend()->getValue($_product) ?>

Example 3

<?php $type = "simple"; $p = "0" ?> 
<?php foreach ($_productCollection as $_product): ?> 
<?php $custom = Mage::getModel('catalog/product_type_configurable')->setProduct($_product); ?> 
<?php $col = $custom->getUsedProductCollection()->addAttributeToSelect('shoecolor')->addFilterByRequiredOptions(); ?> 
<?php foreach($col as $simple_product) { $p=$simple_product->getId(); $type="configurable"; } ?> 

<?php if($type == "configurable"): ?> 
<h5><?php echo $_product->load($p)->getAttributeText('shoecolor'); ?><?php $type="simple" ?></h5> 
 <?php endif; ?> 

4条回答
Ridiculous、
2楼-- · 2020-02-29 06:47

Here is the code get attribute name and value that that doesn't belongs to any product

$attributeCode = 'YOUR_ATTRIBUTE_CODE';

$product = Mage::getModel('catalog/product');

$productCollection = Mage::getResourceModel('eav/entity_attribute_collection')
   ->setEntityTypeFilter($product->getResource()->getTypeId())
   ->addFieldToFilter('attribute_code', $attributeCode);

$attribute = $productCollection->getFirstItem()->setEntity($product->getResource());
print_r($attribute->getData()); // print out the available attributes

$options = $attribute->getSource()->getAllOptions(false);
print_r($options); // print out attribute options
查看更多
Animai°情兽
3楼-- · 2020-02-29 07:03

you can just config it in attribute edit page

Used in Product Listing -> Yes

查看更多
一夜七次
4楼-- · 2020-02-29 07:06

Try this:

$_pp2 = Mage::getModel('catalog/product')->load(    $_product->getId()    );
echo $_pp2->getdm();

in:

 <?php $i=0; foreach ($_productCollection as $_product): ?>
        <?php if ($i++%$_columnCount==0): ?>

Attribute Code:dm

Type: Text area

in view.phtml

echo $_product->get>getdm(); ?>
查看更多
迷人小祖宗
5楼-- · 2020-02-29 07:10

Another way

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

If you create an attribute like yours "shoesize" then you can access by following code.

If your attribute type Text Field ( $_product should loaded ) :

<?php
  echo $_product->getShoesize();
  // if your arribute was shoe_size then
  echo $_product->getShoeSize();
?>

If your attribute type Multiple Select or Dropdown, to get all attribute value :

<?php
   echo $_product->getAttributeText('shoesize');
?>
查看更多
登录 后发表回答