设置产品自定义字段并在购物车、结帐和查看订单中显示值


Set product custom field and display value in cart, checkout and view order

我已经通过function.php在我的产品页面中创建了一个自定义保修字段。

add_action( 'woocommerce_product_options_general_product_data', 'test_custom_fields' );
function test_custom_fields() {
    // Print a custom text field
    woocommerce_wp_text_input( array(
        'id' => '_warranty',
        'label' => 'i.e. 15 years',
        'description' => '',
        'desc_tip' => 'true',
        'placeholder' => 'i.e. 15 years'
    ) );        
}
add_action( 'woocommerce_process_product_meta', 'test_save_custom_fields' );
function test_save_custom_fields( $post_id ) {
    if ( ! empty( $_POST['_warranty'] ) ) {
        update_post_meta( $post_id, '_warranty', esc_attr( $_POST['_warranty'] ) );
    }
}

我想"复制"这个自定义字段的键和值,在管理订单页面上根据购物车/订单(没有插件)的产品自行生成的自定义字段。

因此,在订单页面上有了这个自定义字段,我将最终能够在使用WooCommerce pdf发票插件的pdf发票中显示"保修"。

另一个解释:

  1. 作为管理员,我在"product1"页面中填写_warranty值
  2. 当"product1"在订单中,在管理订单视图中,我希望看到一个自定义字段包含"_warranty + value"从product1页面。
  3. 所以,作为管理员,我可以在WooCommerce PDF发票插件中设置 {{_warranty}} 以显示"保修:15年"

非常感谢你的帮助。

我刚刚测试了以下情况:在order Details

中的order items表中显示产品元

但这并没有给我一个自定义字段,所以我不能得到我的 {{_warranty}} 值宽度它。

我做错了什么?
我怎样才能做到这一点呢?

谢谢。

First: "在管理订单页面的自定义字段中使用键和值复制此自定义字段" 不是好方法。

为了达到你的期望,你只错过了一些小事情。你需要:

  1. 在购物车中存储自定义字段(当产品添加到购物车时)
  2. 在购物车和结帐页面上呈现
  3. 将订单中的信息添加为元数据(使其成为订单的一部分)

使用第3点,您将能够在WooCommerce PDF发票插件上显示"保修:15年"。

所以你需要的代码是:
// create the custom field on product admin tab
add_action( 'woocommerce_product_options_general_product_data', 'create_warranty_custom_field' );
function create_warranty_custom_field() {
    // Create a custom text field
    woocommerce_wp_text_input( array(
        'id'            => '_warranty',
        'type'          => 'text',
        'label'         => __('Warranty', 'woocommerce' ),
        'description'   => '',
        'desc_tip'      => 'true',
        'placeholder'   =>  __('i.e. 15 years', 'woocommerce' ),
    ) );
}
// save the data value from this custom field on product admin tab
add_action( 'woocommerce_process_product_meta', 'save_warranty_custom_field' );
function save_warranty_custom_field( $post_id ) {
    $wc_text_field = $_POST['_warranty'];
    if ( !empty($wc_text_field) ) {
        update_post_meta( $post_id, '_warranty', esc_attr( $wc_text_field ) );
    }
}
// Store custom field in Cart
add_filter( 'woocommerce_add_cart_item_data', 'store_warranty_custom_field', 10, 2 );
function store_warranty_custom_field( $cart_item_data, $product_id ) {
    $warranty_item = get_post_meta( $product_id , '_warranty', true );
    if( !empty($warranty_item) ) {
        $cart_item_data[ '_warranty' ] = $warranty_item;
        // below statement make sure every add to cart action as unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'days_manufacture', $warranty_item );
    }
    return $cart_item_data;
}

// Render meta on cart and checkout
add_filter( 'woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2 );
function rendering_meta_field_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['_warranty'] ) ) {
        $custom_items[] = array( "name" => __( "Warranty", "woocommerce" ), "value" => $cart_item['_warranty'] );
    }
    return $custom_items;
}
// Add the information in the order as meta data
add_action('woocommerce_add_order_item_meta','add_waranty_to_order_item_meta', 1, 3 );
function add_waranty_to_order_item_meta( $item_id, $values, $cart_item_key ) {
    // Retrieving the product id for the order $item_id
    $product_id = wc_get_order_item_meta( $item_id, '_product_id', true );
    // Getting the warranty value for this product Id
    $warranty = get_post_meta( $product_id, '_warranty', true );
    // Add the meta data to the order
    wc_add_order_item_meta($item_id, 'Warranty', $warranty, true);
}

当然,这是在你的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

此代码已经过测试并正常运行。


引用:

    WooCommerce:添加自定义Metabox到管理订单页面
  • 在购物车和结帐中显示的管理产品页面自定义字段

在代码末尾有一个错误,您首先调用变量$prod_id,然后是$product_id。正确的工作代码是:

// Add the information in the order as meta data
add_action('woocommerce_add_order_item_meta','add_waranty_to_order_item_meta', 1, 3 );
function add_waranty_to_order_item_meta( $item_id, $values, $cart_item_key ) {
    // Retrieving the product id for the order $item_id
    $prod_id = wc_get_order_item_meta( $item_id, '_product_id', true );
    // Getting the warranty value for this product Id
    $warranty = get_post_meta( $prod_id, '_warranty', true );
    // Add the meta data to the order
    wc_add_order_item_meta($item_id, 'Warranty', $warranty, true);
}