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.

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

报告相同问题?