1

I am trying to create a custom product page layout inside of a CMS page. I have duplicated my view.phtml and called it view2.phtml. On the CMS editor, I have called for:

{{block type="catalog/product_view" product_id="2279" template="catalog/product/view2.phtml"}}

When I view the page, it pulls in the H1 and the description of the product but not the images. It is referencing the same media file that would pull in the flexslider images as the normal view.phtml, which work perfectly.

One would think it should be displaying the product page exactly like its carbon copy, view.phtml -- no?

2 Answers 2

1

If you want to convert a cms page into product view page, simply adding produt.info block directive inside CMS Page will not be sufficient. This is because default product view page has lot of other product page specific blocks and they will not get rendered if you use the directive method.

If you want to get the exact "carbon copy" of product page in a CMS Page, then you need to observer to the event cms_page_render and then do your magic there. ie you need to do this.

<?php
class Namespace_Module_Model_Observer
{
    const CMS_PAGE_IDENTIFIER = 'test-page';
    const PRODUCT_ID = 418;

    public function convertToProductPage(Varien_Event_Observer $observer)
    {
        //getting  event data
        $page = $observer->getEvent()->getPage();
        $action = $observer->getEvent()->getControllerAction();

        //verify the right page and add `catalog_product_view` handle
        if ($page->getIdentifier() == self::CMS_PAGE_IDENTIFIER) {
            $action->getLayout()->getUpdate()
                ->addHandle('catalog_product_view');

            //register product
            $product = Mage::getModel('catalog/product')->load(self::PRODUCT_ID);
            if ($product instanceof Mage_Catalog_Model_Product) {
                Mage::register('product', $product);
                Mage::register('current_product', $product);
            }
        }

        return $this;
    }
}

Here we are doing two things. First it will update layout with hadle catalog_product_view. This will eventually convert a cms page with product page layout. Second thing is we are then registering the required product. Mage::registry('product') and Mage::registry('current_product') need to be defined because, most of the blocks in product page are using those registries.

Note 1 : Here is the complete reference to the source code of this simple extension. Feel free to use it :).

Note 2 : Dont forget to change the value of constant CMS_PAGE_IDENTIFIER and PRODUCT_ID with your cms page identifier and product id.

6
  • Good works rajeev .. i see you github module... Just suggest to Mage_Catalog as Depends module for Rkt_CmsPageToProduct moduke
    – Amit Bera
    Sep 3, 2015 at 5:08
  • @AmitBera Great. I appreciate that and I added that dependency now Sep 3, 2015 at 5:41
  • Thanks Rajeev! Unfortunately, I am still a bit confused (sorry, very new here) Where would I go about place the snippet of code you have given? I tried inputting it inside of my view2.php with the id/identifiers changed, but nothing happened. Is this a module I need to create? Or do I need to update .xml layout somewhere? Do I need to reference it in the actual CMS page or will it automatically load from the cms_page_identifier? Thanks again!
    – sethrr
    Sep 3, 2015 at 15:38
  • @user1509382 if you open the link that i have provided in note-1, u will find a bunch of files. those files together act as a module. Just go and create those files in the locations that i have specified there. finally update observer file with changes i have mentioned in my thread. Sep 3, 2015 at 16:32
  • Okay -- I have created the 3 files listed in your link in the specified locations. I also made the changes to the CMS_PAGE_IDENTIFIER and PRODUCT_ID inside the observer file. Still nothing showing up on the cms page. Should I be calling for it or inputting anything in the CMS-editor?
    – sethrr
    Sep 3, 2015 at 17:27
0

Try this way:

<?php
$productId = $this->getProduct_id(); 
$_product = Mage::getModel('catalog/product')->load($productId); //load the product
?>
<img src="<? echo Mage::helper('catalog/image')->init($_product, 'thumbnail')>resize(75, 75); ?>" alt="<?php echo $this->htmlEscape($_product['name']); ?>" border="0" width="75" />

When you calling it in CMS page:

{{block type="catalog/product_view" product_id="2279" template="catalog/product/view2.phtml"}}

Hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.