0

I need to create a static block and place the block in view.phtml.

Static block should collect and display 5 random products from the Manufacturer.

Is it possible? if yes anybody give me the code?

0

Create static block using below code:

 {{block type="core/template" name="random_manufacture_products" template="yourtemplatepath/random.phtml"}}

create template file name with random.phtml add below code:

<?php        
    $manufacturer = Mage::registry('current_product')->getManufacturer();

    $productCollection = Mage::getModel('catalog/product')->getCollection()
                         ->addAttributeToSelect('*')
                         ->addAttributeToFilter('manufacturer',$manufacturer);
    $productCollection->getSelect()->order('RAND()');
    $productCollection->limit(5);

    foreach ($productCollection as $product) {
       echo $product->getName();
    }

add below code in view.phtml

<?php echo $this->getLayout()->createBlock("cms/block")->setBlockId("your-static-block-id")->toHtml();?>
  • I have to use merchant_name instead of Manufacturer. shall I use $manufacturer = $this->getProduct()->getmerchant_name(); ? – Doodu Mar 30 '16 at 12:40
  • You can use getMerchantName() – Prashant Valanda Mar 30 '16 at 12:42
  • Not working... do I need to edit anything here "->addAttributeToFilter('manufacturer',$manufacturer);" ? – Doodu Mar 30 '16 at 12:49
  • Yes change it ->addAttributeToFilter('merchant_name',$manufacturer) – Prashant Valanda Mar 30 '16 at 12:51
  • It's just blank in frontend – Doodu Mar 30 '16 at 13:08
0

Step 1 : Create static block Cms->Static Blocks set it's identifier = relatedproduct and put below code and save it.

{{block type="core/template" name="relatedproduct" template="template_folder/relatedproduct.phtml"}}

Step 2: In relatedproduct.phtml paste below code and save it.

<?php  
$currentProduct = Mage::registry('current_product');
$currentManufacturer = $currentProduct->getManufacturer();
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToFilter('status', array("eq" => 1));
$products->addAttributeToFilter('manufacturer', array("eq" => $currentManufacturer));
$products->addAttributeToFilter('visibility', array("eq" => 4));
$products->getSelect()->order('RAND()');
$products->getSelect()->limit(5);
foreach ($products as $product)  {
 echo $product->getName();
}
?>

Step 3: Now call your static block in your catalog/product/view.phtml using this code:

<?php echo $this->getLayout()->createBlock("cms/block")->setBlockId("relatedproduct")->toHtml();?>
  • Parse error: syntax error, unexpected '$products' (T_VARIABLE) in /relatedproduct.phtml on line 6 – Doodu Mar 30 '16 at 13:09
  • Updated the answer there was missing (;) in line – Arunendra Mar 30 '16 at 13:12
  • It's not calling the products... Just remaining blank.. – Doodu Mar 30 '16 at 13:45
  • Please in template relatedproduct.phtml add echo before $currentManufacturer and see your attribute value is showing or not also please share template code here for check – Arunendra Mar 30 '16 at 13:50
  • Can you plz check this page for more detailed description? magento.stackexchange.com/questions/108947/… – Doodu Apr 1 '16 at 11:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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