0

I want the original child price in the below file.

app\design\frontend\MyVendor\MyTheme\Magento_Sales\templates\email\items\order\default.phtml

I try $block->getBasePrice($_item) or only $block->getPrice($_item) but is empty or price of parent product

2 Answers 2

0

This $_item in your code refers to the configurable product object. You need to fetch the children's data from the configurable product object. Can you try with the following code?

$_children = $_item->getTypeInstance()->getUsedProducts($_item);
foreach ($_children as $child){
    echo "Child Price :".$child->getPrice();// This line print the child price.
}

//You can print the $_item object using the test.log file as below.

$writer = new \Laminas\Log\Writer\Stream(BP . '/var/log/test.log');
$logger = new \Laminas\Log\Logger();
$logger->addWriter($writer);
$logger->info('Printing Item Data...');
$logger->info(print_r($_item->getData(),true));
6
  • Hi, I have updated my answer. Please check now. Commented Feb 2, 2023 at 15:45
  • Hi, I get this error "Error: Call to a member function getUsedProducts() on null"
    – yavonz15
    Commented Feb 2, 2023 at 15:56
  • Can you check the data for $_item using var_dump($_item->getData()); Commented Feb 2, 2023 at 16:00
  • 1
    I have added the logger code in my answer which creates the test log file in the var/log/ folder. Comment above code & place only logger code & check. Commented Feb 2, 2023 at 16:20
  • 1
    Laminas wouldn't let me print the log, I used the basic magento one and was able to. I will write the correct answer shortly. Thank you very much you have opened my mind.
    – yavonz15
    Commented Feb 3, 2023 at 9:14
0

Thanks to user Jitendra Patel, I tried a new approach and succeeded.

Here's how to do it

Print the getData() of the $_item object

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$logger = $objectManager->create(\Psr\Log\LoggerInterface::class);
$logger->debug('Order item data', [$_item->getData()]);

By doing so in debug.log you will have all the getData() of $_item

[
  {
    "item_id": "377",
    "order_id": "136",
    "parent_item_id": null,
    "quote_item_id": "4526",
    "store_id": "1",
    "created_at": "2023-02-03 08:36:38",
    "updated_at": "2023-02-03 08:36:38",
    "product_id": "40088",
    "product_type": "configurable",
    "product_options": {
      "info_buyRequest": {
        "uenc": "aHR0cDovL3N0YWdpbmcuc2lvbGEuY29tL2l0L2ZlbHBhLWNvbi1zdGFtcGEtZnJvbnRhbGUtODA0MTIxNmExMjE2Lmh0bWw,",
        "product": "40088",
        "selected_configurable_option": "",
        "related_product": "",
        "item": "40088",
        "super_attribute": {
          "143": "663"
        },
        "qty": "1"
      },
      "attributes_info": [
        {
          "label": "Taglia",
          "value": "6M",
          "option_id": 143,
          "option_value": "663"
        }
      ],
      "simple_name": "Felpa con stampa frontale-6M",
      "simple_sku": "2500016839246",
      "product_calculations": 1,
      "shipment_type": 0
    },
    "weight": null,
    "is_virtual": "0",
    "sku": "2500016839246",
    "name": "Felpa con stampa frontale",
    "description": null,
    "applied_rule_ids": null,
    "additional_data": null,
    "is_qty_decimal": "0",
    "no_discount": "0",
    "qty_backordered": null,
    "qty_canceled": "0.0000",
    "qty_invoiced": "0.0000",
    "qty_ordered": "1.0000",
    "qty_refunded": "0.0000",
    "qty_shipped": "0.0000",
    "base_cost": null,
    "price": "184.0000",
    "base_price": "184.0000",
    "original_price": "230.0000",
    "base_original_price": "230.0000",
    "tax_percent": "0.0000",
    "tax_amount": "0.0000",
    "base_tax_amount": "0.0000",
    "tax_invoiced": "0.0000",
    "base_tax_invoiced": "0.0000",
    "discount_percent": "0.0000",
    "discount_amount": "0.0000",
    "base_discount_amount": "0.0000",
    "discount_invoiced": "0.0000",
    "base_discount_invoiced": "0.0000",
    "amount_refunded": "0.0000",
    "base_amount_refunded": "0.0000",
    "row_total": "184.0000",
    "base_row_total": "184.0000",
    "row_invoiced": "0.0000",
    "base_row_invoiced": "0.0000",
    "row_weight": "0.0000",
    "base_tax_before_discount": null,
    "tax_before_discount": null,
    "ext_order_item_id": null,
    "locked_do_invoice": null,
    "locked_do_ship": null,
    "price_incl_tax": "184.0000",
    "base_price_incl_tax": "184.0000",
    "row_total_incl_tax": "184.0000",
    "base_row_total_incl_tax": "184.0000",
    "discount_tax_compensation_amount": "0.0000",
    "base_discount_tax_compensation_amount": "0.0000",
    "discount_tax_compensation_invoiced": null,
    "base_discount_tax_compensation_invoiced": null,
    "discount_tax_compensation_refunded": null,
    "base_discount_tax_compensation_refunded": null,
    "tax_canceled": null,
    "discount_tax_compensation_canceled": null,
    "tax_refunded": null,
    "base_tax_refunded": null,
    "discount_refunded": null,
    "base_discount_refunded": null,
    "weee_tax_applied": null,
    "weee_tax_applied_amount": null,
    "weee_tax_applied_row_amount": null,
    "weee_tax_disposition": null,
    "weee_tax_row_disposition": null,
    "base_weee_tax_applied_amount": null,
    "base_weee_tax_applied_row_amnt": null,
    "base_weee_tax_disposition": null,
    "base_weee_tax_row_disposition": null,
    "gift_message_id": null,
    "gift_message_available": "0",
    "free_shipping": "0",
    "has_children": true,
    "product_option": "[object] (Magento\\Catalog\\Model\\ProductOption: {})",
    "wrap_items": [],
    "extension_attributes": "[object] (Magento\\Sales\\Api\\Data\\OrderItemExtension: {})",
    "product": "[object] (Magento\\Catalog\\Model\\Product\\Interceptor: {})"
  }
]

Finally <?= /* @escapeNotVerified */ $_item->getBaseOriginalPrice() ?> to get the original price of the child product

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.