简体   繁体   English

WooCommerce 4.0 自定义结帐和电子邮件、管理员订单和感谢页面上的 ACF 字段值

[英]WooCommerce 4.0 custom checkout & ACF field value on email, admin order, and thank you page

I'm having a hard time printing my custom field value to the email notifications, order admin and thank you page.我很难将我的自定义字段值打印到电子邮件通知、订单管理和感谢页面。 I've browsed through StackOverflow, tried every single answer I found but unfortunately not working and I couldn't figure out the problem: I am trying to pass the value of the additional checkout field, it only prints the strong label with a blank value, and in the emails nothing shows, here is my code so far:我浏览了 StackOverflow,尝试了我找到的每一个答案,但不幸的是没有工作,我无法弄清楚问题:我正在尝试传递附加结帐字段的值,它只打印带有空白值的强标签,并且在电子邮件中没有显示任何内容,这是我目前的代码:

//new pickup location checkout field
add_action( 'woocommerce_before_order_notes', 'pickup_location_custom_checkout_field' );

function pickup_location_custom_checkout_field( $checkout ) {

    echo '<div><h3>' . __('Pick-up location') . '</h3>';

    woocommerce_form_field( 'pick_up_location', array(
        'type'          => 'text',
        'class'         => array('notes'),
        'label'         => __('<span style="color:red">[IMPORTANT]</span> Where should we meet you?'),
        'placeholder'   => __('Please enter your accomodation name or the nearest pick-up point if not accessible by car'),
        'required'     => true,
        ), $checkout->get_value( 'pick_up_location' ));

    echo '</div>';

}

// Save the pickup location data to the order meta
add_action( 'woocommerce_checkout_create_order', 'pickup_location_checkout_field_update_order_meta' );
function pickup_location_checkout_field_update_order_meta( $order_id ) {
    if (!empty($_POST['pick_up_location'])) {
        update_post_meta( $order_id, 'Pick-up location', sanitize_text_field( $_POST['pick_up_location']));
    }
}

