Search code examples
magentodynamic-attributes

Magento - Dynamic Product Attribute Only Pulling Data from Single CMS Block


I have set up 2 extra tabs on my individual product pages which should be pulling information from a different CMS Block for each product. However, when I view the product pages they are all pulling the exact same information from 1 of the CMS Blocks I've created and not from the CMS Block for that particular product.

Here's a few more things I noticed while trying to troubleshoot:

1) If I refresh the Magento cache and then go to a product page the correct information is displayed for that page. Then if I navigate to any other product page it always shows the information from the first product I viewed after I refreshed the Magento cache.

2) This seems to be related to product categories as well. I have 2 product categories. Every product in Category 1 will show the same CMS Block's info. Every product in Category 2 will show the same CMS Block's info, but the CMS Block's info for these products are actually different from the products in Category 1, but all the same.

Here's how I've set everything up:

1) Created separate CMS Blocks for each product each with different information.

2) Created a textarea attribute and added it to the correct attribute set. For each product I have entered the ID to the CMS Block for that product. I've double checked and every product has a different ID entered.

3) In app/design/frontend/rwd/default/layout/catalog.xml I have added the following to display the 2 new tabs:

<!-- Features -->
<block type="catalog/product_view_attributes" name="product.features" as="features" template="catalog/product/view/features.phtml">
<action method="addToParentGroup"><group>detailed_info</group></action>
<action method="setTitle" translate="value"><value>Features</value></action>
</block>
<!-- END Features -->

<!-- TECH SPECS -->
<block type="catalog/product_view_attributes" name="product.tech_specs" as="techspecs" template="catalog/product/view/tech_specs.phtml">
<action method="addToParentGroup"><group>detailed_info</group></action>
<action method="setTitle" translate="value"><value>Tech Specs</value></action>
</block>
<!-- END TECH SPECS -->

4) Lastly, I've created the 2 files

app/design/frontend/rwd/default/template/catalog/product/view/features.phtml app/design/frontend/rwd/default/template/catalog/product/view/tech_specs.phtml

Here's the code:

features.phtml

<?php
$_product = $this->getProduct();
$attribute = $_product->getResource()->getAttribute('features');

if ( is_object($attribute) ) {
    $identifier = $_product->getData("features");
}
?>

<?php if ($_sizeBlock = Mage::app()->getLayout()->createBlock('cms/block')->setBlockId($identifier)): ?>
    <div class="std">
        <?php echo $_sizeBlock->toHtml() ?>
    </div>
<?php endif; ?>

tech_specs.phtml

<?php
$_product = $this->getProduct();
$attribute = $_product->getResource()->getAttribute('tech_specs');
if ( is_object($attribute) ) {
    $identifier = $_product->getData("tech_specs");
}
?>

<?php if ($_sizeBlock = Mage::app()->getLayout()->createBlock('cms/block')->setBlockId($identifier)): ?>
    <div class="std">
        <?php echo $_sizeBlock->toHtml() ?>
    </div>
<?php endif; ?>

Anyone have a clue what's going on here?


Solution

  • Figured it out. It's because Magento is caching the CMS Blocks.

    Solution:

    Copy app/code/core/Mage/Cms/Block/Block.php

    To app/code/local/Mage/Cms/Block/ (I had to add the missing folder hierarchy)

    Then edit Block.php

    In protected function _construct()

    Change $this->setCacheLifetime(false);

    To $this->setCacheLifetime(null);

    No more CMS Block caching and the dynamic content is displayed as expected!