当前位置: 首页 > 知识库问答 >
问题:

WooCommerce 4.0定制结账

袁山
2023-03-14

我很难打印我的自定义字段值到电子邮件通知,订单管理和感谢页面。我浏览了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;
}

不管我怎么做,代码只打印标签,而不打印签出表单中的任何值。我知道这个问题已经被问了很多次,但我尝试了6天以上的每一个答案都没有任何运气。我还需要提到,我在这个项目中使用Woocommerce预订。谢谢你的帮助

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

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

在购物车和结账台上呈现自定义字段元

添加自定义字段元命令管理详细信息

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

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

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

Woocommerce 4.0.0和Wordpress 5.3.2

共有1个答案

壤驷茂实
2023-03-14

一步一步检查你的代码

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

您使用拾取位置拾取位置

通过picku\u location\u checkout\u field\u order\u 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;
}
 类似资料:
  • 流程控制语句对于程序设计来说特别重要,它可以用于设定程序的逻辑结构。一般需要与条件判断语句结合使用。Lua 语言提供的控制结构有 if,while,repeat,for,并提供 break 关键字来满足更丰富的需求。本章主要介绍 Lua 语言的控制结构的使用。

  • Control structures 控制结构 The control structures of Go are related to those of C but differ in important ways. There is no do or while loop, only a slightly generalized for; switch is more flexible; if

  • 控制结构 If If是分支 (branch) 的一种特殊形式,也可以使用else和else if。 与C语言不同的是,逻辑条件不需要用小括号括起来,但是条件后面必须跟一个代码块。 Rust中的if是一个表达式 (expression),可以赋给一个变量: let x = 5; let y = if x == 5 { 10 } else { 15 }; Rust是基于表达式的编程语言,有且仅有两种语

  • 控制结构 函数式风格的程序倾向于需要更少的传统的控制结构,并且使用声明式风格写的程序读起来更好。这通常意味着打破你的逻辑,拆分到若干个小的方法或函数,用匹配表达式(match expression)把他们粘在一起。函数式程序也倾向于更多面向表达式(expression-oriented):条件分支是同一类型的值计算,for(..) yield 表达式,以及递归都是司空见惯的。 递归 用递归术语来表

  • 条件分支 if if 修饰句 unless unless 修饰句 case 循环 while while 修饰句 until until修饰句 for break next redo retry 异常处理 raise begin rescue修饰句 其他 return BEGIN END (与C等语言不同的是)Ruby的控制结构是表达式,其中的一部分还会返回值(也有不返回值的,若把这些不返回值的表

  • 不支持switch和goto,支持if,else,while,do,for,break,continue,return,?:。 条件判断中的括号不可省略,但在单行语句中的大括号可以省略。 需要注意的是,这里没有像C语言,和javascript里的非Boolean类型的自动转换,比如if(1){...}在Solidity中是无效的。