1

I have a template which uses banners on the homepage,these banners get filled by a static block like the following:

<div class="col-sm-6">
    <a href="{{store url=''}}women.html">
    <img src="{{skin url='images/banner-3.jpg'}}" alt="" />
    <span class="s-desc">
        <span>
            <h1>Product ABC</h1>
            <h2>$99,99</h2>
            <span class="banners-button">Buy Now</span>
        </span>
    </span>
    </a>
</div>

This is static however as I need to put in the image and write down the product name and so on by hand. What I would like to to from the stativ block is something like this:

<div class="col-sm-6">
    {{block type="core/template" name="product_id" pid="1" template="myfolder/product_id.phtml"}}
</div>

And then have a .phtml file where I get the product ID from the static block as follows:

<?php echo $this->getPid();?>

and then create the input for the banner there, so that I can just change the pid in the static block, because as far as I know I cannot get the image, product price and name of a product and URL in a static block or not?

UPDATE

I also would need to pass on a flag to the template file to differentiate between two types of products and the URL

1 Answer 1

1

Well, if you know those data you can definitely do something like this:

<div class="col-sm-6">
    {{block type="core/template" name="product_id" pid="1" pimage="imageurl" template="myfolder/product_id.phtml"}}
</div>

Then call

<?php echo $this->getPimage(); ?>

But that would be painful and you will have to change several arguments each time you use that block for a different product.

What you can do is get those data directly in your template using the pid:

Either by loading the product (not recommended, loading an entire product to get just a few attributes is very bad in terms of performance):

$_product = Mage::getModel('catalog/product')->load($this->getPid());
$_productImage = Mage::helper('catalog/image')->init($_product, 'image');
$_productName = $_product->getName();
$_productPrice = $_product->getPrice();

Or using a collection (more code but way better in terms of performance):

$_collection = Mage::getResourceModel('catalog/product_collection')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('price')
    ->joinAttribute('image', 'catalog_product/image', 'entity_id', null, 'left')
    ->addAttributeToFilter('entity_id', $this->getPid())
    ->setPageSize(1);

if ($_collection->getSize()) {
    $_product = $_collection->getFirstItem();
    $_productImage = Mage::helper('catalog/image')->init($_product, 'image');
    $_productName = $_product->getName();
    $_productPrice = $_product->getPrice();
}
11
  • So if i would need the image, name and price which would be the best way? At the end I will call this template file 3 times for 3 different banners.
    – Klettseb
    Jun 2, 2016 at 13:22
  • 1
    @Klettseb see my updated code, I would personally go for the collection solution Jun 2, 2016 at 13:24
  • 1
    Ok i will give it a try now ;-) Thanks a lot for the answer, i will go for the collection to keep the performance in mind :-)
    – Klettseb
    Jun 2, 2016 at 13:29
  • Ok product comes up works like a charm so far, two more simple questions, to add the URL, but only the relative one to the store URL, can I use getProductUrl();? And is there a way I can pass another value on to the template file, somehow like a flag to differentiate between two types?
    – Klettseb
    Jun 2, 2016 at 15:14
  • @Klettseb yeah you can pass as many values as you want to the template file ;) if you also need the product URL just add ->addAttributeToSelect('product_url') to the collection ;) Jun 2, 2016 at 15:17

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.