doutang9037 2014-07-29 23:23 采纳率: 100%
浏览 23
已采纳

Magento:如何使用属性日期在PHP中显示日期

I've created a attribute-set called pdmset1 and pdmset2 where pdmset1 is a YES/NO attribute and pdmset2 a date.

I echo both by the following php-code

 echo "PDMSET1: " . Mage::getModel('catalog/product')->load($_product->getId())->getAttributeText('pdmset1')
 echo "PDMSET2: Mage::getModel('catalog/product')->load($_product->getId())->getAttributeText('pdmset2')

Output is the following:

>> PDMSET1: YES 
>> PDMSET2: 

So it seems to me PDMSET2 is empty or so. Can somebody explain to me what I do wrong and even better what to do so I get the correct date for PDMSET2

  • 写回答

1条回答 默认 最新

  • du8442 2014-07-30 00:22
    关注

    If your attribute is set up correctly, you should be able to do $product->getData('pdmset2'). The problem with your code is that getAttributeText() method is used for getting attributes that use sources (such as drop-down select box), as seen in Mage_Catalog_Model_Product:

    public function getAttributeText($attributeCode)
        {
            return $this->getResource()
                ->getAttribute($attributeCode)
                    ->getSource()
                        ->getOptionText($this->getData($attributeCode));
        }
    

    Your Yes/ No is a select so getAttributeText works, but for date you should just use getData, your code should look something like this:

    $product = Mage::getModel('catalog/product')->load($_product->getId());
    echo $product->getAttributeText('pdmset1');
    echo $product->getData('pdmset2');   // or $product->getPdmset2();
    

    Also note that loading an object using id is memory consuming, so you should make an effort not to call Mage::getModel('catalog/product')->load($_product->getId()) so many times. If you use it more than once then it's a good idea to assigned the object to a variable and reuse it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用python处理excel的数据(极值标准化)
  • ¥15 三向应力状态求剪应力
  • ¥15 jupyter notebook如何添加libGL.so.1库
  • ¥20 easyPoi能否实现下拉多选或者复选框
  • ¥15 网桥在转发帧时,会变帧的源地址和目的地址吗?
  • ¥15 用Multisim设计汽车尾灯控制电路
  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析