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

Posted

技术标签:

【中文标题】WooCommerce 4.0 电子邮件、管理员订单和感谢页面上的自定义结帐和 ACF 字段值【英文标题】:WooCommerce 4.0 custom checkout & ACF field value on email, admin order, and thank you page 【发布时间】:2020-06-25 22:56:38 【问题描述】:

我很难将我的自定义字段值打印到电子邮件通知、订单管理和感谢页面。我浏览了 ***,尝试了我找到的每一个答案,但不幸的是没有工作,我无法找出问题所在: 我正在尝试传递附加结帐字段的值,它只打印带有空白值的强标签,并且在电子邮件中没有任何显示,这是我到目前为止的代码:

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


无论我尝试什么,代码只会打印结帐表单中没有任何值的标签。 我知道这个问题已经被问过很多次了,但是我尝试了每一个答案超过 6 天,但没有任何运气。我还需要提到我在这个项目中使用 Woocommerce Bookings。 感谢您的帮助


[更新:] 在购物车、管理订单、客户订单、结帐和电子邮件中保存和显示 ACF 字段。 感谢@7uc1f3r的详细解释,他的回答帮助我以类似的方式显示ACF字段,它也是基于@LoicTheAztec的This answer。

在 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
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 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 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_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

如果您发现任何错误或改进方法,请发表评论, 谢谢。

Woocommerce 4.0.0 和 Wordpress 5.3.2

【问题讨论】:

【参考方案1】:

逐步检查您的代码

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 ),

您使用Pick-up location_pick_up_locationpick_up_location 作为meta_key 而这应该是相同值的 3 倍。

您还错过了pickup_location_checkout_field_order_page 函数的结束标记。

你还使用了错误的参数和一些钩子等。

试试这个

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

【讨论】:

非常感谢@7uc1f3r,你的代码工作起来很有魅力,我会尝试从你上面提到的错误中学习:),因为它现在正在工作,我还想打印一个 ACF 自定义字段从订单中的产品来看,它是一组6个中的一个字段,字段标签:“Pick UP Time”,字段名称:“pick_up_time”,我需要编辑挂钩中的任何内容吗?我将在这里发布我的代码。谢谢

以上是关于WooCommerce 4.0 电子邮件、管理员订单和感谢页面上的自定义结帐和 ACF 字段值的主要内容,如果未能解决你的问题,请参考以下文章

向管理员发送电子邮件通知,了解 WooCommerce 中的待处理订单状态

将 JavaScript 变量传递给 PHP,然后传递给 woo commerce 支持的电子商务网站中的购物车和订单项

禁用 Woocommerce 购物车订单项数量价格计算

在 Woocommerce 3+ 中使用订单项以编程方式创建订单

在 woocommerce 3 中获取订单项的元数据

如何从 Woocommerce 中的订单项中获取产品 sku [重复]