Display product ACF fields on a specific WooCommerce email

為{幸葍}努か 提交于 2021-01-28 13:54:05

问题


So I almost got it, but this last detail of specifying WooCommerce emails is leaving my head in a bit of a knot here.

I need to show ACF (advanced custom fields) fields of products (in this case it's a custom shipping time)

  • But only in the new order email (processing order and waiting payment order sent to the client) and then new order email to admin.

This was the main way I found: "Display product ACF field value in Woocommerce transaction emails" Thank you in advance @LoicTheAztec

I also added some conditional settings to it (mind me my PHP is very beginning copy-pasty) which are working pretty well.

However what I can't get around is making it work only on new order emails. I have it setup like this, and it works, however it shows on all emails that contain the email order details, and I can't have the shipping time showing on the completed order emails as it will create confusion.

// Display product ACF custom field value shipping in email notification
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
    // Targeting email notifications only
    if ( 'new_order' == $email->id )   
        return $item_name;

    // Get the WC_Product object (from order item)
    $product = $item->get_product();
    $othershipping = get_field( 'shipping_custom', $product->get_id());

    if( $shpng_value = get_field('shipping_', $product->get_id())== "24h") {
        $item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
        <strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . '<p>Get it tomorrow(24h)</p>' . '</p>';
    }
    elseif( $shpng_value = get_field('shipping_', $product->get_id())== "2-5 days") {
        $item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
        <strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . '<p>2-5 days</p>' . '</p>';
    }
    elseif( $shpng_value = get_field('shipping_', $product->get_id())== "other") {
        $item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
        <strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . $othershipping . '</p>';
    }

    return $item_name;
}

I have tried switching the

if ( 'new_order' == $email->id ) 

to

if ( 'new_order' != $email->id ) 

But that just makes it not work anywhere.


I also thought it could be this part

function custom_order_item_name( $item_name, $item ) {

Where I need to add ($order, $sent_to_admin, $plain_text, $email )

function custom_order_item_name( $item_name, $item, $order, $sent_to_admin, $plain_text, $email ) 

But it makes the email return an error.


回答1:


Normally this should work with the code below

Note: my answer is largely based on: "Customize order item meta only for WooCommerce admin email notifications". CREDITS: @Loictheaztec so don't forget to upvote that answer to!

// Setting the "sent_to_admin" as a global variable
function email_order_id_as_a_global($order, $sent_to_admin, $plain_text, $email) {
    $GLOBALS['email_data'] = array(
        'sent_to_admin' => $sent_to_admin, // <== HERE we set "$sent_to_admin" value
        'email_id' => $email->id, // The email ID (to target specific email notification)
    );
}
add_action('woocommerce_email_before_order_table', 'email_order_id_as_a_global', 1, 4);

function custom_order_item_name( $item_name, $item ) {
    if ( ! is_wc_endpoint_url() && $item->is_type('line_item') ) {

        // Getting the custom 'email_data' global variable
        $refNameGlobalsVar = $GLOBALS;
        $email_data = $refNameGlobalsVar['email_data'];

        // Only for new order
        if( is_array( $email_data ) && $email_data['email_id'] == 'new_order' ) {

            // Get the WC_Product object (from order item)
            $product = $item->get_product();

            $othershipping = get_field( 'shipping_custom', $product->get_id());

            if( $shpng_value = get_field('shipping_', $product->get_id())== "24h") {
                $item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
                <strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . '<p>Get it tomorrow(24h)</p>' . '</p>';
            }
            elseif( $shpng_value = get_field('shipping_', $product->get_id())== "2-5 days") {
                $item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
                <strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . '<p>2-5 days</p>' . '</p>';
            }
            elseif( $shpng_value = get_field('shipping_', $product->get_id())== "other") {
                $item_name .= '<br><p class="item-shpng" style="margin:12px 0 0;">
                <strong>' . __( 'Shipping time', 'woocommerce' ) . ': </strong>' . $othershipping . '</p>';
            }
        }
    }

    return $item_name;
}
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );


来源:https://stackoverflow.com/questions/61826893/display-product-acf-fields-on-a-specific-woocommerce-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!