Enhancing Magento Product Page Titles: Including SKU and Product Name
As an e-commerce developer, crafting effective page titles is crucial for both user experience and search engine optimization (SEO). In Magento, by default, the product details page title only displays the product name. However, you might want to include the SKU number as well to provide more contextual information.
Let’s dive into how to achieve this in your Magento store.
Understanding the Default Page Title Structure
Before making changes, it’s essential to understand the default structure of a Magento product page title. By default, the page title is set based on the config.xml files located under app/code/core/Mage/Catalog/etc/.
The default configuration looks like this:
<page>
<title_replace>
<product>Product Name</product>
</title_replace>
</page>
This means that Magento only includes the product name in the page title.
Modifying the Page Title to Include SKU
To include both the SKU and the product name, you’ll need to override the config.xml file. Here’s a step-by-step guide:
-
Navigate to your theme directory under
app/design/frontend/<your_package>/<your_theme>. -
Create a new folder named
etc, if it doesn’t exist. -
Inside the
etcfolder, create another folder calledmodules. -
In the
modulesfolder, create a new XML file namedMage_Catalog.xml. -
Open the
Mage_Catalog.xmlfile and add the following code:
<config>
<global>
<models>
<catalog>
<rewrite>
<product>LaravelCompany\MagentoProductTitleRewrite\Model\Product</product>
</rewrite>
</catalog>
</models>
</global>
</config>
This code tells Magento to use a custom model for the Mage_Catalog package.
-
Next, create a new folder named
app/code/local/LaravelCompany/MagentoProductTitleRewrite/Model(replace ‘LaravelCompany’ with your actual namespace). -
Inside the
Modelfolder, create a file calledProduct.php. -
Open the
Product.phpfile and add the following code:
<?php
namespace LaravelCompany\MagentoProductTitleRewrite\Model;
class Product extends \Mage_Catalog_Model_Product
{
public function getName()
{
$name = parent::getName();
return $this->getSku() . ' - ' . $name;
}
}
This code overrides the getName method to include both the SKU and the product name.
- Save all files, clear your Magento cache, and refresh your store front-end.
Conclusion: Enhancing SEO with Custom Page Titles
By following these steps, you’ve successfully modified your Magento page titles to include both the product SKU and the product name on the product details page. This approach provides a more comprehensive title that can help improve user navigation and potentially boost your site’s relevance in search engine results.
Remember, while this is a simple example, it demonstrates how you can leverage Magento’s flexibility for customization. For more advanced SEO strategies, consider integrating tools like Yoast SEO or using services provided by Laravel Company to optimize your e-commerce platform.
Don’t forget that small details like this can make a significant difference in the user experience and search engine performance of your Magento store. Keep exploring and optimizing!
Happy coding!