Trouble getting product URLsMagento: 'Use categories path for product URL’s to no' - Will the old URLs be auto redirected?Magento invalid product urlsMultistore - Prevent product from having multiple urlsLayered Navigation - altering URLsgetting product URL by IDduplicated content, product URLsRegenerate product URLs for new storeConfigurable product with different URLS for simpleURL Rewrite issue - category/sub-category/product urls not workingMagento 2 Duplicate Product URLs Problem

Do I have to take mana from my deck or hand when tapping this card?

Would a primitive species be able to learn English from reading books alone?

Can you describe someone as luxurious? As in someone who likes luxurious things?

Output visual diagram of picture

Do native speakers use "ultima" and "proxima" frequently in spoken English?

Extract substring according to regexp with sed or grep

How can I, as DM, avoid the Conga Line of Death occurring when implementing some form of flanking rule?

Weird lines in Microsoft Word

How do you justify more code being written by following clean code practices?

Reasons for having MCU pin-states default to pull-up/down out of reset

Are all namekians brothers?

Mortal danger in mid-grade literature

What is this high flying aircraft over Pennsylvania?

Center page as a whole without centering each element individually

I keep switching characters, how do I stop?

Showing mass murder in a kid's book

Calculate Pi using Monte Carlo

Is there a distance limit for minecart tracks?

Should I be concerned about student access to a test bank?

How do I prevent inappropriate ads from appearing in my game?

Why does a 97 / 92 key piano exist by Bosendorfer?

Is there a POSIX way to shutdown a UNIX machine?

Why is indicated airspeed rather than ground speed used during the takeoff roll?

Hashing password to increase entropy



Trouble getting product URLs


Magento: 'Use categories path for product URL’s to no' - Will the old URLs be auto redirected?Magento invalid product urlsMultistore - Prevent product from having multiple urlsLayered Navigation - altering URLsgetting product URL by IDduplicated content, product URLsRegenerate product URLs for new storeConfigurable product with different URLS for simpleURL Rewrite issue - category/sub-category/product urls not workingMagento 2 Duplicate Product URLs Problem













0















After looking at various methods, the following code is almost getting what I want:



 $product = Mage::getModel('catalog/product')->load($product_id);
$site_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
$product_url = '';
$cats = $product->getCategoryIds();
if (sizeof($cats))
$category = Mage::getModel('catalog/category')->load((int) $cats[0]);
$product_url = $site_url.$product->getUrlPath($category);



This returns: http://magento.dev/men/shirts/french-cuff-cotton-twill-oxford-570.html.



However this produces a 404.



570 is not the ID that corresponds to this product so I'm wondering where it's coming from.










