1

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?

0

Try it like this:

$product->setStoreId(2)->getUrlInStore();
  • 2
    the call is also returning the url with backoffice in it. – Johannes Pichler Apr 18 '16 at 8:58
  • strange...that used to work for m1. I thought it would be the same. – Marius Apr 18 '16 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

  • useful would be an example – Joel Davey Dec 21 '18 at 17:03
  • Make sense this answer does not – Andre Nickatina Jan 24 '19 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
        );
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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