Question

I am extending Magento_ProductAlert module. By default when subscribing to product it saves configurable product id but I have managed to get selected simple product ID. And later I know that I can get my product url by $product->getProductUrl() but in this case it doesn't work for me because simple products are not visible individually, only in configurable products and using that it returns me link to simple product like this - http://mysite/myproduct-123 but is there a way how can I retrieve configurable product url but with attributes that respond to my simple product? I mean like this - http://mysite/myconfigurableproduct#123=12

Thank you!

When sending email to user that he has subscribed to product I am using this -

<?php if ($_products = $block->getProducts()): ?>
    <?php foreach ($_products as $_product): ?>
        <p><?= __('Hello!') ?></p>
        <p>
            <?= __(
                'You have subscribed for product: <strong class="product name"><a href="%1">%2</a></strong>',
                $_product->getProductUrl(),
                $block->escapeHtml($_product->getName())
            )
            ?>
        </p>
        <p>
            <a href="<?= $block->getUserUnsubscribeUrl() ?>">
                <?= __('Click here to stop alerts for this product.') ?>
            </a>
        </p>
    <?php endforeach ?>
<?php endif ?>

No correct solution

OTHER TIPS

This article by Daan van den Bergh explains how you can do exactly what you need: https://daan.dev/magento/2/redirect-simple-products-configurable-parent-attributes-pre-selected/

And if the image doesn't update when the simple product is selected check this article: https://daan.dev/how-to/uncaught-typeerror-updatedata-undefined/

Regards

Create a custom module which has helper class and it has a method which gives a Configurable product URl.

Helper class

<?php

namespace StackExchange\Magento\Helper;

use Magento\Catalog\Model\Product\Visibility;
class Product  extends \Magento\Framework\App\Helper\AbstractHelper
{

    /**
     * @var \Magento\ConfigurableProduct\Model\Product\Type\Configurable
     */
    private $configurable;

    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface
     */
    private $productRepository;

    public function __construct(
        \Magento\Framework\App\Helper\Context $context,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\ConfigurableProduct\Model\Product\Type\Configurable $configurable    
    ) {
        parent::__construct($context);
        $this->productRepository = $productRepository;
        $this->configurable = $configurable;
    }
   public function getConfigurableProductUrl($product)
   {

      if($product->getVisibility() != Visibility::VISIBILITY_NOT_VISIBLE){
          return false;
      }

      $configProductIds =  $this->configurable->getParentIdsByChild($product->getId());

      if(is_array($configProductIds) && empty($configProductIds)){
          return '#';
      }

      if(is_array($configProductIds) && !empty($configProductIds)){
          $configProductId = $configProductIds[0];
      }
      if(is_int($configProductIds)){
          $configProductId = $configProductIds;
      }

      try{
           $configurablProduct =$this->productRepository
                   ->getById($configProductId, false, $product->getStoreId());
      } catch (\Magento\Framework\Exception\NoSuchEntityException $ex) {
          return false;
      }

     return $configurablProduct->getUrlModel()->getUrl($configurablProduct);
   }
}

Call this helper to your phtml files using

$configHelper = $this->helper('StackExchange\Magento\Helper\Product');

PHTML code looks like:

<?php
$configHelper = $this->helper('StackExchange\Magento\Helper\Product');
?>
<?php if ($_products = $block->getProducts()): ?>
    <?php foreach ($_products as $_product): ?>
        <p><?= __('Hello!') ?></p>
        <?php $isConigProduct = $configHelper->getConfigurableProductUrl($product); ?>
        <p>
            <?php if($isConigProduct): ?>
            <?= __(
                'You have subscribed for product: <strong class="product name"><a href="%1">%2</a></strong>',
                $isConigProduct,
                $block->escapeHtml($_product->getName())
            )
            ?>            
            <?php else: ?>
            <?= __(
                'You have subscribed for product: <strong class="product name"><a href="%1">%2</a></strong>',
                $_product->getProductUrl(),
                $block->escapeHtml($_product->getName())
            )
            ?>
            <?php  endif; ?>
        </p>
        <p>
            <a href="<?= $block->getUserUnsubscribeUrl() ?>">
                <?= __('Click here to stop alerts for this product.') ?>
            </a>
        </p>
    <?php endforeach ?>
<?php endif ?>

Code is not tested given base on Idea.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top