在 WooCommerce 订单和电子邮件中添加和显示自定义购物车项目数据

问题描述

我有一个自定义的 woocommerce 产品类型,称为彩票。我需要一个自定义选择字段(因为它不是可变产品)所以我添加一个

一切都很好,我在购物车和结帐中也得到了价值,但我无法在管理员订单或订单邮件(客户和管理员)中获取价值。

这是我在function.PHP添加代码。我做错了什么?

function hpplrs_display_custom_field() {
    global $post;
    // Check for the custom field value
    $product = wc_get_product( $post->ID );
    $title = $product->get_Meta( 'custom_text_field_title' );
    // display in lottery.PHP

    
        printf(
            '<div class="col-md-12 small-gap">
                <div class="size_select">
                    <select id="hpplrs-size-select" name="hpplrs-size-select" title="Size" class="size form-control">
                        <option value="" disabled selected>' . __('Select your size','hpplrs') . '</option>
                        <option value="option 1">option 1</option>
                        <option value="option 2">option 2</option>
                        <option value="option 3">option 3</option>
                    </select>
                </div>
            </div>',esc_html( $title )
        );
        
        
        
}
add_action( 'hpplrs_before_single_product_qty','hpplrs_display_custom_field' );

/**
 * Add the text field as item data to the cart object
 * @since 1.0.0
 */
function hpplrs_add_custom_field_item_data( $cart_item_data,$product_id,$variation_id,$quantity ) {
    if( ! empty( $_POST['hpplrs-size-select'] ) ) {
        // Add the item data
        $cart_item_data['title_field'] = $_POST['hpplrs-size-select'];
        $product = wc_get_product( $product_id ); // Expanded function
        $price = $product->get_price(); // Expanded function
    }
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data','hpplrs_add_custom_field_item_data',10,4 );


/**
 * display the custom field value in the cart
 * @since 1.0.0
 */
function hpplrs_cart_item_name( $name,$cart_item,$cart_item_key ) {
    if( isset( $cart_item['title_field'] ) ) {
      $name .= sprintf(
            '<p class="size"><span>'. __('Size','hpplrs') .':</span> %s</p>',esc_html( $cart_item['title_field'] )
        );
    }
    return $name;
}
add_filter( 'woocommerce_cart_item_name','hpplrs_cart_item_name',3 );


/**
 * Add custom field to order object
 */
function hpplrs_add_custom_data_to_order( $item,$cart_item_key,$values,$order ) {
    foreach( $item as $cart_item_key=>$values ) {
        if( isset( $values['title_field'] ) ) {
            $item->add_Meta_data( _e( 'Size','hpplrs' ),$values['title_field'],true );
        }
    }
}
add_action( 'woocommerce_checkout_create_order_line_item','hpplrs_add_custom_data_to_order',4 );

解决方法

您的上一个函数有一些错误……而是将其替换为以下内容:

/**
 * Set custom field as visible order item meta data (displayed on orders and email notifications)
 */
add_action( 'woocommerce_checkout_create_order_line_item','hpplrs_add_custom_data_to_order',10,4 );
function hpplrs_add_custom_data_to_order( $item,$cart_item_key,$values,$order ) {
    if( isset($values['title_field']) && ! empty($values['title_field']) ) {
        $item->add_meta_data( __( 'Size','hpplrs' ),$values['title_field'] );
    }
}

经过测试并有效。现在您的自定义字段将显示在订单和电子邮件通知中。