share|improve this question




























    0















    After looking at various methods, the following code is almost getting what I want:



     $product = Mage::getModel('catalog/product')->load($product_id);
    $site_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
    $product_url = '';
    $cats = $product->getCategoryIds();
    if (sizeof($cats))
    $category = Mage::getModel('catalog/category')->load((int) $cats[0]);
    $product_url = $site_url.$product->getUrlPath($category);



    This returns: http://magento.dev/men/shirts/french-cuff-cotton-twill-oxford-570.html.



    However this produces a 404.



    570 is not the ID that corresponds to this product so I'm wondering where it's coming from.










    share|improve this question


























      0












      0








      0








      After looking at various methods, the following code is almost getting what I want:



       $product = Mage::getModel('catalog/product')->load($product_id);
      $site_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
      $product_url = '';
      $cats = $product->getCategoryIds();
      if (sizeof($cats))
      $category = Mage::getModel('catalog/category')->load((int) $cats[0]);
      $product_url = $site_url.$product->getUrlPath($category);



      This returns: http://magento.dev/men/shirts/french-cuff-cotton-twill-oxford-570.html.



      However this produces a 404.



      570 is not the ID that corresponds to this product so I'm wondering where it's coming from.










      share|improve this question
















      After looking at various methods, the following code is almost getting what I want:



       $product = Mage::getModel('catalog/product')->load($product_id);
      $site_url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB);
      $product_url = '';
      $cats = $product->getCategoryIds();
      if (sizeof($cats))
      $category = Mage::getModel('catalog/category')->load((int) $cats[0]);
      $product_url = $site_url.$product->getUrlPath($category);



      This returns: http://magento.dev/men/shirts/french-cuff-cotton-twill-oxford-570.html.



      However this produces a 404.



      570 is not the ID that corresponds to this product so I'm wondering where it's coming from.







      url






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited 47 mins ago









      Teja Bhagavan Kollepara

      3,00641949




      3,00641949










      asked Mar 24 '17 at 17:42









      mikejwmikejw

      1




      1




















          1 Answer
          1






          active

          oldest

          votes


















          0














          Instead of using $product_url = $site_url.$product->getUrlPath($category); those.



          You use below code




           $product = Mage::getModel('catalog/product')->load($product_id);
          $product_url = '';
          $cats = $product->getCategoryIds();
          if (sizeof($cats))
          $category = Mage::getModel('catalog/category')->load((int) $cats[0]);
          if($category->getId())
          $product->setCategory($category);


          $product_url = $product->getProductUrl();



          Or via ProductCollection with addUrlRewrite() function:




          $product = Mage::getModel('catalog/product')->load($product_id);
          $product_url = $product->getProductUrl();
          $cats = $product->getCategoryIds();

          if (sizeof($cats))
          $collection = Mage::getResourceModel('catalog/product_collection');
          $collection->addIdFilter($product->getId());
          if($collection->getSize() >= 1):
          /* Add addUrlRewrite for add category url with product */
          $collection->addUrlRewrite((int)$cats[0]);
          $ProductWithCategory = $collection->getFirstItem();
          $product_url = $ProductWithCategory->getProductUrl();
          endif;







          share|improve this answer

























          • Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.

            – mikejw
            Mar 27 '17 at 9:53












          • Please do indexing from shell check and check the result

            – Amit Bera
            Mar 27 '17 at 9:57












          • Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.

            – mikejw
            Mar 27 '17 at 10:11










          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%2f166106%2ftrouble-getting-product-urls%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














          Instead of using $product_url = $site_url.$product->getUrlPath($category); those.



          You use below code




           $product = Mage::getModel('catalog/product')->load($product_id);
          $product_url = '';
          $cats = $product->getCategoryIds();
          if (sizeof($cats))
          $category = Mage::getModel('catalog/category')->load((int) $cats[0]);
          if($category->getId())
          $product->setCategory($category);


          $product_url = $product->getProductUrl();



          Or via ProductCollection with addUrlRewrite() function:




          $product = Mage::getModel('catalog/product')->load($product_id);
          $product_url = $product->getProductUrl();
          $cats = $product->getCategoryIds();

          if (sizeof($cats))
          $collection = Mage::getResourceModel('catalog/product_collection');
          $collection->addIdFilter($product->getId());
          if($collection->getSize() >= 1):
          /* Add addUrlRewrite for add category url with product */
          $collection->addUrlRewrite((int)$cats[0]);
          $ProductWithCategory = $collection->getFirstItem();
          $product_url = $ProductWithCategory->getProductUrl();
          endif;







          share|improve this answer

























          • Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.

            – mikejw
            Mar 27 '17 at 9:53












          • Please do indexing from shell check and check the result

            – Amit Bera
            Mar 27 '17 at 9:57












          • Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.

            – mikejw
            Mar 27 '17 at 10:11















          0














          Instead of using $product_url = $site_url.$product->getUrlPath($category); those.



          You use below code




           $product = Mage::getModel('catalog/product')->load($product_id);
          $product_url = '';
          $cats = $product->getCategoryIds();
          if (sizeof($cats))
          $category = Mage::getModel('catalog/category')->load((int) $cats[0]);
          if($category->getId())
          $product->setCategory($category);


          $product_url = $product->getProductUrl();



          Or via ProductCollection with addUrlRewrite() function:




          $product = Mage::getModel('catalog/product')->load($product_id);
          $product_url = $product->getProductUrl();
          $cats = $product->getCategoryIds();

          if (sizeof($cats))
          $collection = Mage::getResourceModel('catalog/product_collection');
          $collection->addIdFilter($product->getId());
          if($collection->getSize() >= 1):
          /* Add addUrlRewrite for add category url with product */
          $collection->addUrlRewrite((int)$cats[0]);
          $ProductWithCategory = $collection->getFirstItem();
          $product_url = $ProductWithCategory->getProductUrl();
          endif;







          share|improve this answer

























          • Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.

            – mikejw
            Mar 27 '17 at 9:53












          • Please do indexing from shell check and check the result

            – Amit Bera
            Mar 27 '17 at 9:57












          • Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.

            – mikejw
            Mar 27 '17 at 10:11













          0












          0








          0







          Instead of using $product_url = $site_url.$product->getUrlPath($category); those.



          You use below code




           $product = Mage::getModel('catalog/product')->load($product_id);
          $product_url = '';
          $cats = $product->getCategoryIds();
          if (sizeof($cats))
          $category = Mage::getModel('catalog/category')->load((int) $cats[0]);
          if($category->getId())
          $product->setCategory($category);


          $product_url = $product->getProductUrl();



          Or via ProductCollection with addUrlRewrite() function:




          $product = Mage::getModel('catalog/product')->load($product_id);
          $product_url = $product->getProductUrl();
          $cats = $product->getCategoryIds();

          if (sizeof($cats))
          $collection = Mage::getResourceModel('catalog/product_collection');
          $collection->addIdFilter($product->getId());
          if($collection->getSize() >= 1):
          /* Add addUrlRewrite for add category url with product */
          $collection->addUrlRewrite((int)$cats[0]);
          $ProductWithCategory = $collection->getFirstItem();
          $product_url = $ProductWithCategory->getProductUrl();
          endif;







          share|improve this answer















          Instead of using $product_url = $site_url.$product->getUrlPath($category); those.



          You use below code




           $product = Mage::getModel('catalog/product')->load($product_id);
          $product_url = '';
          $cats = $product->getCategoryIds();
          if (sizeof($cats))
          $category = Mage::getModel('catalog/category')->load((int) $cats[0]);
          if($category->getId())
          $product->setCategory($category);


          $product_url = $product->getProductUrl();



          Or via ProductCollection with addUrlRewrite() function:




          $product = Mage::getModel('catalog/product')->load($product_id);
          $product_url = $product->getProductUrl();
          $cats = $product->getCategoryIds();

          if (sizeof($cats))
          $collection = Mage::getResourceModel('catalog/product_collection');
          $collection->addIdFilter($product->getId());
          if($collection->getSize() >= 1):
          /* Add addUrlRewrite for add category url with product */
          $collection->addUrlRewrite((int)$cats[0]);
          $ProductWithCategory = $collection->getFirstItem();
          $product_url = $ProductWithCategory->getProductUrl();
          endif;








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 24 '17 at 18:15

























          answered Mar 24 '17 at 18:06









          Amit BeraAmit Bera

          59.3k1575177




          59.3k1575177












          • Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.

            – mikejw
            Mar 27 '17 at 9:53












          • Please do indexing from shell check and check the result

            – Amit Bera
            Mar 27 '17 at 9:57












          • Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.

            – mikejw
            Mar 27 '17 at 10:11

















          • Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.

            – mikejw
            Mar 27 '17 at 9:53












          • Please do indexing from shell check and check the result

            – Amit Bera
            Mar 27 '17 at 9:57












          • Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.

            – mikejw
            Mar 27 '17 at 10:11
















          Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.

          – mikejw
          Mar 27 '17 at 9:53






          Thanks Amit. The first recommendation worked and produced the url: magento.dev/index.php/catalog/product/view/id/402/s/… The second solution didn't seem to do what was expected. It produced the URL: magento.dev/index.php/catalog/product/view/id/402 This seems less "url-rewritten" than the first one. It seems insane but I'm now seriously considering sticking with my original method but doing a REGEX replace on the ID to produce the correct URL. Only by doing this horrible hack will I get the URL that the front-end uses.

          – mikejw
          Mar 27 '17 at 9:53














          Please do indexing from shell check and check the result

          – Amit Bera
          Mar 27 '17 at 9:57






          Please do indexing from shell check and check the result

          – Amit Bera
          Mar 27 '17 at 9:57














          Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.

          – mikejw
          Mar 27 '17 at 10:11





          Hi Amit, I've successfully run "php indexer.php --reindex" and "php indexer.php --reindex all". This has no impact on the results I'm getting.

          – mikejw
          Mar 27 '17 at 10:11

















          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%2f166106%2ftrouble-getting-product-urls%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

          Can not update quote_id field of “quote_item” table magento 2Magento 2.1 - We can't remove the item. (Shopping Cart doesnt allow us to remove items before becomes empty)Add value for custom quote item attribute using REST apiREST API endpoint v1/carts/cartId/items always returns error messageCorrect way to save entries to databaseHow to remove all associated quote objects of a customer completelyMagento 2 - Save value from custom input field to quote_itemGet quote_item data using quote id and product id filter in Magento 2How to set additional data to quote_item table from controller in Magento 2?What is the purpose of additional_data column in quote_item table in magento2Set Custom Price to Quote item magento2 from controller

          Magento 2 disable Secret Key on URL's from terminal The Next CEO of Stack OverflowMagento 2 Shortcut/GUI tool to perform commandline tasks for windowsIn menu add configuration linkMagento oAuth : Generating access token and access secretMagento 2 security key issue in Third-Party API redirect URIPublic actions in admin controllersHow to Disable Cache in Custom WidgetURL Key not changing in Magento 2Product URL Key gets deleted when importing custom options - Magento 2Problem with reindex terminalMagento 2 - bin/magento Commands not working in Cpanel Terminal

          Aasi (pallopeli) Navigointivalikko