2

I'm writing a custom indexer and I need to retrieve the product URL for different stores.

$product->setStoreId(2)->getProductUrl();

still returns the URL with the admin store (backoffice) in it.
Do you have an idea how I can retrieve the store specific product URL?

4 Answers 4

0

Try it like this:

$product->setStoreId(2)->getUrlInStore();
2
  • 2
    the call is also returning the url with backoffice in it. Commented Apr 18, 2016 at 8:58
  • strange...that used to work for m1. I thought it would be the same.
    – Marius
    Commented Apr 18, 2016 at 9:05
0

$product->setStoreId(2)->getUrlInStore();

but if you use it from backoffice, be sure that productUrlModel not the backoffice model - it's the bug from the Magento as I suppose. Set Magento\Framework\Url

2
  • useful would be an example
    – Joel Davey
    Commented Dec 21, 2018 at 17:03
  • Make sense this answer does not Commented Jan 24, 2019 at 22:27
0

There are Magento 2 bugs created concerning this issue: https://github.com/magento/magento2/issues/4247 https://github.com/magento/magento2/issues/19196

I have only made a workaround by replacing admin URL with frontend store URL:

    /**
     * @var \Magento\Framework\App\Config\ScopeConfigInterface
     */
    private $config;

    /**
     * Url constructor.
     *
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $config
     */
    public function __construct(\Magento\Framework\App\Config\ScopeConfigInterface $config)
    {
        $this->config = $config;
    }

    /**
     * {@inheritdoc}
     */
    public function getProductUrl(Product $product): string
    {
        $isSecure   = $product->getStore()->isFrontUrlSecure();
        $productUrl = $product->getUrlModel()->getUrl($product, ['_secure' => $isSecure]);

        /**
         * There is a Magento bug with getting product URL from adminhtml area:
         * https://github.com/magento/magento2/issues/4247
         * https://github.com/magento/magento2/issues/19196
         *
         * Making a workaround to return proper frontend product URL based on store
         */
        if ($this->isAdminUrlUseCustom()) {
            $productUrl = str_replace(
                $this->getBaseUrl(0, $isSecure),
                $this->getBaseUrl($product->getStoreId(), $isSecure),
                $productUrl
            );
        }

        return $productUrl;
    }

    /**
     * Is admin URL use custom
     *
     * @return bool
     */
    private function isAdminUrlUseCustom(): bool
    {
        return $this->config->isSetFlag('admin/url/use_custom');
    }

    /**
     * Get base url for store
     *
     * @param int  $storeId
     * @param bool $isSecure
     * @return string
     */
    private function getBaseUrl(int $storeId, bool $isSecure): string
    {
        if ($isSecure || 0 === $storeId) {
            return $this->config->getValue(
                'web/secure/base_url',
                \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
                $storeId
            );
        }

        return $this->config->getValue(
            'web/unsecure/base_url',
            \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
            $storeId
        );
    }
0

Get product URL store wise using following code.

protected $productRepository;
protected $_storeManager;


public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\Store\Model\StoreManagerInterface $storeManager
) {
    $this->productRepository = $productRepository;
    $this->_storeManager = $storeManager;
}



public function getProductUrlStoreWise(int $productId)
{
    
    $websiteId = $this->_storeManager->getStore()->getWebsiteId();

    $product    = $this->productRepository->getById($productId, false, $websiteId);
    $productUrl = $product->getProductUrl();
    return $productUrl;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.