设置产品自定义字段并在购物车,结帐和查看订单中显示值 [英] Set product custom field and display value in cart, checkout and view order

查看:96
本文介绍了设置产品自定义字段并在购物车,结帐和查看订单中显示值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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.

另一种解释:

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

非常感谢您的帮助.

我刚刚测试了以下情况:

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

但这并没有给我一个自定义字段,因此我无法获得 {{_warranty}} 的值宽度.

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?

谢谢.

推荐答案

首先:在管理订单页面上的自定义字段中复制此自定义字段和键和值" 不是一个好方法

要实现您的期望,您仅错过了一些小事情.您需要:

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

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

使用要点3 ,您将可以在WooCommerce PDF发票插件上获得此标签,以显示保修期:15年".

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

所以您需要的代码是:

// 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 : Add custom Metabox to admin order page
  • Admin product pages custom field displayed in Cart and checkout

这篇关于设置产品自定义字段并在购物车,结帐和查看订单中显示值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