1

I am using Algolia Instant Search for the category to show quick results on the category page the same as on the search result page. I have many categories on the website, and 90% of them missing the meta description. It only shows if I do set them manually from the backend.

I am trying to set the general rule for Magento categories for example

shop your favorite products in {{category}} and get he best price.

Above General rule will apply to all categories that does not have meta description set in magento backend and have no meta description tag in source of category page.

Here is code i am trying to use to see if i can use this code and edit it so it can do what i did explained above.

Event

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="catalog_category_load_after">
    <observer instance="StackExchange\Magento\Observer\Frontend\Catalog\CategoryLoadAfter" 
              name="stackexchange_magento_observer_frontend_catalog_category_load_after"
    />
</event>

Observer

class CategoryLoadAfter implements \Magento\Framework\Event\ObserverInterface {

/**
 * @var \Psr\Log\LoggerInterface
 */
private $logger;

public function __construct(
 \Psr\Log\LoggerInterface $logger
) {

    $this->logger = $logger;
}
/**
 * Execute observer
 *
 * @param \Magento\Framework\Event\Observer $observer
 * @return void
 */
public function execute(
    \Magento\Framework\Event\Observer $observer
) {
    $this->logger->debug(__METHOD__);
    $category = $observer->getEvent()->getCategory();
    $metadesc = "shop your favorite products in".$category."and get he best price.";
    if ($category instanceof \Magento\Catalog\Model\Category) {
        $oldMetaDesc = $category->getMetaDescription();
        $category->setMetaTitle($metadesc);
    }
}

}

1 Answer 1

0

I just managed to do it, here is my code for observer!

public function execute(
\Magento\Framework\Event\Observer $observer
) {
  $category = $observer->getEvent()->getCategory();
  $metaTitle = $category->getName() ." - xxx.club";
  $metaDesc = "Place order for " .$category->getName() . " with us and get huge sales and discount!";
  if ($category instanceof \Magento\Catalog\Model\Category) {
  //$oldMetaTitle = $category->getMetaTitle();
  $oldMetaDesc = $category->getMetaDesc();
  //$category->setMetaTitle($metaTitle);
  $category->setMetaDescription($metaDesc);
 }
}

Your Answer

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

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