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

2021-12-22 00:00:00 product php wordpress woocommerce orders

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

add_action( 'woocommerce_product_options_general_product_data', 'test_custom_fields' );函数 test_custom_fields() {//打印自定义文本字段woocommerce_wp_text_input(数组('id' =>'_保修单','标签' =>'IE.15年','说明' =>'','desc_tip' =>'真的','占位符' =>'IE.15年') );}add_action('woocommerce_process_product_meta', 'test_save_custom_fields');功能 test_save_custom_fields( $post_id ) {如果(!空($_POST['_warranty'])){update_post_meta( $post_id, '_warranty', esc_attr( $_POST['_warranty'] ) );}}

我想根据购物车/订单中的产品(无插件)在管理订单页面上的一个自生成的自定义字段中使用键和值复制"这个自定义字段.

因此,通过订单页面上的这个自定义字段,我终于可以使用 WooCommerce PDF Invoice 插件在我的 pdf 发票中显示保修"了.

另一种解释:

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

非常感谢您的帮助.

我刚刚测试了以下案例:在订单详情中的订单项表中显示产品元数据

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

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

谢谢.

解决方案

第一:在管理订单页面上的一个自生成的自定义字段中复制带有键和值的自定义字段" 不是好方法.

为了实现您的期望,您只是错过了一些小事.您需要:

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

<块引用>

使用第 3 点,您将能够在 WooCommerce PDF Invoice 插件上获得此信息,以显示保修:15 年".

所以你需要的代码是:

//在产品管理选项卡上创建自定义字段add_action('woocommerce_product_options_general_product_data', 'create_warranty_custom_field');函数 create_warranty_custom_field() {//创建自定义文本框woocommerce_wp_text_input(数组('id' =>'_保修单','类型' =>'文本','标签' =>__('保修', 'woocommerce'),'说明' =>'','desc_tip' =>'真的','占位符' =>__('即 15 年', 'woocommerce'),) );}//将此自定义字段中的数据值保存在产品管理选项卡上add_action('woocommerce_process_product_meta', 'save_warranty_custom_field');功能 save_warranty_custom_field( $post_id ) {$wc_text_field = $_POST['_warranty'];如果(!空($wc_text_field)){update_post_meta( $post_id, '_warranty', esc_attr( $wc_text_field ) );}}//将自定义字段存储在购物车中add_filter( 'woocommerce_add_cart_item_data', 'store_warranty_custom_field', 10, 2 );函数 store_warranty_custom_field( $cart_item_data, $product_id ) {$warranty_item = get_post_meta( $product_id , '_warranty', true );如果(!空($warranty_item)){$cart_item_data['_warranty'] = $warranty_item;//下面的语句确保每个添加到购物车的操作都是唯一的订单项$cart_item_data['unique_key'] = md5( microtime().rand() );WC()->session->set('days_manufacture', $warranty_item);}返回 $cart_item_data;}//在购物车和结帐上呈现元数据add_filter('woocommerce_get_item_data', 'rendering_meta_field_on_cart_and_checkout', 10, 2);函数 rendering_meta_field_on_cart_and_checkout( $cart_data, $cart_item ) {$custom_items = array();//Woo 2.4.2 更新if( !empty( $cart_data ) ) {$custom_items = $cart_data;}if( isset( $cart_item['_warranty'] ) ) {$custom_items[] = array( "name" => __( "Warranty", "woocommerce" ), "value" => $cart_item['_warranty'] );}返回 $custom_items;}//将订单中的信息添加为元数据add_action('woocommerce_add_order_item_meta','add_waranty_to_order_item_meta', 1, 3);函数 add_waranty_to_order_item_meta( $item_id, $values, $cart_item_key ) {//检索订单 $item_id 的产品 ID$product_id = wc_get_order_item_meta( $item_id, '_product_id', true );//获取此产品 ID 的保修值$warranty = get_post_meta( $product_id, '_warranty', true );//将元数据添加到订单中wc_add_order_item_meta($item_id, '保修', $warranty, true);}

当然,这会出现在活动子主题(或主题)的 function.php 文件或任何插件文件中.

此代码已经过测试且有效.

<小时>

参考文献:

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

I've created a custom field for warranty in my products pages, via 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'] ) );
    }
}

I would like to "duplicate" this custom field with key and value, in an self-generated custom field on the admin order page depending on products in cart/order (without plugin).

So, with this custom field on order page, I will finally be able to display "warranty" in my pdf invoice with WooCommerce PDF Invoice plugin.

Another explanation :

  1. As admin, I fill _warranty value in "product1" page
  2. When "product1" is in an order, on the admin order view, I would like to see a custom field containing "_warranty + value" from product1 page.
  3. So, as admin, I could set {{_warranty}} in WooCommerce PDF Invoice plugin to display "Warranty : 15 years"

Many thanks for your help.

I have just tested the following case: show product meta in order items table in Order Details

But this does not give me a custom field, so I couldn't get my {{_warranty}} value width it.

What I am doing wrong?
How can I achieve this?

Thanks.

解决方案

First: "Duplicating this custom field with key and value, in an self-generated custom field on the admin order page" is not the good approach.

To achieve what you are expecting, you have missed just some little things. You need to:

  1. Store custom field in Cart (when product is added to cart)
  2. Render this on cart and checkout pages
  3. Add the information in the order as meta data (to make it part of the order)

With point 3 you will be able to get this on WooCommerce PDF Invoice plugin to display "Warranty : 15 years".

So the code you need is:

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

Naturally, this goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.


References:

  • WooCommerce : Add custom Metabox to admin order page
  • Admin product pages custom field displayed in Cart and checkout

相关文章