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

标签 php wordpress woocommerce custom-fields orders

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

一切工作正常,我在购物车和结帐中也获得了值(value),但我无法在管理订单或订单邮件(客户和管理员)中获得值(value)。

这是我在 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', 10, 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', 10, 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'] );
    }
}

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

关于php - 在 WooCommerce 订单和电子邮件中添加并显示自定义购物车项目数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65694153/

相关文章:

php - 如何查找数组项以特定字母开头的所有场合

php - 当订单 ID 不可用时,在 WooCommerce 电子邮件中获取订单元数据

php - Wordpress 以什么方式重写页面 URL?

wordpress - Woocommerce 模板覆盖不起作用

php - 从 Web 根目录的子目录中的 CodeIgniter URL 中删除 index.php

php - 如何只取 url 中文件名的结尾?

mysql - WP Job Manager 数据库查询

php - 将一般的 wordpress 搜索与 woocommerce 产品搜索合并?

php - 从所选变体向 WooCommerce 变量产品标题添加一些属性值

javascript - 当其他人通过同一页面的另一个实例更新数据库时,我想更新我的网页