首页 > 解决方案 > 自定义 Woocommerce 电子邮件:获取产品缩略图和价格

问题描述

我最近刚刚为 Woocommerce 创建了自定义电子邮件。除了一个问题,我一切正常。在我带来/获取“产品图片”之前,我能够毫无问题地加载“产品价格”。

但是现在我正在获取产品图片,我无法获取“产品价格”并且订单未处理和完成。

这是我用来收集要在电子邮件中显示的所有信息的 PHP 代码。

<?php               
    foreach ($order->get_items() as $theitem_id => $theitem ) { 

    // PRODUCT NAME
    $product_name = $theitem->get_name(); 
    // PRODUCT QUANTITY
    $quantity = $theitem->get_quantity();  


    // LINE ITEM SUBTOTAL (Non discounted)
    $item_subtotal = $theitem->get_subtotal();
    $item_subtotal = number_format( $item_subtotal, 2 );

    // LINE ITEM TOTAL (discounted)
    $item_total = $theitem->get_total();
    $item_total = number_format( $item_total, 2 );


    $product_id = $theitem['product_id'];
    $product = wc_get_product( $product_id );

    // PRODUCT IMAGE
    $prodimage = $product->get_image( array( 200, 200 ) )

    // PRODUCT PRICE
    $product_price = $product->get_price(); 
?>

我敢肯定,当我注释掉“产品价格”PHP 代码时,我离正确的做法不远了,然后一切正常(显然没有收集到价格)

任何帮助都会很棒,非常感谢

标签: phpwordpressobjectwoocommerceproduct

解决方案


从 Woocommerce 3 开始,产品 ID 是错误 的(产品对象也是错误的) ......尝试改为:

<?php
    // Loop through order items
    foreach ($order->get_items() as $item_id => $item ) { 

    // PRODUCT NAME
    $product_name = $item->get_name(); 
    // PRODUCT QUANTITY
    $quantity = $item->get_quantity();  

    // LINE ITEM SUBTOTAL (Non discounted)
    $item_subtotal = $item->get_subtotal();
    $item_subtotal = number_format( $item_subtotal, 2 );

    // LINE ITEM TOTAL (discounted)
    $item_total = $item->get_total();
    $item_total = number_format( $item_total, 2 );

    // Get an instance of the product Object
    $product = $item->get_product(); // <==== HERE
    $product_id = $product->get_id(); // <==== and HERE

    // PRODUCT IMAGE
    $product_image = $product->get_image( array( 200, 200 ) );

    // PRODUCT PRICE
    $product_price = $product->get_price(); 
?>

它现在应该更好地工作。

相关:在 Woocommerce 3 中获取订单商品和 WC_Order_Item_Product


推荐阅读