// Display 'pickup location' on the order edit page (backend)
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'pickup_location_checkout_field_order_page', 10, 1 );
function pickup_location_checkout_field_order_page($order){
    global $post_id;
    $order = new WC_Order( $post_id );
    echo '<p><strong style="color:red">'.__('Pickup Location').':</strong> ' . get_post_meta($order->get_id(), '_pick_up_location', true ) . '</p>';

// Display 'pickup location' in "Order received" and "Order view" pages (frontend)
add_action( 'woocommerce_order_details_after_order_table', 'display_client_pickup_data_in_orders', 10 );
function display_client_pickup_data_in_orders( $order ) {
    global $post_id;
    $order = new WC_Order( $post_id );
    echo '<p><strong style="color:red">'.__('Pickup Location').':</strong> ' . get_post_meta($order->get_id(), '_pick_up_location', true ) . '</p>';
}

// Display 'pickup location data' in Email notifications
add_filter( 'woocommerce_email_order_meta_fields', 'display_client_pickup_data_in_emails', 10, 3 );

function display_client_pickup_data_in_emails( $fields, $sent_to_admin, $order ) {
    $fields['Pickup Location'] = array(
        'label' => __( 'Pickup Location' ),
        'value' => get_post_meta( $order->get_id(), 'pick_up_location', true ),
    );
    return $fields;
}

No matter what I try, the code only prints the label without any value from the checkout form.无论我尝试什么,代码都只会打印标签,而没有任何结帐表单中的值。 I know this question has been asked many times, but I tried every single answer for over 6 days without any luck.我知道这个问题已经被问过很多次了,但是我尝试了超过 6 天的每一个答案都没有任何运气。 I also need to mention that I am using Woocommerce Bookings in this project.我还需要提到我在这个项目中使用了 Woocommerce Bookings。 Thanks for your help谢谢你的帮助


[Update:] Saving and displaying ACF field in the cart, admin order, customer order, checkout, and emails. [更新:]在购物车、管理订单、客户订单、结帐和电子邮件中保存和显示 ACF 字段。 Thanks to @7uc1f3r for the detailed explanation, his answer helped me to display the ACF field in a similar way, it is also based on This answer from @LoicTheAztec.感谢@7uc1f3r 的详细解释,他的回答帮助我以类似的方式显示 ACF 字段,它也基于@LoicTheAztec 的这个回答

Displaying custom field on the product page above ATC:在 ATC 上方的产品页面上显示自定义字段:

// Displaying Pick-up time custom field value in single product page
add_action( 'woocommerce_before_add_to_cart_button', 'add_pickup_time_custom_field', 0 );
function add_pickup_time_custom_field() {
    global $product;
    //(compatibility with WC +3) 
    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

    echo "<div class='pickup-time-atc'>";
    echo "<span>Pick-up time: </span>";
    echo get_field( 'pick_up_time', $product_id );
    echo "</div>";

    return true;
}

Displaying custom field value in single product page在单个产品页面中显示自定义字段值

Saving Pick-up time custom field into cart and session将取货时间自定义字段保存到购物车和会话中

// Saving Pick-up time custom field into cart and session
add_filter( 'woocommerce_add_cart_item_data', 'save_pickup_time_custom_field', 10, 2 );
function save_pickup_time_custom_field( $cart_item_data, $product_id ) {
    $custom_field_value = get_field( 'pick_up_time', $product_id, true );
    if( !empty( $custom_field_value ) ) 
    {
        $cart_item_data['pick_up_time'] = $custom_field_value;
    }
    return $cart_item_data;
}

Render Pick-up time custom field meta on cart and checkout在购物车和结帐上渲染取货时间自定义字段元

// Render Pick-up time meta on cart and checkout
add_filter( 'woocommerce_get_item_data', 'render_pickuptime_meta_on_cart_and_checkout', 10, 2 );
function render_pickuptime_meta_on_cart_and_checkout( $cart_data, $cart_item ) {
    $custom_items = array();
    // Woo 2.4.2 updates
    if( !empty( $cart_data ) ) {
        $custom_items = $cart_data;
    }
    if( isset( $cart_item['pick_up_time'] ) ) {
        $custom_items[] = array( "name" => "Pickup time", "value" => $cart_item['pick_up_time'] );
    }
    return $custom_items;
}

Render custom field meta on cart and checkout在购物车和结帐上呈现自定义字段元

Add custom field meta to order admin details添加自定义字段元以订购管理员详细信息

// Add pickup time custom field meta to order admin
add_action( 'woocommerce_checkout_create_order_line_item', 'pickuptime_checkout_create_order_line_item', 10, 4 );
function pickuptime_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
  if( isset( $values['pick_up_time'] ) ) {
    $item->add_meta_data(
      __( 'Pickup time' ),
      $values['pick_up_time'],
      true
    );
  }
}

Add custom field meta to order admin details添加自定义字段元以订购管理员详细信息

Add pickup time custom field meta to emails将取件时间自定义字段元添加到电子邮件

// Add pickup time custom field meta to emails
add_filter( 'woocommerce_order_item_name', 'pickuptime_order_item_emails', 10, 2 );
function pickuptime_order_item_emails( $product_name, $item ) {
  if( isset( $item['pick_up_time'] ) ) {
    $product_name .= sprintf(
      '<ul"><li>%s: %s</li></ul>',
      __( '<span style="color:red !important">Pickup time</span>' ),
      esc_html( $item['pick_up_time'] )
     ); 
  }
  return $product_name;
}

Add pickup time custom field meta to emails将取件时间自定义字段元添加到电子邮件

Please comment if you see any errors or ways to improve, Thanks.如果您发现任何错误或改进方法,请发表评论,谢谢。

Woocommerce 4.0.0 and Wordpress 5.3.2 Woocommerce 4.0.0 和 Wordpress 5.3.2

Go through your code step by step逐步完成您的代码

update_post_meta( $order_id, 'Pick-up location', sanitize_text_field( $_POST['pick_up_location']));
echo '<p><strong style="color:red">'.__('Pickup Location').':</strong> ' . get_post_meta($order->get_id(), '_pick_up_location', true ) . '</p>';
'value' => get_post_meta( $order->get_id(), 'pick_up_location', true ),

