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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。