2

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 ?>
2
  • Share your code
    – Asad Khan
    May 30, 2019 at 8:03
  • @AsadKhan not sure what exactly you wanted to see, but updated with example to email template I am sending to user. It gets productUrl but not exactly how I want to. Currently it retrieves simple product id but I want to retrieve configurable with attributes that when opening it selects my simple product
    – Mee
    May 30, 2019 at 8:07

2 Answers 2

1

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.

0

Simple products URL should look like this for example:

/mona-pullover-hoodlie.html#143=167&93=53

where

/mona-pullover-hoodlie.html - configurable product URL

143, 93 - attributes IDs

167, 53 - option IDs.

Attributes IDs and option IDs can be obtained using Magento\ConfigurableProduct\Model\Product\Type\Configurable::getConfigurableAttributesAsArray($product) function.


I made VCT Simple Product URL module on Magento Marketplace that solves this problem.

Your Answer

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

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