skip to Main Content

I created a code to add text in an email for a specific product.
I want to retrieve the price of the product but it does not work.
It gives me an error when ordering "There was an error processing the order. Please try again."

Here is the code :

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;
        }
    }
}

I manage to retrieve the product id but not the "get_price()".

Please, any help for me. Sorry for my english 🙂

2

Answers


  1. The $product_id variable looks like it’s an integer, which doesn’t have any methods on it. Rather, try using wc_get_product( $product_id )->get_price().

    Untested:

    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 );
    
    Login or Signup to reply.
  2. There are some mistakes in your code and unnecessary things.

    • global $woocommerce; is not needed
    • $item[‘product_id’] is obsolete since WooCommerce version 3
    • You can’t use the WC_Product method get_price() using the product ID variable, as it’s not the WC_Product object.

    So the correct revisited code is:

    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 ) {
    
        foreach ( $order->get_items() as $item ) {
            $product      = $item->get_product(); // Get the WC_Product object
            $product_id   = $product->get_id(); // Get The product ID
            $product_name = $product->get_name(); // Get The product name
            $price        = $product->get_price(); // Product raw price
            $price_html   = $product->get_price_html(); // Html formatted price with currency
    
            if ($product_id == 3550 ) {
                echo '<p>';
                echo '<p>Message uniquement sur la carte cadeaux libre.<br>';
                echo 'Nom du produit : ' . $product_name . '<br>';
                echo 'Id du produit : ' . $product_id . '<br>';
                echo 'Prix : ' . strip_tags($price_html);
                echo '</p>';
            }
        }
    }
    

    Tested and works.

    Related: Get Order items and WC_Order_Item_Product in WooCommerce 3

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search