自定义Woocommerce电子邮件:获取产品缩略图和价格

时间:2019-02-19 13:30:56

标签: php wordpress object woocommerce product

我最近刚刚为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代码时,一切正常(没有明显收取价格)

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

1 个答案:

答案 0 :(得分:0)

自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(); 
?>

现在应该更好地工作。

相关:Get Order items and WC_Order_Item_Product in Woocommerce 3

相关问题