Home > Blockchain >  Woocommerce: get post meta and put only in new order email
Woocommerce: get post meta and put only in new order email

Time:11-18

i want to get a post meta (_stock_sede) and put in the Woocommerce new order email for the admin only, I entered the code below but it is giving errors, can someone point me to what I am doing wrong? :)

// ----------
// STEP 1: DECLARE EMAIL ID GLOBAL
 
add_action( 'woocommerce_email_before_order_table', 'email_id_as_global', 9999, 4 );
 
function email_id_as_global( $order, $sent_to_admin, $plain_text, $email ){
    $GLOBALS['email_id'] = $email->id;
}
 
// ----------
// STEP 2: IF NEW ORDER EMAIL, ADD REMAINING STOCK QUANTITY

add_filter( 'woocommerce_email_order_item_quantity', 'item_remaining_stock', 9999, 2 );
 
function item_remaining_stock( $qty_display, $item ) {
    $email_id = $GLOBALS['email_id'];
    if ( empty( $email_id ) ) return $qty_display;
    if ( 'new_order' !== $email_id ) return $qty_display;
    $product = $item->get_product();
    if ( is_object( $product ) && $product->managing_stock() ) {
       $qty_display .= ' (' . $product->get_post_meta( $post->ID, '_stock_sede', true ) . ' remaining)';
    }
    return $qty_display;
}

----------UPDATE I got what I wanted with this code, however I get the error below in the cancelled order emails, any hints on what it could be?

// ----------
// STEP 1: DECLARE EMAIL ID GLOBAL
 
add_action( 'woocommerce_email_before_order_table', 'email_id_as_global', 9999, 4 );
 
function email_id_as_global( $order, $sent_to_admin, $plain_text, $email ){
    $GLOBALS['email_id'] = $email->id;
}
 
// ----------
// STEP 2: IF NEW ORDER EMAIL, ADD REMAINING STOCK QUANTITY

add_filter( 'woocommerce_email_order_item_quantity', 'item_remaining_stock', 9999, 2 );
 
function item_remaining_stock( $qty_display, $item ) {
 global $post, $product;
    $email_id = $GLOBALS['email_id'];
    if ( empty( $email_id ) ) return $qty_display;
    if ( 'new_order' !== $email_id ) return $qty_display;
    $product = $item->get_product();
    if ( is_object( $product ) && $product->managing_stock() ) {
       $qty_display .= ' (' . $product->get_meta('_stock_sede') . ' em armazém)';
    }
    return $qty_display;
}

Error when sending cancelled order emails

CRITICAL Uncaught ArgumentCountError: Too few arguments to function email_id_as_global(), 3 passed in /home/autobrin/public_html/wp-includes/class-wp-hook.php on line 308 and exactly 4 expected in /home/autobrin/public_html/wp-content/themes/autobrinca/functions.php:798 Stack trace: #0 /home/autobrin/public_html/wp-includes/class-wp-hook.php(308): email_id_as_global(Object(Automattic\WooCommerce\Admin\Overrides\Order), true, false) #1 /home/autobrin/public_html/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters(NULL, Array) #2 /home/autobrin/public_html/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #3 /home/autobrin/public_html/wp-content/plugins/woocommerce-pretty-emails/emails/admin-cancelled-order.php(25): do_action('woocommerce_ema...', Object(Automattic\WooCommerce\Admin\Overrides\Order), true, false) #4 /home/autobrin/public_html/wp-content/plugins/woocommerce/includes/wc-core-functions.php(345): include('/home/autobrin/...') #5 /home/autobrin/public_html/wp-content/plugins/woocommerce/includes/ em /home/autobrin/public_html/wp-content/themes/autobrinca/functions.php na linha 798

CodePudding user response:

This is your hint " Too few arguments to function email_id_as_global(), 3 passed and exactly 4 expected"

So this means one of your input vars does not have a value.

Try to declare your input vars with a default value and maybe add the var type. Something like this

function email_id_as_global( $order = array(), $sent_to_admin = null, $plain_text = '', $email = null )
  • Related