可配置产品的子产品属性和库存清单

3

我正在尝试显示与可配置产品相关的简单产品的库存状态列表。 这个功能很好,除了数组的第一个实例未显示相关属性“大小”。

<?php $_helper = $this->helper('catalog/output'); ?>
<?php $_product = $this->getProduct(); ?>
<?php $instock = "Next Day"; ?>
<?php $outofstock = "4 to 7 Days"; ?>
<?php $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product); ?>
<?php $col = $conf->getUsedProductCollection()->addAttributeToSelect('Size')->addFilterByRequiredOptions(); ?>
<ul>
<?php foreach($col as $simple_product){
    $qty = intval(Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple_product)->getQty());   
    $size = Mage::getModel('catalog/product')->load($simple_product->getId())->getAttributeText('Size');
?>
  <li>
      <?php 
      if ( $qty >= 1 ) 
         {echo $qty, " ",$size," ",$instock;} 
      else 
         {echo $qty, " ",$size," ",$outofstock;}  ?>
  </li>
<?php } ?>
</ul>

结果集如下所示:
99 Next Day
99 9 Next Day
99 8.5 Next Day
99 8 Next Day
99 7.5 Next Day
0 7 4 to 7 Days
99 12 Next Day
99 11.5 Next Day
99 11 Next Day
99 10.5 Next Day
99 10 Next Day

有没有什么指导可以帮我找出问题并显示第一个属性?
4个回答

2

解决问题比理解问题的原因要容易得多!但是让我按正确的顺序来解决它:

  1. First of all, both addAttributeToSelect and getAttributeText actually operates with attribute_code, not attribute_value (which I believe you have as size, not Size).

  2. So the first time Magento does $product->getData('Size') inside the getAttributeText, it returns null.

  3. When you pass the Size to getAttributeText function, the size code gets replaced with Size for the particular attribute instance. How is it happening? Very simple: in the deepest layer of the methods chain, Magento makes next request to the DB:

    SELECT `eav_attribute`.* FROM `eav_attribute` WHERE (`eav_attribute`.`attribute_code`='Size') AND (entity_type_id = :entity_type_id);
    

    And if you have collate for your table as case-insensitive (*_ci), it will get the value no matter that actual attribute_code is "size". You can read more about collate in official article and on SO.

  4. So after the first getAttributeText('Size') function every consecutive $product->getData('Size') will return correct value.

现在回到你的脚本。除了大小-Size问题外,你还有一些小问题,但是处理它们将有助于你更好地理解Magento和php:

  1. First of all, it's really a bad practice to use load function inside the loop - especially two times in a row, especially with the product object. If your configurable product has a lot of simples, you'll get unnecessary memory and time losses. It is way better to add all the data you need to your collection, and then use loaded products in the loop. BTW you've already added size to your collection using addAttributeToSelect function. So the correct way to approach your issue would be:

    $col = $conf->getUsedProductCollection()
        ->addAttributeToSelect('size')
        ->joinField(
        'qty',
        'cataloginventory/stock_item',
        'qty',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left'
    )->addFilterByRequiredOptions();
    foreach($col as $simple_product){
        $qty = (int)$simple_product->getQty();
        $size = $simple_product->getAttributeText('size');
        // do your logic
    }
    
  2. And second issue I wanted to mention, since you're in the .phtml template file, it's required from you to follow Magento template standards - using close tags for you loops and cases:

    <?php foreach($col as $simple_product):?>
        <li>
        <?php if ( $qty >= 1 ):?>
              <!-- some html -->
        <?php else:?>
              <!-- some html -->
        <?php endif:?>
        </li>
    <?php endforeach;?>
    

感谢您抽出时间回答问题并解释事情,非常感激。 - Simon Earle
谢谢您提到循环在模板文件中应该使用这种语法。我打开了太多其他开发人员没有这样做并且代码完全未缩进/格式化的模板文件。看到这种情况让我想踩到婴儿身上。 - pspahn

1
这将适用于版本1.7.0.2,您不必自己输入AttributeText,它会自动收集。
<?php if($_product->getTypeId() == "configurable"):
     $ids = $_product->getTypeInstance()->getUsedProductIds();  ?>
     <?php $instock = "Next Day"; ?>
    <?php $outofstock = "4 to 7 Days"; ?>
    <ul>
    <?php foreach ($ids as $id) :
    $simpleproduct = Mage::getModel('catalog/product')->load($id); 
    $name = $simpleproduct->getName();
    $qty = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($simpleproduct)->getQty();?>
    <li><?php echo $simpleproduct->getName()." - ".(int)Mage::getModel('cataloginventory/stock_item')
    ->loadByProduct($simpleproduct)->getQty();?>
     </li>   
     <li><?php 
     if ( $qty >= 1 ) 
             {echo $qty, " ",$name," ",$instock;} 
          else 
             {echo $qty, " ",$name," ",$outofstock;}
             ?>
     </li> 
     <?php endforeach;    ?>
     </ul>
    <?php endif; ?>

0
我想显示条形码属性值。我该如何显示条形码值?谢谢你的帮助。
  <?
$childProducts = Mage::getModel('catalog/product_type_configurable')
                 ->getUsedProducts(null,$product);
$stock_count=0;
$sizeqty="";
foreach($childProducts as $child){
 if($child->getAttributeText('ust_beden_no')!=""){   
 $sizeqty.="<Stok><isim>Beden</isim><Beden>".$child->getAttributeText('ust_beden_no')."</Beden><Miktar>".
(int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($child)->getQty()."



</Miktar></Stok>";
 

}
}
 return $sizeqty;
?>

0
你确定第一个产品上设置了Size属性吗?我已经使用shirt_size作为我的属性测试了你的代码,结果返回如预期。

嗨,感谢抽出时间来检查代码。是的,'Size' 属性肯定已设置,它是必填属性,也是唯一可配置的属性。令人烦恼的是,'Size' 下拉框确实缺少此属性,在本例中为尺码 9.5,因此我真的不知道发生了什么。 - Simon Earle
如果产品是基于导入的或使用批量操作进行更新的,请尝试在产品管理中仅打开该产品并重新保存。有时候值不会保持不变。 - pspahn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接