You use Pick-up location , _pick_up_location & pick_up_location as meta_key while this should be 3x the same value.您使用Pick-up location_pick_up_locationpick_up_location作为meta_key而这应该是相同值的 3 倍。

You also miss a closing tag by pickup_location_checkout_field_order_page function.您还错过了pickup_location_checkout_field_order_page函数的结束标记。

You also use wrong parameters with some hooks, etc..您还对某些钩子等使用了错误的参数。

Try this instead试试这个

//new pickup location checkout field
add_action( 'woocommerce_before_order_notes', 'pickup_location_custom_checkout_field' );
function pickup_location_custom_checkout_field( $checkout ) {
    echo '<div><h3>' . __('Pick-up location') . '</h3>';

    woocommerce_form_field( 'pick_up_location', array(
        'type'          => 'text',
        'class'         => array('notes'),
        'label'         => __('<span style="color:red">[IMPORTANT]</span> Where should we meet you?'),
        'placeholder'   => __('Please enter your accomodation name or the nearest pick-up point if not accessible by car'),
        'required'     => true,
        ), $checkout->get_value( 'pick_up_location' ));

    echo '</div>';
}

// Save the pickup location data to the order meta
add_action( 'woocommerce_checkout_create_order', 'pickup_location_checkout_field_update_order_meta', 10, 2 );
function pickup_location_checkout_field_update_order_meta( $order, $data ) {    
    if ( !empty( $_POST['pick_up_location']) ) {
        $order->update_meta_data( '_pick_up_location', sanitize_text_field( $_POST['pick_up_location'] ) ); // Order meta data
    }
}

// Display 'pickup location' on the order edit page (backend)
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'pickup_location_checkout_field_order_page', 10, 1 );
function pickup_location_checkout_field_order_page( $order ) {
    $pick_up_location = $order->get_meta( '_pick_up_location' );
    echo '<p><strong style="color:red">'.__('Pickup Location').':</strong> ' . $pick_up_location . '</p>';
}

// Display 'pickup location' in "Order received" and "Order view" pages (frontend)
add_action( 'woocommerce_order_details_after_order_table', 'display_client_pickup_data_in_orders', 10 );
function display_client_pickup_data_in_orders( $order ) {
    $pick_up_location = $order->get_meta( '_pick_up_location' );
    echo '<p><strong style="color:red">'.__('Pickup Location').':</strong> ' . $pick_up_location . '</p>';
}

// Display 'pickup location data' in Email notifications
add_filter( 'woocommerce_email_order_meta_fields', 'display_client_pickup_data_in_emails', 10, 3 );
function display_client_pickup_data_in_emails( $fields, $sent_to_admin, $order ) {
    $fields['Pickup Location'] = array(
        'label' => __( 'Pickup Location' ),
        'value' => $order->get_meta( '_pick_up_location' ),
    );
    return $fields;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Output WooCommerce 订单管理页面 (ACF) 中的产品自定义字段 - Output a product custom field in WooCommerce Order Admin Page (ACF) 在 WooCommerce 订单管理页面 (ACF) 显示产品自定义字段 - Display product custom field in WooCommerce Order admin Page (ACF) 在 Woocommerce 管理订单页面中显示自定义结帐字段值 - Displaying custom checkout field value in Woocommerce admin order pages 在 Woocommerce 的管理订单详细信息部分显示自定义结帐字段值 - Display custom checkout field value on admin order detail section in Woocommerce 在Woocommerce管理员订单列表自定义列中显示结帐字段值 - Display checkout field value in Woocommerce admin order list custom column Woocommerce(感谢页面)在订单概览字段中添加自定义元键 - Woocommerce (thank you page) add custom meta_key in order overview field 在自定义感谢页面上显示 Woocommerce 订单详细信息 - Show Woocommerce order details on custom thank you page 在“谢谢”页面上通过AJAX创建WooCommerce自定义订单元 - Create WooCommerce custom Order meta via AJAX on thank you page 将自定义字段 (ACF) 添加到 WooCommerce 完成订单 email 通知 - Add custom field (ACF) to WooCommerce completed order email notification Woocommerce在“谢谢”页面上获得自定义选择字段值 - Woocommerce get custom select fields value at thank you page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM