0

I have made a new Controller and inside there a new Action in which I want to be able to get the metadescription of every product. My code snippet is the following:

public function metaDescriptionAction()
{
    $collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*');
    foreach ($collection as $product) {
        var_dump($product->getData());
    }
}

But I can't see any information associated with metadescription in the output. What I'm getting is:

array (size=52)
'entity_id' => string '20869' (length=5)
 'type_id' => string 'configurable' (length=12)
 'attribute_set_id' => string '4' (length=1)
 'color' => null
 'color_value' => null
 'cost' => null
 'created_at' => string '2016-05-21 07:58:42' (length=19)
 'gift_message_available' => null
 'has_options' => string '1' (length=1)
 'image_label' => null
 'is_recurring' => null
 'links_exist' => null
 'links_purchased_separately' => null
 'links_title' => null
 'msrp' => null
 'msrp_display_actual_price_type' => null
 'msrp_enabled' => null
 'name' => string 'Jean καμπάνα με patchwork' (length=34)
 'news_from_date' => null
 'news_to_date' => null
 'price' => string '99.9000' (length=7)
 'price_type' => null
 'price_view' => null
 'recurring_profile' => null
 'required_options' => string '1' (length=1)
 'shipment_type' => null
 'short_description' => string 'Jean καμπάνα με patchwork' (length=34)
 'sku' => string '9905202' (length=7)
 'sku_type' => null
 'small_image' => null
 'small_image_label' => null
 'special_from_date' => null
 'special_price' => string '30.0000' (length=7)
 'special_to_date' => null
 'status' => string '1' (length=1)
 'tax_class_id' => string '2' (length=1)
 'thumbnail' => null
 'thumbnail_label' => null
 'updated_at' => string '2017-03-27 10:58:54' (length=19)
 'url_key' => string 'jean-kampana-me-patchwork' (length=25)
 'url_path' => string 'jean-kampana-me-patchwork.html' (length=30)
 'visibility' => string '4' (length=1)
 'weight' => null
 'weight_type' => null
 'size' => string '435' (length=3)
 'size_value' => string 'XL-14' (length=5)
 'barcode' => string '9905202' (length=7)
 'cvg_smartprice' => null
 'season' => string '20' (length=2)
 'season_value' => string 'ΧΕΙΜΩΝΑΣ ' (length=17)
 'is_salable' => string '0' (length=1)
 'stock_item' => 
   object(Varien_Object)[61]
     protected '_data' => 
      array (size=1)
        'is_in_stock' => string '0' (length=1)
    protected '_hasDataChanges' => boolean false
  protected '_origData' => null
  protected '_idFieldName' => null
  protected '_isDeleted' => boolean false
  protected '_oldFieldsMap' => 
    array (size=0)
      empty
  protected '_syncFieldsMap' => 
    array (size=0)
      empty

Any help would be appreciated. Thank you

  • Please check my answer and let me know it helps you. – Nikunj Vadariya Jul 7 '17 at 11:20
  • If you have found an own soultion or an answer here, please post it or accept one to close this. :) – sv3n Jul 12 '17 at 13:47
1
$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect(array('meta_title','meta_description'));

foreach ($collection as $product) { 
    print_r($product->getData()); 
}

output

Array ( [entity_id] => 231 [entity_type_id] => 4 [attribute_set_id] => 13 [type_id] => simple [sku] => msj000 [has_options] => 0 [required_options] => 0 [created_at] => 2013-03-05 10:48:12 [updated_at] => 2017-07-07 11:35:03 [meta_title] => fgdg [meta_description] => dfgfdgf [is_salable] => 1 [stock_item] => Varien_Object Object ( [_data:protected] => Array ( [is_in_stock] => 1 ) [_hasDataChanges:protected] => [_origData:protected] => [_idFieldName:protected] => [_isDeleted:protected] => [_oldFieldsMap:protected] => Array ( ) [_syncFieldsMap:protected] => Array ( ) ) )

  • I'm not getting the [meta-description] .... have I done something wrong in the admin ? – Stefanos Jul 7 '17 at 11:32
  • @Stefanos it is [meta_description] and do the reindexing and make sure updated the cache or disabled even better – Qaisar Satti Jul 7 '17 at 11:36
  • Cleared the cache twice, then reindexed... still, no [meta_description] – Stefanos Jul 7 '17 at 11:44
  • @Stefanos product detail page showing the description? – Qaisar Satti Jul 7 '17 at 11:49
  • What do you mean? – Stefanos Jul 7 '17 at 13:48
0

It should be when you load the product:

$_product = Mage::getModel('catalog/product')->load($product->getId());  

if($_product){
    $title = $_product->getMetaTitle();
    $keyword = $_product->getMetaKeyword();
    $description = $_product->getMetaDescription();
}
  • This seems to work but it takes almost 10 minutes to finish loading.... and I only get two values... I'm wandering if it's valid – Stefanos Jul 7 '17 at 13:27
  • Yes it's because every product is loaded completely, including all the attributes and other data. This could take quite long when you have a large collection, though it would certainly not require 10 minutes... I think @Qaisar Satti's answer is a better solution, in theory. – Niels Jul 7 '17 at 13:37
0

Try like this

<?php $_product = Mage::getModel('catalog/product')->load($productId); ?>
<?php echo $_product->getMetaKeyword(); ?>
<?php echo $_product->getMetaDescription(); ?>
0

I assume you have the flat catalog flag enabled.
This means that the collection of products is retrieved from the flat tables. This means that if you want a certain attribute it needs to be in that table.
Go to Catalog->Attributes->Manage Attributes, look for the meta_description attribute, edit it and set the flag "Used in product listing" to "Yes".
Then rebuild your indexes.
After that your code should work.

  • I can't see any neta_description attribute there... that's weird cause in the meta section I can see it – Stefanos Jul 7 '17 at 13:10
  • It should be there, it's a Magento core attribute. Maybe you have a filter applied? – Niels Jul 7 '17 at 13:39
0

Try adding it in manually:

$collection = Mage::getModel('catalog/product')->getCollection()
    ->addAttributeToSelect('*')
    ->addAttributeToSelect('meta_keyword');

foreach ($collection as $product) {         
    var_dump($product->getData());
}  
  • This return an array but there is no meta_description anywhere... have I done something wrong anywhere in the back end? – Stefanos Jul 7 '17 at 11:45
-1

It turned out that I had to go to Product->Catalog->General and then enable the Visible on catalog option. The the controller is the following:

public function exportCsvAction()
{
    $products = Mage::getModel("catalog/product")->getCollection()->addAttributeToSelect('*');

    $csv = '';
    $_columns = array(
        "Id",
        "Product Name/H1",
        "Meta description",
        "Meta Title",
        "Url"
        );
    $data = array();

    foreach ($_columns as $column) {
        $data[] = '"'.$column.'"';
    }
    $csv .= implode(',', $data)."\n";
    foreach ($products as $product) {
        $loadProduct = Mage::getModel('catalog/product')->load($product->getProductId());

        $data = array();
        $data[] = $product->getId();
        $data[] = $product->getName();
        $data[] = $product->getMetaDescription();
        $data[] = $product->getMetaTitle();
        $data[] = 'mysite/'.$product->getUrlKey();

        $csv .= implode(',', $data)."\n";
    }

    $this->_prepareDownloadResponse('file.csv', $csv, 'text/csv');
}

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.