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
Solved! Go to Solution.
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(); }
Have you tried with $block->getShipment(); ?
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(); }
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:
<!-- 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.