cancel
Showing results for 
Search instead for 
Did you mean: 

Get Child Product Name Shipment Email

SOLVED

Get Child Product Name Shipment Email

I am trying to retrieve the Child Item Name of a Configurable Product in the Shipment email.

 

I have overwritten the default template file in my theme at: app/code/design/MyTheme/theme/Magento_Sales/templates/email/items/order/default.phtml

 

I did this in the Order Confirmation email using $product = $_item->getProduct(). I'm unable to use this in the shipment email.

This snippet is how I retrieve Child Product Details in my Order Confirmation Template:

 

$_item = $block->getItem();
$_order = $_item->getOrder();
$product = $_item->getProduct();

// Normal Products
$productId = $product->getId();
$productName = $product->getName();
$productUrl = $product->getProductUrl();
$productSku = $product->getSku();

// Configurable Child Item Details
  foreach($_item->getChildrenItems() as $item){
    $product = $item->getProduct();
    $productId = $product->getId();
    $productName = $product->getName();
    $productUrl = $product->getProductUrl();
    $productSku = $product->getSku();
  }

 

This does not work in the Shipment email. How would I get the Child Product Name for the Shipment email?

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Get Child Product Name Shipment Email

The solution for me was to get the product using getOrderItem();

I then retrieve the Child Items from that using getChildrenItems();

 

$_item = $block->getItem();
$productId = $_item->getId();
$productName = $_item->getName();
$productUrl = $_item->getProductUrl();
$productSku = $_item->getSku();

// Configurable Child Items
// Get Order Item
  foreach($_item->getOrderItem()->getChildrenItems() as $orderItem){
    $productId = $orderItem->getId();
    $productName = $orderItem->getName();
    $productUrl = $orderItem->getProductUrl();
    $productSku = $orderItem->getSku();
  }

View solution in original post

3 REPLIES 3

Re: Get Child Product Name Shipment Email

Have you tried with $block->getShipment(); ?

Founder at https://agency418.com

Re: Get Child Product Name Shipment Email

The solution for me was to get the product using getOrderItem();

I then retrieve the Child Items from that using getChildrenItems();

 

$_item = $block->getItem();
$productId = $_item->getId();
$productName = $_item->getName();
$productUrl = $_item->getProductUrl();
$productSku = $_item->getSku();

// Configurable Child Items
// Get Order Item
  foreach($_item->getOrderItem()->getChildrenItems() as $orderItem){
    $productId = $orderItem->getId();
    $productName = $orderItem->getName();
    $productUrl = $orderItem->getProductUrl();
    $productSku = $orderItem->getSku();
  }

Re: Get Child Product Name Shipment Email

hI @christopher_oliver,

 

To include the child product names in the shipment email in Magento 2, you'll need to customize the email template to fetch and display the names of the child products associated with the shipment. Here's a general outline of how you can achieve this:

 

  • Override Shipment Email Template: Create a custom module or theme in Magento and override the shipment email template. The default template file for the shipment email is located at
    Spoiler
    vendor/magento/module-sales/view/frontend/email/shipment_new.html
  • Fetch Child Product Names: In the overridden template file, retrieve the child product names associated with the shipment. You'll need to loop through the items in the shipment and fetch the names of the child products. 
<!-- File: app/design/frontend/YourVendor/YourTheme/Magento_Sales/email/shipment_new.html -->
<!-- Override the default shipment email template -->
<!-- Your custom header and intro message here -->
{{layout handle="sales_email_order_shipment_items" shipment=$shipment order=$order}}
<!-- Loop through each item in the shipment -->
{{block class="Magento\Framework\View\Element\Template" area="frontend" template="YourVendor_YourTheme::email/items.phtml" shipment=$shipment}}
<!-- Your custom footer message here -->

 

In the items.phtml template file (app/design/frontend/YourVendor/YourTheme/templates/email/items.phtml), you can include the logic to fetch and display the child product names:

 

<?php
/** @var \Magento\Sales\Model\Order\Shipment $shipment */
$shipment = $block->getShipment();
?>

<?php foreach ($shipment->getAllItems() as $item): ?>
    <?php foreach ($item->getChildrenItems() as $childItem): ?>
        <!-- Display the name of the child product -->
        <p><?= $childItem->getName() ?></p>
    <?php endforeach; ?>
<?php endforeach; ?>

This code iterates through each item in the shipment and then through each child item associated with it. It displays the name of each child product in the shipment email.

 

If the issue will be resolved, Click Kudos & Accept as a Solution.