Getting id_path or request_path or target_path of current category or product page












2















I have many store views. I need to be able to retrieve id_path or request_path or target_path of current category or product page, so that I could retrieve target_path of the same category or product page for a different store view.










share|improve this question














bumped to the homepage by Community 1 hour ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.




















    2















    I have many store views. I need to be able to retrieve id_path or request_path or target_path of current category or product page, so that I could retrieve target_path of the same category or product page for a different store view.










    share|improve this question














    bumped to the homepage by Community 1 hour ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.


















      2












      2








      2








      I have many store views. I need to be able to retrieve id_path or request_path or target_path of current category or product page, so that I could retrieve target_path of the same category or product page for a different store view.










      share|improve this question














      I have many store views. I need to be able to retrieve id_path or request_path or target_path of current category or product page, so that I could retrieve target_path of the same category or product page for a different store view.







      redirect






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 3 '15 at 14:33









      tmmtmm

      1198




      1198





      bumped to the homepage by Community 1 hour ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 1 hour ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I needed to do this for the hreflang tags.



          To make things easier I have the same url-key for the products, set to the product name and colour. Fortunately the model names are the same in all languages for this catalogue. I don't include the category path in the URL, model name and colour gives the uniques.



          When it comes to categories, here is how I do it for the sitemaps. I am sure you can make this code more elegant, however, it is not something that needs to be fast for a user so I have gone for code that works rather than optimised code. My store codes are things like 'en_us' and I do a bit of manipulation on that, but here is the loop I have:



              /**
          * Generate categories sitemap
          */
          $changefreq = (string) Mage::getStoreConfig('sitemap/category/changefreq', $storeId);
          $priority = (string) Mage::getStoreConfig('sitemap/category/priority', $storeId);
          $collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
          $categories = new Varien_Object();
          $categories->setItems($collection);
          Mage::dispatchEvent('sitemap_categories_generating_before', array(
          'collection' => $categories
          ));
          foreach ($categories->getItems() as $item) {
          $hreflangs ='';
          foreach (Mage::app()->getWebsites() as $website) {
          foreach ($website->getGroups() as $group) {
          $stores = $group->getStores();
          foreach ($stores as $store) {
          $code = explode('_', $store->getCode());
          $hreflang = $code[0];
          if(isset($code[1])) $hreflang .= '-' . strtolower($code[1]);
          $rewriteUrl = Mage::getModel('core/url_rewrite')->setStoreId($store->getId())->loadByIdPath('category/'. $item->getId());
          if ($rewriteUrl->getId()) {
          $link = $rewriteUrl->getRequestPath();
          } else {
          $link = $item->getUrl();
          }
          $hreflangs .= '<xhtml:link rel="alternate" href="' . $store->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) . $link . '" hreflang="' . $hreflang . '"/>' . "n";
          }
          }
          }

          $xml = sprintf(
          '<url><loc>%s</loc>%s<lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
          htmlspecialchars($baseUrl . $item->getUrl()),
          $hreflangs,
          substr(Mage::getModel('catalog/category')->load($item->getId())->getUpdatedAt(),0,10),
          $changefreq,
          $priority
          );
          $io->streamWrite($xml);
          }
          unset($collection);


          This results in things like:



          <url>
          <loc>http://local.example.com/en_gb/men/key-holders</loc>
          <xhtml:link rel="alternate" href="http://local.example.com/it_it/men/portachiavi" hreflang="it-it"/>
          <xhtml:link rel="alternate" href="http://local.example.com/en_eu/men/key-holders" hreflang="en-eu"/>
          <xhtml:link rel="alternate" href="http://local.example.com/it_gb/men/portachiavi" hreflang="it-gb"/>
          <xhtml:link rel="alternate" href="http://local.example.com/en_gb/men/key-holders" hreflang="en-gb"/>
          <xhtml:link rel="alternate" href="http://local.example.com/it_us/men/portachiavi" hreflang="it-us"/>
          <xhtml:link rel="alternate" href="http://local.example.com/en_us/men/key-holders" hreflang="en-us"/>
          <xhtml:link rel="alternate" href="http://local.example.com/it/men/portachiavi" hreflang="it"/>
          <xhtml:link rel="alternate" href="http://local.example.com/en/men/key-holders" hreflang="en"/>
          <lastmod>2015-01-09</lastmod>
          <changefreq>daily</changefreq>
          <priority>0.5</priority>
          </url>


          It is still early days for me on this, please let me know if I am getting my hreflang tags wrong!



          Needless to say the same technique should work for products.






          share|improve this answer























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "479"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f59430%2fgetting-id-path-or-request-path-or-target-path-of-current-category-or-product-pa%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I needed to do this for the hreflang tags.



            To make things easier I have the same url-key for the products, set to the product name and colour. Fortunately the model names are the same in all languages for this catalogue. I don't include the category path in the URL, model name and colour gives the uniques.



            When it comes to categories, here is how I do it for the sitemaps. I am sure you can make this code more elegant, however, it is not something that needs to be fast for a user so I have gone for code that works rather than optimised code. My store codes are things like 'en_us' and I do a bit of manipulation on that, but here is the loop I have:



                /**
            * Generate categories sitemap
            */
            $changefreq = (string) Mage::getStoreConfig('sitemap/category/changefreq', $storeId);
            $priority = (string) Mage::getStoreConfig('sitemap/category/priority', $storeId);
            $collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
            $categories = new Varien_Object();
            $categories->setItems($collection);
            Mage::dispatchEvent('sitemap_categories_generating_before', array(
            'collection' => $categories
            ));
            foreach ($categories->getItems() as $item) {
            $hreflangs ='';
            foreach (Mage::app()->getWebsites() as $website) {
            foreach ($website->getGroups() as $group) {
            $stores = $group->getStores();
            foreach ($stores as $store) {
            $code = explode('_', $store->getCode());
            $hreflang = $code[0];
            if(isset($code[1])) $hreflang .= '-' . strtolower($code[1]);
            $rewriteUrl = Mage::getModel('core/url_rewrite')->setStoreId($store->getId())->loadByIdPath('category/'. $item->getId());
            if ($rewriteUrl->getId()) {
            $link = $rewriteUrl->getRequestPath();
            } else {
            $link = $item->getUrl();
            }
            $hreflangs .= '<xhtml:link rel="alternate" href="' . $store->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) . $link . '" hreflang="' . $hreflang . '"/>' . "n";
            }
            }
            }

            $xml = sprintf(
            '<url><loc>%s</loc>%s<lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
            htmlspecialchars($baseUrl . $item->getUrl()),
            $hreflangs,
            substr(Mage::getModel('catalog/category')->load($item->getId())->getUpdatedAt(),0,10),
            $changefreq,
            $priority
            );
            $io->streamWrite($xml);
            }
            unset($collection);


            This results in things like:



            <url>
            <loc>http://local.example.com/en_gb/men/key-holders</loc>
            <xhtml:link rel="alternate" href="http://local.example.com/it_it/men/portachiavi" hreflang="it-it"/>
            <xhtml:link rel="alternate" href="http://local.example.com/en_eu/men/key-holders" hreflang="en-eu"/>
            <xhtml:link rel="alternate" href="http://local.example.com/it_gb/men/portachiavi" hreflang="it-gb"/>
            <xhtml:link rel="alternate" href="http://local.example.com/en_gb/men/key-holders" hreflang="en-gb"/>
            <xhtml:link rel="alternate" href="http://local.example.com/it_us/men/portachiavi" hreflang="it-us"/>
            <xhtml:link rel="alternate" href="http://local.example.com/en_us/men/key-holders" hreflang="en-us"/>
            <xhtml:link rel="alternate" href="http://local.example.com/it/men/portachiavi" hreflang="it"/>
            <xhtml:link rel="alternate" href="http://local.example.com/en/men/key-holders" hreflang="en"/>
            <lastmod>2015-01-09</lastmod>
            <changefreq>daily</changefreq>
            <priority>0.5</priority>
            </url>


            It is still early days for me on this, please let me know if I am getting my hreflang tags wrong!



            Needless to say the same technique should work for products.






            share|improve this answer




























              0














              I needed to do this for the hreflang tags.



              To make things easier I have the same url-key for the products, set to the product name and colour. Fortunately the model names are the same in all languages for this catalogue. I don't include the category path in the URL, model name and colour gives the uniques.



              When it comes to categories, here is how I do it for the sitemaps. I am sure you can make this code more elegant, however, it is not something that needs to be fast for a user so I have gone for code that works rather than optimised code. My store codes are things like 'en_us' and I do a bit of manipulation on that, but here is the loop I have:



                  /**
              * Generate categories sitemap
              */
              $changefreq = (string) Mage::getStoreConfig('sitemap/category/changefreq', $storeId);
              $priority = (string) Mage::getStoreConfig('sitemap/category/priority', $storeId);
              $collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
              $categories = new Varien_Object();
              $categories->setItems($collection);
              Mage::dispatchEvent('sitemap_categories_generating_before', array(
              'collection' => $categories
              ));
              foreach ($categories->getItems() as $item) {
              $hreflangs ='';
              foreach (Mage::app()->getWebsites() as $website) {
              foreach ($website->getGroups() as $group) {
              $stores = $group->getStores();
              foreach ($stores as $store) {
              $code = explode('_', $store->getCode());
              $hreflang = $code[0];
              if(isset($code[1])) $hreflang .= '-' . strtolower($code[1]);
              $rewriteUrl = Mage::getModel('core/url_rewrite')->setStoreId($store->getId())->loadByIdPath('category/'. $item->getId());
              if ($rewriteUrl->getId()) {
              $link = $rewriteUrl->getRequestPath();
              } else {
              $link = $item->getUrl();
              }
              $hreflangs .= '<xhtml:link rel="alternate" href="' . $store->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) . $link . '" hreflang="' . $hreflang . '"/>' . "n";
              }
              }
              }

              $xml = sprintf(
              '<url><loc>%s</loc>%s<lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
              htmlspecialchars($baseUrl . $item->getUrl()),
              $hreflangs,
              substr(Mage::getModel('catalog/category')->load($item->getId())->getUpdatedAt(),0,10),
              $changefreq,
              $priority
              );
              $io->streamWrite($xml);
              }
              unset($collection);


              This results in things like:



              <url>
              <loc>http://local.example.com/en_gb/men/key-holders</loc>
              <xhtml:link rel="alternate" href="http://local.example.com/it_it/men/portachiavi" hreflang="it-it"/>
              <xhtml:link rel="alternate" href="http://local.example.com/en_eu/men/key-holders" hreflang="en-eu"/>
              <xhtml:link rel="alternate" href="http://local.example.com/it_gb/men/portachiavi" hreflang="it-gb"/>
              <xhtml:link rel="alternate" href="http://local.example.com/en_gb/men/key-holders" hreflang="en-gb"/>
              <xhtml:link rel="alternate" href="http://local.example.com/it_us/men/portachiavi" hreflang="it-us"/>
              <xhtml:link rel="alternate" href="http://local.example.com/en_us/men/key-holders" hreflang="en-us"/>
              <xhtml:link rel="alternate" href="http://local.example.com/it/men/portachiavi" hreflang="it"/>
              <xhtml:link rel="alternate" href="http://local.example.com/en/men/key-holders" hreflang="en"/>
              <lastmod>2015-01-09</lastmod>
              <changefreq>daily</changefreq>
              <priority>0.5</priority>
              </url>


              It is still early days for me on this, please let me know if I am getting my hreflang tags wrong!



              Needless to say the same technique should work for products.






              share|improve this answer


























                0












                0








                0







                I needed to do this for the hreflang tags.



                To make things easier I have the same url-key for the products, set to the product name and colour. Fortunately the model names are the same in all languages for this catalogue. I don't include the category path in the URL, model name and colour gives the uniques.



                When it comes to categories, here is how I do it for the sitemaps. I am sure you can make this code more elegant, however, it is not something that needs to be fast for a user so I have gone for code that works rather than optimised code. My store codes are things like 'en_us' and I do a bit of manipulation on that, but here is the loop I have:



                    /**
                * Generate categories sitemap
                */
                $changefreq = (string) Mage::getStoreConfig('sitemap/category/changefreq', $storeId);
                $priority = (string) Mage::getStoreConfig('sitemap/category/priority', $storeId);
                $collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
                $categories = new Varien_Object();
                $categories->setItems($collection);
                Mage::dispatchEvent('sitemap_categories_generating_before', array(
                'collection' => $categories
                ));
                foreach ($categories->getItems() as $item) {
                $hreflangs ='';
                foreach (Mage::app()->getWebsites() as $website) {
                foreach ($website->getGroups() as $group) {
                $stores = $group->getStores();
                foreach ($stores as $store) {
                $code = explode('_', $store->getCode());
                $hreflang = $code[0];
                if(isset($code[1])) $hreflang .= '-' . strtolower($code[1]);
                $rewriteUrl = Mage::getModel('core/url_rewrite')->setStoreId($store->getId())->loadByIdPath('category/'. $item->getId());
                if ($rewriteUrl->getId()) {
                $link = $rewriteUrl->getRequestPath();
                } else {
                $link = $item->getUrl();
                }
                $hreflangs .= '<xhtml:link rel="alternate" href="' . $store->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) . $link . '" hreflang="' . $hreflang . '"/>' . "n";
                }
                }
                }

                $xml = sprintf(
                '<url><loc>%s</loc>%s<lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
                htmlspecialchars($baseUrl . $item->getUrl()),
                $hreflangs,
                substr(Mage::getModel('catalog/category')->load($item->getId())->getUpdatedAt(),0,10),
                $changefreq,
                $priority
                );
                $io->streamWrite($xml);
                }
                unset($collection);


                This results in things like:



                <url>
                <loc>http://local.example.com/en_gb/men/key-holders</loc>
                <xhtml:link rel="alternate" href="http://local.example.com/it_it/men/portachiavi" hreflang="it-it"/>
                <xhtml:link rel="alternate" href="http://local.example.com/en_eu/men/key-holders" hreflang="en-eu"/>
                <xhtml:link rel="alternate" href="http://local.example.com/it_gb/men/portachiavi" hreflang="it-gb"/>
                <xhtml:link rel="alternate" href="http://local.example.com/en_gb/men/key-holders" hreflang="en-gb"/>
                <xhtml:link rel="alternate" href="http://local.example.com/it_us/men/portachiavi" hreflang="it-us"/>
                <xhtml:link rel="alternate" href="http://local.example.com/en_us/men/key-holders" hreflang="en-us"/>
                <xhtml:link rel="alternate" href="http://local.example.com/it/men/portachiavi" hreflang="it"/>
                <xhtml:link rel="alternate" href="http://local.example.com/en/men/key-holders" hreflang="en"/>
                <lastmod>2015-01-09</lastmod>
                <changefreq>daily</changefreq>
                <priority>0.5</priority>
                </url>


                It is still early days for me on this, please let me know if I am getting my hreflang tags wrong!



                Needless to say the same technique should work for products.






                share|improve this answer













                I needed to do this for the hreflang tags.



                To make things easier I have the same url-key for the products, set to the product name and colour. Fortunately the model names are the same in all languages for this catalogue. I don't include the category path in the URL, model name and colour gives the uniques.



                When it comes to categories, here is how I do it for the sitemaps. I am sure you can make this code more elegant, however, it is not something that needs to be fast for a user so I have gone for code that works rather than optimised code. My store codes are things like 'en_us' and I do a bit of manipulation on that, but here is the loop I have:



                    /**
                * Generate categories sitemap
                */
                $changefreq = (string) Mage::getStoreConfig('sitemap/category/changefreq', $storeId);
                $priority = (string) Mage::getStoreConfig('sitemap/category/priority', $storeId);
                $collection = Mage::getResourceModel('sitemap/catalog_category')->getCollection($storeId);
                $categories = new Varien_Object();
                $categories->setItems($collection);
                Mage::dispatchEvent('sitemap_categories_generating_before', array(
                'collection' => $categories
                ));
                foreach ($categories->getItems() as $item) {
                $hreflangs ='';
                foreach (Mage::app()->getWebsites() as $website) {
                foreach ($website->getGroups() as $group) {
                $stores = $group->getStores();
                foreach ($stores as $store) {
                $code = explode('_', $store->getCode());
                $hreflang = $code[0];
                if(isset($code[1])) $hreflang .= '-' . strtolower($code[1]);
                $rewriteUrl = Mage::getModel('core/url_rewrite')->setStoreId($store->getId())->loadByIdPath('category/'. $item->getId());
                if ($rewriteUrl->getId()) {
                $link = $rewriteUrl->getRequestPath();
                } else {
                $link = $item->getUrl();
                }
                $hreflangs .= '<xhtml:link rel="alternate" href="' . $store->getBaseUrl(Mage_Core_Model_Store::URL_TYPE_LINK) . $link . '" hreflang="' . $hreflang . '"/>' . "n";
                }
                }
                }

                $xml = sprintf(
                '<url><loc>%s</loc>%s<lastmod>%s</lastmod><changefreq>%s</changefreq><priority>%.1f</priority></url>',
                htmlspecialchars($baseUrl . $item->getUrl()),
                $hreflangs,
                substr(Mage::getModel('catalog/category')->load($item->getId())->getUpdatedAt(),0,10),
                $changefreq,
                $priority
                );
                $io->streamWrite($xml);
                }
                unset($collection);


                This results in things like:



                <url>
                <loc>http://local.example.com/en_gb/men/key-holders</loc>
                <xhtml:link rel="alternate" href="http://local.example.com/it_it/men/portachiavi" hreflang="it-it"/>
                <xhtml:link rel="alternate" href="http://local.example.com/en_eu/men/key-holders" hreflang="en-eu"/>
                <xhtml:link rel="alternate" href="http://local.example.com/it_gb/men/portachiavi" hreflang="it-gb"/>
                <xhtml:link rel="alternate" href="http://local.example.com/en_gb/men/key-holders" hreflang="en-gb"/>
                <xhtml:link rel="alternate" href="http://local.example.com/it_us/men/portachiavi" hreflang="it-us"/>
                <xhtml:link rel="alternate" href="http://local.example.com/en_us/men/key-holders" hreflang="en-us"/>
                <xhtml:link rel="alternate" href="http://local.example.com/it/men/portachiavi" hreflang="it"/>
                <xhtml:link rel="alternate" href="http://local.example.com/en/men/key-holders" hreflang="en"/>
                <lastmod>2015-01-09</lastmod>
                <changefreq>daily</changefreq>
                <priority>0.5</priority>
                </url>


                It is still early days for me on this, please let me know if I am getting my hreflang tags wrong!



                Needless to say the same technique should work for products.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 3 '15 at 16:02









                Henry's CatHenry's Cat

                1,7051923




                1,7051923






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Magento Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmagento.stackexchange.com%2fquestions%2f59430%2fgetting-id-path-or-request-path-or-target-path-of-current-category-or-product-pa%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    What other Star Trek series did the main TNG cast show up in?

                    Berlina muro

                    Berlina aerponto