Magento 1 get product or category id by url string












0















I have url string. I need to detect if url belongs to product or category and get product or category id.



I tried this, but it won't work:



  $vPath = 'http://dev.magento186.com/default/example/example3.html';
$oRewrite = Mage::getModel('core/url_rewrite')
->setStoreId(Mage::app()->getStore()->getId())
->loadByRequestPath($vPath);
var_dump($oRewrite->getProductId());


It works only if $vPath is path and not full url. Is there any easy way to get product id or category id from full url?










share|improve this question
















bumped to the homepage by Community 4 mins ago


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




















    0















    I have url string. I need to detect if url belongs to product or category and get product or category id.



    I tried this, but it won't work:



      $vPath = 'http://dev.magento186.com/default/example/example3.html';
    $oRewrite = Mage::getModel('core/url_rewrite')
    ->setStoreId(Mage::app()->getStore()->getId())
    ->loadByRequestPath($vPath);
    var_dump($oRewrite->getProductId());


    It works only if $vPath is path and not full url. Is there any easy way to get product id or category id from full url?










    share|improve this question
















    bumped to the homepage by Community 4 mins ago


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


















      0












      0








      0








      I have url string. I need to detect if url belongs to product or category and get product or category id.



      I tried this, but it won't work:



        $vPath = 'http://dev.magento186.com/default/example/example3.html';
      $oRewrite = Mage::getModel('core/url_rewrite')
      ->setStoreId(Mage::app()->getStore()->getId())
      ->loadByRequestPath($vPath);
      var_dump($oRewrite->getProductId());


      It works only if $vPath is path and not full url. Is there any easy way to get product id or category id from full url?










      share|improve this question
















      I have url string. I need to detect if url belongs to product or category and get product or category id.



      I tried this, but it won't work:



        $vPath = 'http://dev.magento186.com/default/example/example3.html';
      $oRewrite = Mage::getModel('core/url_rewrite')
      ->setStoreId(Mage::app()->getStore()->getId())
      ->loadByRequestPath($vPath);
      var_dump($oRewrite->getProductId());


      It works only if $vPath is path and not full url. Is there any easy way to get product id or category id from full url?







      magento-1 url url-rewrite product-urls






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 1 '17 at 6:26









      Teja Bhagavan Kollepara

      2,95841847




      2,95841847










      asked Oct 31 '17 at 16:56









      JohnyFreeJohnyFree

      85411331




      85411331





      bumped to the homepage by Community 4 mins 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 4 mins ago


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
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Here's an alternative solution.



          First use the URL rewrite model to find the route which matches your product:



              $vPath = 'electronics/cameras/olympus-stylus-750-7-1mp-digital-camera.html';
          $oRewrite = Mage::getModel('core/url_rewrite')
          ->setStoreId(Mage::app()->getStore()->getId())
          ->loadByRequestPath($vPath);


          Then you can call getProductId() on the route to locate the produc's id:



              $iProductId = $oRewrite->getProductId();


          Finally if you require the product model object itself it's then a simple matter to call:



              $oProduct = Mage::getModel('catalog/product')->load($iProductId);


          The main difference between the above, and the code example you've posted is the call to setStoreId. The same product may have different URLs depending on which store it's in, so the routing component needs to have the appropriate store context before it can locate the product to display.



          The advantages of this over Zachary Schuessler's solution is that using the URL rewriter will locate the correct product every time if the trailing portions of the url are the same for different products (e.g. folder1/my-product-name and folder2/my-product-name are different products). Using the URL rewriter also works in situations where "folder1/my-product" refers to different products on different stores. This may or may not apply to your environment.






          share|improve this answer
























          • I think you didn't read my question properly. I don't see any different between your code and the code I provided as example regarding setStoreId. The only different is that I have full url and you put as example path url. My question is how to get product id from string if I have full url. This is the main question. If I had just path it would be easy, but I don't have path, I have full url and I need to get product id from this url when I am inside admin.

            – JohnyFree
            Oct 31 '17 at 21:15



















          0














          After researching I didn't find any better solution so I ended up with this:



            $urlString = "http://dev.magento186.com/default/example/example3.html?example=1";
          $url = Mage::getSingleton('core/url')->parseUrl($urlString);
          $path = $url->getPath();
          $storeCode = Mage::app()->getStore()->getCode();
          $path = str_replace("/".$storeCode."/", "", $path);
          $vPath = str_replace("//", "", $path);


          Now I get proper url path, I can use this:



            $oRewrite = Mage::getModel('core/url_rewrite')
          ->setStoreId(Mage::app()->getStore()->getId())
          ->loadByRequestPath($vPath);
          var_dump($oRewrite->getProductId());





          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%2f199485%2fmagento-1-get-product-or-category-id-by-url-string%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Here's an alternative solution.



            First use the URL rewrite model to find the route which matches your product:



                $vPath = 'electronics/cameras/olympus-stylus-750-7-1mp-digital-camera.html';
            $oRewrite = Mage::getModel('core/url_rewrite')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->loadByRequestPath($vPath);


            Then you can call getProductId() on the route to locate the produc's id:



                $iProductId = $oRewrite->getProductId();


            Finally if you require the product model object itself it's then a simple matter to call:



                $oProduct = Mage::getModel('catalog/product')->load($iProductId);


            The main difference between the above, and the code example you've posted is the call to setStoreId. The same product may have different URLs depending on which store it's in, so the routing component needs to have the appropriate store context before it can locate the product to display.



            The advantages of this over Zachary Schuessler's solution is that using the URL rewriter will locate the correct product every time if the trailing portions of the url are the same for different products (e.g. folder1/my-product-name and folder2/my-product-name are different products). Using the URL rewriter also works in situations where "folder1/my-product" refers to different products on different stores. This may or may not apply to your environment.






            share|improve this answer
























            • I think you didn't read my question properly. I don't see any different between your code and the code I provided as example regarding setStoreId. The only different is that I have full url and you put as example path url. My question is how to get product id from string if I have full url. This is the main question. If I had just path it would be easy, but I don't have path, I have full url and I need to get product id from this url when I am inside admin.

              – JohnyFree
              Oct 31 '17 at 21:15
















            0














            Here's an alternative solution.



            First use the URL rewrite model to find the route which matches your product:



                $vPath = 'electronics/cameras/olympus-stylus-750-7-1mp-digital-camera.html';
            $oRewrite = Mage::getModel('core/url_rewrite')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->loadByRequestPath($vPath);


            Then you can call getProductId() on the route to locate the produc's id:



                $iProductId = $oRewrite->getProductId();


            Finally if you require the product model object itself it's then a simple matter to call:



                $oProduct = Mage::getModel('catalog/product')->load($iProductId);


            The main difference between the above, and the code example you've posted is the call to setStoreId. The same product may have different URLs depending on which store it's in, so the routing component needs to have the appropriate store context before it can locate the product to display.



            The advantages of this over Zachary Schuessler's solution is that using the URL rewriter will locate the correct product every time if the trailing portions of the url are the same for different products (e.g. folder1/my-product-name and folder2/my-product-name are different products). Using the URL rewriter also works in situations where "folder1/my-product" refers to different products on different stores. This may or may not apply to your environment.






            share|improve this answer
























            • I think you didn't read my question properly. I don't see any different between your code and the code I provided as example regarding setStoreId. The only different is that I have full url and you put as example path url. My question is how to get product id from string if I have full url. This is the main question. If I had just path it would be easy, but I don't have path, I have full url and I need to get product id from this url when I am inside admin.

              – JohnyFree
              Oct 31 '17 at 21:15














            0












            0








            0







            Here's an alternative solution.



            First use the URL rewrite model to find the route which matches your product:



                $vPath = 'electronics/cameras/olympus-stylus-750-7-1mp-digital-camera.html';
            $oRewrite = Mage::getModel('core/url_rewrite')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->loadByRequestPath($vPath);


            Then you can call getProductId() on the route to locate the produc's id:



                $iProductId = $oRewrite->getProductId();


            Finally if you require the product model object itself it's then a simple matter to call:



                $oProduct = Mage::getModel('catalog/product')->load($iProductId);


            The main difference between the above, and the code example you've posted is the call to setStoreId. The same product may have different URLs depending on which store it's in, so the routing component needs to have the appropriate store context before it can locate the product to display.



            The advantages of this over Zachary Schuessler's solution is that using the URL rewriter will locate the correct product every time if the trailing portions of the url are the same for different products (e.g. folder1/my-product-name and folder2/my-product-name are different products). Using the URL rewriter also works in situations where "folder1/my-product" refers to different products on different stores. This may or may not apply to your environment.






            share|improve this answer













            Here's an alternative solution.



            First use the URL rewrite model to find the route which matches your product:



                $vPath = 'electronics/cameras/olympus-stylus-750-7-1mp-digital-camera.html';
            $oRewrite = Mage::getModel('core/url_rewrite')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->loadByRequestPath($vPath);


            Then you can call getProductId() on the route to locate the produc's id:



                $iProductId = $oRewrite->getProductId();


            Finally if you require the product model object itself it's then a simple matter to call:



                $oProduct = Mage::getModel('catalog/product')->load($iProductId);


            The main difference between the above, and the code example you've posted is the call to setStoreId. The same product may have different URLs depending on which store it's in, so the routing component needs to have the appropriate store context before it can locate the product to display.



            The advantages of this over Zachary Schuessler's solution is that using the URL rewriter will locate the correct product every time if the trailing portions of the url are the same for different products (e.g. folder1/my-product-name and folder2/my-product-name are different products). Using the URL rewriter also works in situations where "folder1/my-product" refers to different products on different stores. This may or may not apply to your environment.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 31 '17 at 19:06









            Ami KambojAmi Kamboj

            768418




            768418













            • I think you didn't read my question properly. I don't see any different between your code and the code I provided as example regarding setStoreId. The only different is that I have full url and you put as example path url. My question is how to get product id from string if I have full url. This is the main question. If I had just path it would be easy, but I don't have path, I have full url and I need to get product id from this url when I am inside admin.

              – JohnyFree
              Oct 31 '17 at 21:15



















            • I think you didn't read my question properly. I don't see any different between your code and the code I provided as example regarding setStoreId. The only different is that I have full url and you put as example path url. My question is how to get product id from string if I have full url. This is the main question. If I had just path it would be easy, but I don't have path, I have full url and I need to get product id from this url when I am inside admin.

              – JohnyFree
              Oct 31 '17 at 21:15

















            I think you didn't read my question properly. I don't see any different between your code and the code I provided as example regarding setStoreId. The only different is that I have full url and you put as example path url. My question is how to get product id from string if I have full url. This is the main question. If I had just path it would be easy, but I don't have path, I have full url and I need to get product id from this url when I am inside admin.

            – JohnyFree
            Oct 31 '17 at 21:15





            I think you didn't read my question properly. I don't see any different between your code and the code I provided as example regarding setStoreId. The only different is that I have full url and you put as example path url. My question is how to get product id from string if I have full url. This is the main question. If I had just path it would be easy, but I don't have path, I have full url and I need to get product id from this url when I am inside admin.

            – JohnyFree
            Oct 31 '17 at 21:15













            0














            After researching I didn't find any better solution so I ended up with this:



              $urlString = "http://dev.magento186.com/default/example/example3.html?example=1";
            $url = Mage::getSingleton('core/url')->parseUrl($urlString);
            $path = $url->getPath();
            $storeCode = Mage::app()->getStore()->getCode();
            $path = str_replace("/".$storeCode."/", "", $path);
            $vPath = str_replace("//", "", $path);


            Now I get proper url path, I can use this:



              $oRewrite = Mage::getModel('core/url_rewrite')
            ->setStoreId(Mage::app()->getStore()->getId())
            ->loadByRequestPath($vPath);
            var_dump($oRewrite->getProductId());





            share|improve this answer




























              0














              After researching I didn't find any better solution so I ended up with this:



                $urlString = "http://dev.magento186.com/default/example/example3.html?example=1";
              $url = Mage::getSingleton('core/url')->parseUrl($urlString);
              $path = $url->getPath();
              $storeCode = Mage::app()->getStore()->getCode();
              $path = str_replace("/".$storeCode."/", "", $path);
              $vPath = str_replace("//", "", $path);


              Now I get proper url path, I can use this:



                $oRewrite = Mage::getModel('core/url_rewrite')
              ->setStoreId(Mage::app()->getStore()->getId())
              ->loadByRequestPath($vPath);
              var_dump($oRewrite->getProductId());





              share|improve this answer


























                0












                0








                0







                After researching I didn't find any better solution so I ended up with this:



                  $urlString = "http://dev.magento186.com/default/example/example3.html?example=1";
                $url = Mage::getSingleton('core/url')->parseUrl($urlString);
                $path = $url->getPath();
                $storeCode = Mage::app()->getStore()->getCode();
                $path = str_replace("/".$storeCode."/", "", $path);
                $vPath = str_replace("//", "", $path);


                Now I get proper url path, I can use this:



                  $oRewrite = Mage::getModel('core/url_rewrite')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->loadByRequestPath($vPath);
                var_dump($oRewrite->getProductId());





                share|improve this answer













                After researching I didn't find any better solution so I ended up with this:



                  $urlString = "http://dev.magento186.com/default/example/example3.html?example=1";
                $url = Mage::getSingleton('core/url')->parseUrl($urlString);
                $path = $url->getPath();
                $storeCode = Mage::app()->getStore()->getCode();
                $path = str_replace("/".$storeCode."/", "", $path);
                $vPath = str_replace("//", "", $path);


                Now I get proper url path, I can use this:



                  $oRewrite = Mage::getModel('core/url_rewrite')
                ->setStoreId(Mage::app()->getStore()->getId())
                ->loadByRequestPath($vPath);
                var_dump($oRewrite->getProductId());






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 1 '17 at 9:59









                JohnyFreeJohnyFree

                85411331




                85411331






























                    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%2f199485%2fmagento-1-get-product-or-category-id-by-url-string%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