NewsModules_CanonicalUrl - Version 0.1.0

Version Notes

Change the url of the products to improve indexing on search engines.

Download this release

Release Info

Developer Alessandro Lioce
Extension NewsModules_CanonicalUrl
Version 0.1.0
Comparing to
See all releases


Version 0.1.0

app/code/community/NewsModules/CanonicalUrl/Block/Product/View.php ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class NewsModules_CanonicalUrl_Block_Product_View extends Mage_Catalog_Block_Product_View
4
+ {
5
+ /**
6
+ * Add meta information from product to head block
7
+ *
8
+ * @return Mage_Catalog_Block_Product_View
9
+ */
10
+ protected function _prepareLayout()
11
+ {
12
+ $this->getLayout()->createBlock('catalog/breadcrumbs');
13
+ $headBlock = $this->getLayout()->getBlock('head');
14
+ if ($headBlock) {
15
+ $product = $this->getProduct();
16
+ $title = $product->getMetaTitle();
17
+ if ($title) {
18
+ $headBlock->setTitle($title);
19
+ }
20
+ $keyword = $product->getMetaKeyword();
21
+ $currentCategory = Mage::registry('current_category');
22
+ if ($keyword) {
23
+ $headBlock->setKeywords($keyword);
24
+ } elseif ($currentCategory) {
25
+ $headBlock->setKeywords($product->getName());
26
+ }
27
+ $description = $product->getMetaDescription();
28
+ if ($description) {
29
+ $headBlock->setDescription( ($description) );
30
+ } else {
31
+ $headBlock->setDescription(Mage::helper('core/string')->substr($product->getDescription(), 0, 255));
32
+ }
33
+ if ($this->helper('catalog/product')->canUseCanonicalTag()) {
34
+ $params = array(); //array('_ignore_category' => true);
35
+ $headBlock->addLinkRel('canonical', $product->getUrlModel()->getUrl($product, $params));
36
+ }
37
+ }
38
+
39
+ return Mage_Catalog_Block_Product_Abstract::_prepareLayout();
40
+ }
41
+ }
app/code/community/NewsModules/CanonicalUrl/Catalog/Model/Product/Url.php ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ if (version_compare(Mage::getVersion(), '1.8', '>='))
3
+ {
4
+ //version is 1.8 or greater
5
+ /***/
6
+ class NewsModules_CanonicalUrl_Catalog_Model_Product_Url extends Mage_Catalog_Model_Product_Url
7
+ {
8
+ /**
9
+ * Check product category
10
+ *
11
+ * @param Mage_Catalog_Model_Product $product
12
+ * @param array $params
13
+ *
14
+ * @return int|null
15
+ */
16
+ protected function _getCategoryIdForUrl($product, $params)
17
+ {
18
+ if (isset($params['_ignore_category'])) {
19
+ return null;
20
+ } else {
21
+ $category_ids = $product->getCategoryIds();
22
+ foreach ($product->getCategoryIds() as $catalog_id)
23
+ {
24
+ $category = Mage::getModel('catalog/category')->load($catalog_id);
25
+ $parent_ids = $category->getParentIds();
26
+ $category_ids = array_diff($category_ids, $parent_ids);
27
+ }
28
+ return array_shift($category_ids);
29
+
30
+ /*
31
+ return $product->getCategoryId() && !$product->getDoNotUseCategoryId()
32
+ ? $product->getCategoryId() : null;
33
+ */
34
+ }
35
+ }
36
+ }
37
+ /***/
38
+ }
39
+ else
40
+ {
41
+ /***/
42
+ class NewsModules_CanonicalUrl_Catalog_Model_Product_Url extends Mage_Catalog_Model_Product_Url
43
+ {
44
+ /**
45
+ * Retrieve Product URL using UrlDataObject
46
+ *
47
+ * @param Mage_Catalog_Model_Product $product
48
+ * @param array $params
49
+ * @return string
50
+ */
51
+ public function getUrl(Mage_Catalog_Model_Product $product, $params = array())
52
+ {
53
+ //mail('a.lioce@ne-ws.it', Mage::getVersion(), '');
54
+
55
+ $routePath = '';
56
+ $routeParams = $params;
57
+
58
+ $storeId = $product->getStoreId();
59
+ if (isset($params['_ignore_category'])) {
60
+ unset($params['_ignore_category']);
61
+ $categoryId = null;
62
+ } else {
63
+ /*
64
+ $categoryId = $product->getCategoryId() && !$product->getDoNotUseCategoryId()
65
+ ? $product->getCategoryId() : null;
66
+ */
67
+ $category_ids = $product->getCategoryIds();
68
+ foreach ($product->getCategoryIds() as $catalog_id)
69
+ {
70
+ $category = Mage::getModel('catalog/category')->load($catalog_id);
71
+ $parent_ids = $category->getParentIds();
72
+ $category_ids = array_diff($category_ids, $parent_ids);
73
+ }
74
+ $categoryId = array_shift($category_ids);
75
+ }
76
+
77
+ if ($product->hasUrlDataObject()) {
78
+ $requestPath = $product->getUrlDataObject()->getUrlRewrite();
79
+ $routeParams['_store'] = $product->getUrlDataObject()->getStoreId();
80
+ } else {
81
+ $requestPath = $product->getRequestPath();
82
+ if (empty($requestPath) && $requestPath !== false) {
83
+ $idPath = sprintf('product/%d', $product->getEntityId());
84
+ if ($categoryId) {
85
+ $idPath = sprintf('%s/%d', $idPath, $categoryId);
86
+ }
87
+ $rewrite = $this->getUrlRewrite();
88
+ $rewrite->setStoreId($storeId)
89
+ ->loadByIdPath($idPath);
90
+ if ($rewrite->getId()) {
91
+ $requestPath = $rewrite->getRequestPath();
92
+ $product->setRequestPath($requestPath);
93
+ } else {
94
+ $product->setRequestPath(false);
95
+ }
96
+ }
97
+ }
98
+
99
+ if (isset($routeParams['_store'])) {
100
+ $storeId = Mage::app()->getStore($routeParams['_store'])->getId();
101
+ }
102
+
103
+ if ($storeId != Mage::app()->getStore()->getId()) {
104
+ $routeParams['_store_to_url'] = true;
105
+ }
106
+
107
+ if (!empty($requestPath)) {
108
+ $routeParams['_direct'] = $requestPath;
109
+ } else {
110
+ $routePath = 'catalog/product/view';
111
+ $routeParams['id'] = $product->getId();
112
+ $routeParams['s'] = $product->getUrlKey();
113
+ if ($categoryId) {
114
+ $routeParams['category'] = $categoryId;
115
+ }
116
+ }
117
+
118
+ // reset cached URL instance GET query params
119
+ if (!isset($routeParams['_query'])) {
120
+ $routeParams['_query'] = array();
121
+ }
122
+
123
+ return $this->getUrlInstance()->setStore($storeId)
124
+ ->getUrl($routePath, $routeParams);
125
+ }
126
+ }
127
+ /***/
128
+ }
app/code/community/NewsModules/CanonicalUrl/etc/config.xml ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <NewsModules_CanonicalUrl>
5
+ <version>0.1.0</version>
6
+ </NewsModules_CanonicalUrl>
7
+ </modules>
8
+ <global>
9
+ <blocks>
10
+ <catalog>
11
+ <rewrite>
12
+ <product_view>NewsModules_CanonicalUrl_Block_Product_View</product_view>
13
+ </rewrite>
14
+ </catalog>
15
+ </blocks>
16
+ <models>
17
+ <catalog>
18
+ <rewrite>
19
+ <product_url>NewsModules_CanonicalUrl_Catalog_Model_Product_Url</product_url>
20
+ </rewrite>
21
+ </catalog>
22
+ </models>
23
+ </global>
24
+ </config>
app/etc/modules/NewsModules_CanonicalUrl.xml ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <config>
3
+ <modules>
4
+ <NewsModules_CanonicalUrl>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ </NewsModules_CanonicalUrl>
8
+ </modules>
9
+ </config>
package.xml ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>NewsModules_CanonicalUrl</name>
4
+ <version>0.1.0</version>
5
+ <stability>stable</stability>
6
+ <license>GNU General Public License (GPL)</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Change the url of the products to improve indexing on search engines.</summary>
10
+ <description>When Magento generates the url of a product, use the url key of the categories that represent the path to the current category and add the url key of the product followed by .html&#xD;
11
+ &#xD;
12
+ If we are in the category Chancellery and click on the product Pen, Magento will use the url www.domain.com/chancellery/pen.html&#xD;
13
+ &#xD;
14
+ If the product Pen is also visible in Homepage and we click on it, Magento will use the url www.domain.com/pen.html&#xD;
15
+ &#xD;
16
+ If we enable the option&#xD;
17
+ System &gt; Configuration &gt; Catalog &gt; Search Engine Optimizations &gt; Use Canonical Link Meta Tag For Products&#xD;
18
+ putting it to "Yes" in the product page will be added the meta tag for the canonical url which in our example will always correspond to www.domain.com/pen.html&#xD;
19
+ &#xD;
20
+ Our module allows Magento to use the full url with the path of the category for both products in Homepage than the canonical url for improving the indexing on search engines.&#xD;
21
+ The url generated to display a product passing from one category are not altered.&#xD;
22
+ &#xD;
23
+ If the product is assigned to more than one category, it always will use the path to the first category.&#xD;
24
+ &#xD;
25
+ The specified versions are tested versions. The module may also work on other versions.</description>
26
+ <notes>Change the url of the products to improve indexing on search engines.</notes>
27
+ <authors><author><name>Alessandro Lioce</name><user>alioce</user><email>a.lioce@ne-ws.it</email></author></authors>
28
+ <date>2014-06-10</date>
29
+ <time>08:38:38</time>
30
+ <contents><target name="magecommunity"><dir name="NewsModules"><dir name="CanonicalUrl"><dir name="Block"><dir name="Product"><file name="View.php" hash="c4b2421cf6161539cabb9d037afc8de1"/></dir></dir><dir name="Catalog"><dir name="Model"><dir name="Product"><file name="Url.php" hash="0daed50ad3951ee14800df4c0db1aa2a"/></dir></dir></dir><dir name="etc"><file name="config.xml" hash="091ca25983e433bad8023565f275c95c"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="NewsModules_CanonicalUrl.xml" hash="810f1c3601c299daa715a21abd71372b"/></dir></target></contents>
31
+ <compatible/>
32
+ <dependencies><required><php><min>5.2.13</min><max>5.4.29</max></php></required></dependencies>
33
+ </package>