2

Is there a way to set dynamic meta-descriptions by changing the Magento code. For instance:

"Buy PRODUCT_NAME online and save PERCENTANGE% today. Free shipping and secure payments"

In the above case PRODUCT_NAME would be the actual prodcut name. But PERCENTAGE would require some coding as its changed over time. (In my case it would be something like 1-(price/suggested_price). As you understand the percentage would not be the same for all products so it needs to be updated dynamically and this is just for the products.

  • will data also update at backend? – Amit Bera Jan 27 '15 at 13:27
  • Yes, if we change the price for the product for instance. Also we might refer to how much you save by ordering 10 items instad of 1. Ie. 10 cost 10/each and one cost 15 it would be. "Save 33% today". So if any of the prices changes it needs to be reflected in the meta-description. – Andreas Jan 27 '15 at 13:32
  • cannot understand by "10 cost 10/each and one cost 15 it would be. "Save 33% today"." – Amit Bera Jan 27 '15 at 13:55
5

I see 3 ways to tackle this.
Option 1.
Have a cron that runs every night that builds this meta description for each product. This way you don't interfere with the Magento flow for frontend.

The code can look something like this (untested)

$storeViews = Mage::getModel('core/store')->getCollection();
foreach ($storeViews as $store) {
    if ($store->getId() == 0) {
        continue; //skip admin store
    }
    $collection = Mage::getModel('catalog/product')->setStoreId($store->getId())->getCollection()->addAttributeToSelect('*');
    foreach ($collection as $product) {
        $metaDescription = 'Buy '.$product->getName().' online and save '.CUSTOM LOGIC HERE FOR THE PERCENTAGE.'% today. Free shipping and secure payments';
        Mage::getSingleton('catalog/product_action')->updateAttributes(
             array($product->getId()),
             array('meta_description' => $metaDescription),
             $store->getId()
        )
    }
}

This makes sense if your meta descriptions are going to change a lot and if you don't have a lot of products.

Option 2.
Rewrite the product model Mage_Catalog_Model_Product and add a method called getMetaDescription() like this:

public function getMetaDescription() {
    $name = $this->getName();
    $percentage = ...custom logic for percentage
    return Mage::helper('catalog')->__('Buy %s online and save %s % today. Free shipping and secure payments', $name, $percentage);
}

This will generate the meta description on the fly. It makes sense if you don't have a very complicated discount formula. I would choose this one. I wrote it as the second one (in the middle) to see if you are paying attention. :)

Option 3.
Is very similar to the one above, but involves modifying the block that adds the meta description. You should use this in case you don't want to touch the product model.
You have to rewrite the Mage_Catalog_Block_Product_View and in the method _prepareLayout replace $description = $product->getMetaDescription(); with

$name = $product->getName();
$percentage = ...custom logic for percentage
$description = Mage::helper('catalog')->__('Buy %s online and save %s % today. Free shipping and secure payments', $name, $percentage);
  • You are given fast answer :) – Amit Bera Jan 27 '15 at 13:38
  • If I may, I would go for the first one if I were you. The "custom logic for percentage" isn't going to be very lightweight. The less you run on the frontend, the better – Mayers Jan 27 '15 at 13:47
  • @MayersDev Sure. If the custom logic is heavy the first one is better. But if you just have to divide or multiply some numbers that you already have on the product instance it's not the first choice. – Marius Jan 27 '15 at 13:55
  • @Marius, agreed. I misread the original post, I thought some catalog price rules were involved. – Mayers Jan 27 '15 at 14:05
1

Solution that worked for me: simple module

  1. \app\etc\modules\Metadesc.xml

    <?xml version="1.0"?>
    <config>
      <modules>
        <Metadesc_Catalog>
          <active>true</active>
          <codePool>local</codePool>
        </Metadesc_Catalog>
      </modules>
    </config>
    
  2. /app/code/local/Metadesc/Catalog/etc/config.xml

    <?xml version="1.0"?>
    <config>
      <modules>
        <Metadesc_Catalog>
          <version>1.0</version>
        </Metadesc_Catalog>
      </modules>
    
      <global>
        <blocks>
          <catalog>
            <rewrite>
              <product_view>Metadesc_Catalog_Block_Product_View</product_view>
            </rewrite>
          </catalog>
        </blocks>
      </global>
    </config>
    
  3. /app/code/local/Metadesc/Catalog/Block/Product/View.php

    class Metadesc_Catalog_Block_Product_View extends Mage_Catalog_Block_Product_View
    {
        public function _prepareLayout()
        {
            $layout = parent::_prepareLayout();
    
            $headBlock = $this->getLayout()->getBlock('head');
            if ($headBlock) {
                $product = $this->getProduct();
                $description = $product->getMetaDescription();
                if (!$description) {
                    $description = Mage::helper('core/string')->substr(strip_tags($product->getDescription()), 0, 130);
                }
    
                $headBlock->setDescription( ($description) );
            }
    
            return $layout;
        }
    }
    
0

if want do this for frontend then you can easily do this.

Magento is set Meta description on class Mage_Catalog_Block_Product_View on function _prepareLayout.Just rewrite that class and _prepareLayout on this set meta description.

Here the code of rewrite class

<?php
class MyModule_Amit_Block_Catalog__Product_View  Mage_Catalog_Block_Product_View
{
    protected function _prepareLayout()
    {
     $product = $this->getProduct();

        $headBlock = $this->getLayout()->getBlock('head');
    $headBlock->setDescription('YOUR STRING', 0, 255));

     return parent::_prepareLayout();
}

}
  • Thank you. It works. Maybe code can be optimized but. code $description = $product->getMetaDescription(); if ($description) { $_productPrice = $product->getPrice(); $_tierPrices = array(); foreach ($product->getTierPrice() as $tierPrice) { $_tierPrices[] = Mage::helper('core')->currency($tierPrice['website_price'], false, false); } $save = number_format((1-($_tierPrices[0] / $_productPrice))*100, 0) . '%'; $description = str_replace('s%',$save,$description); $headBlock->setDescription($description); } – Andreas Jan 27 '15 at 16:34
0

Thank you. Both #1 (option 3) and #2 helped me out here. Below code solve my problem. I only use one level at tier so that is why I use [0].

Maybe code could be optimized. `code

$description = $product->getMetaDescription(); if ($description) {

            $_productPrice = $product->getPrice();
            $_tierPrices = array();        
            foreach ($product->getTierPrice() as $tierPrice) {
                $_tierPrices[] = Mage::helper('core')->currency($tierPrice['website_price'], false, false);
            }
            $save = number_format((1-($_tierPrices[0] / $_productPrice))*100, 0) . '%'; 
            $description = str_replace('s%',$save,$description);
            $headBlock->setDescription($description);

        } else {
            $headBlock->setDescription(Mage::helper('core/string')->substr($product->getDescription(), 0, 255));
        }

`

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.