Woocommerce - 在 function.php 中添加 get_price 不起作用

问题描述 投票:0回答:1

我创建了一个代码来在特定产品的电子邮件中添加文本。 我想检索产品的价格,但不起作用。 订购时出现错误“处理订单时出错。请重试。”

这是代码:

add_action( 'woocommerce_email_before_order_table', 'esprit_add_content_specific_email', 20, 4 );
function esprit_add_content_specific_email( $order, $sent_to_admin, $plain_text, $email ) {
    global $woocommerce;
    $items = $order->get_items(); 
    foreach ( $items as $item ) {
    $product_id = $item['product_id'];
    $price = $product_id->get_price();
    if ( $product_id == 3550 ) {
        echo 'Message uniquement sur la carte cadeaux libre.';
        echo "id";
        echo $product_id;
        echo "prix";
        echo $price;
        }
    }
}

我设法检索产品 ID,但没有检索到“get_price()”。

请对我有任何帮助。抱歉我的英语:-)

php wordpress woocommerce product
1个回答
1
投票

$product_id
变量看起来像是一个整数,它没有任何方法。相反,请尝试使用
wc_get_product( $product_id )->get_price()

未经测试:

function esprit_add_content_specific_email( $order ) {
    $items = $order->get_items();

    foreach ( $items as $item ) {
        if ( 3550 !== $item['product_id'] ) {
            continue;
        }

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

        if ( empty( $product ) ) {
            continue;
        }

        echo 'Message uniquement sur la carte cadeaux libre.';
        printf( 'id %d prix %s', $item['product_id'], $product->get_price() );
    }
}
add_action( 'woocommerce_email_before_order_table', 'esprit_add_content_specific_email', 20 );
© www.soinside.com 2019 - 2024. All rights reserved.