0

From my magento2 backend i created 2 website. 1st default website xxxx.com 2nd website is yyyy.com. In my sales order email template, added

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

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

When i place order from 2nd website (i.e.,) yyyy.com, product Base URL getting xxxx.com

Please let me know how to get base URL for multiwebsite sales email template.

2
  • Could you please specify where exactly you added the code above?
    – underser
    Commented Oct 30, 2019 at 11:59
  • in my child them/Magento_Sales/templates/email/items/order/default.phtml
    – Priya
    Commented Oct 30, 2019 at 12:06

4 Answers 4

0

try this.

$product = $_objectManager->get('Magento\Catalog\Model\Product')->setStoreId($your_store_id)->load($_item->getProductId());
3
  • i tried this too. still geting same base_url. For my clarification let me know how to find "$your_store_id" ?
    – Priya
    Commented Oct 30, 2019 at 12:35
  • rakeshjesadiya.com/… follow this to get default store id based on website id. Commented Oct 30, 2019 at 12:39
  • i had tried this, but still getting default Base URL
    – Priya
    Commented Oct 31, 2019 at 12:06
0

Access your DB and browse your store table. you will get the complete picture what store do you want.

Then Try this:

$product = $_objectManager->get('Magento\Catalog\Model\Product')->setStoreId($your_store_id)->load($_item->getProductId());
1
  • i tried above method, still geting default base_url
    – Priya
    Commented Oct 31, 2019 at 6:37
0

In case that you are using template from Magento_Sales/templates/email/items/order/default.html

You can simply do:

$_item->getProduct()->getProductUrl()

this works fine in my fresh Magento 2.3.3

Note from me: Please don't use Object Manager, especially in templates.

8
  • Please let me know, why you are saying not to use "Object Manager"?
    – Priya
    Commented Oct 31, 2019 at 10:23
  • @Priya you can read about this in magento.stackexchange.com/questions/117098/…
    – underser
    Commented Oct 31, 2019 at 10:54
  • @Priya does my answer help you?
    – underser
    Commented Oct 31, 2019 at 12:07
  • Yeah, Thank you
    – Priya
    Commented Oct 31, 2019 at 12:08
  • But for my Question i did not get answer.
    – Priya
    Commented Oct 31, 2019 at 12:10
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 and acknowledge you have read our privacy policy.

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