WooCommerce 4.0自定义结帐&电子邮件,管理员订单和“谢谢"页面上的ACF字段值 [英] WooCommerce 4.0 custom checkout & ACF field value on email, admin order, and thank you page

查看:67
本文介绍了WooCommerce 4.0自定义结帐&电子邮件,管理员订单和“谢谢"页面上的ACF字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难将我的自定义字段值打印到电子邮件通知,订购管理员和谢谢"页面.我浏览了StackOverflow,尝试了找到的每个答案,但不幸的是无法正常工作,我无法弄清问题所在:我正在尝试传递附加checkout字段的值,它只打印带有空白值的强标签,并且在电子邮件中什么也没有显示,到目前为止这是我的代码:

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:

//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.谢谢您的帮助

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. I also need to mention that I am using Woocommerce Bookings in this project. Thanks for your help

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

[Update:] Saving and displaying ACF field in the cart, admin order, customer order, checkout, and emails. 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.

在ATC上方的产品页面上显示自定义字段:

Displaying custom field on the product page above 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;
}

在单个产品页面中显示自定义字段值

将接机时间自定义字段保存到购物车和会话中

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

在购物车和结帐处呈现自定义字段元

添加自定义字段元以订购管理员详细信息

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

将提取时间自定义字段元添加到电子邮件中

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

Please comment if you see any errors or ways to improve, Thanks.

Woocommerce 4.0.0和Wordpress 5.3.2

Woocommerce 4.0.0 and Wordpress 5.3.2

推荐答案

逐步执行代码

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_location 作为 meta_key 而这应该是相同值的3倍.

You use Pick-up location, _pick_up_location & pick_up_location as meta_key while this should be 3x the same value.

您还可以通过 pickup_location_checkout_field_order_page 函数错过结束标记.

You also miss a closing tag by pickup_location_checkout_field_order_page function.

您还使用了带有一些钩子的错误参数,等等.

You also use wrong parameters with some hooks, etc..

试试看

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

这篇关于WooCommerce 4.0自定义结帐&amp;电子邮件,管理员订单和“谢谢"页面上的ACF字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