0

I want to get producturl in sales order email template in multi website.

I have used

$_item = $block->getItem();
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();

$product = $_objectManager->get('Magento\Catalog\Model\Product')->load($_item->getProductId());
$product->setStoreId($_item->getStoreId());

$_imagehelper = $this->helper('Magento\Catalog\Helper\Image');
if($childProd = current($_item->getChildrenItems()))
{
   $productImage = $_imagehelper->init($childProd->getProduct(), 'category_page_list')->resize(80, 80)->getUrl();
}
else
{
   $productImage = $_imagehelper->init($_item->getProduct(), 'category_page_list')->resize(80, 80)->getUrl();
}

?>
<tr>      
    <td class="item-image">
       <img src="<?php echo $productImage; ?>" alt="<?= $block->escapeHtml($_item->getName()) ?>" />
    </td>   
    <td class="item-info<?php if ($block->getItemOptions()): ?> has-extra<?php endif; ?>">      
        <p class="product-name"><a href="<?= $product->getProductUrl(); ?>" style="color:#000;"><?= $block->escapeHtml($_item->getName()) ?></a></p>

        <p class="sku"><?= /* @escapeNotVerified */  __('SKU') ?>: <?= $block->escapeHtml($block->getSku($_item)) ?></p>

        <?php if ($block->getItemOptions()): ?>
            <dl class="item-options">
            <?php foreach ($block->getItemOptions() as $option): ?>
                <dt style="font-size: 13px;font-family: Tahoma,sans-serif;"><strong><em><?= /* @escapeNotVerified */  $option['label'] ?> : </em></strong><?= /* @escapeNotVerified */  nl2br($option['value']) ?></dt>               
            <?php endforeach; ?>
            </dl>
        <?php endif; ?>
        <?php $addInfoBlock = $block->getProductAdditionalInformationBlock(); ?>
        <?php if ($addInfoBlock) :?>
            <?= $addInfoBlock->setItem($_item)->toHtml() ?>
        <?php endif; ?>
        <?= $block->escapeHtml($_item->getDescription()) ?>
    </td>
    <td class="item-qty"><?= /* @escapeNotVerified */  $_item->getQtyOrdered() * 1 ?></td>
    <td class="item-price">
        <?= /* @escapeNotVerified */  $block->getItemPrice($_item) ?>
    </td>
</tr>

in my Magento_Sales/templates/email/items/order/default.phtml file

it's getting default base url.

1

3 Answers 3

1

You can get product object from $_item.

No need to load product again in default.phtml. Try below solution

<?= $_item->getproduct()->getProductUrl() ?>
0

You can set Store ID using this function setStoreId($_item->getStoreId()) like..

$_item = $block->getItem();
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$product = $_objectManager->get('Magento\Catalog\Model\Product')->load($_item->getProductId());
$product->setStoreId($_item->getStoreId());

echo $product->getProductUrl();

Hope this will help you!

8
  • Hi, I tried this method, by base url geting default url.. i need to get second website url
    – Priya
    Oct 31, 2019 at 9:20
  • Can you please update your code in your question ? Oct 31, 2019 at 9:59
  • I have updated. Please check above. Thank you
    – Priya
    Oct 31, 2019 at 10:05
  • Can you please set this line after product->load() ? $product->setStoreId($_item->getStoreId()); Oct 31, 2019 at 10:09
  • Yeah, i added . Stil it getting same base url.. Now i updated in my question.
    – Priya
    Oct 31, 2019 at 10:13
0

I tried may ways but i got only Default base url. So 1st i get store BaseUrl and and get UrlKey, then i combined.

$base_url = $_item->getStore(2)->getBaseUrl();
$product_key = $_item->getProduct()->getUrlKey(); 
echo $base_url . $product_key; 

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.