WooCommerce:随处添加/显示产品或版本自定义字段 [英] WooCommerce: Add/display Product or Variation custom field everywhere

查看:65
本文介绍了WooCommerce:随处添加/显示产品或版本自定义字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前使用WordPress 5.1.1和WooCommerce 3.5.7。我的WooCommerce商店有大约500种产品,由简单易变的产品组成。



每个产品自然都有一个SKU,但是每个产品还具有唯一的ID代码,称为商品码'。我向特定行业销售特定产品。



我已在我的functions.php文件中添加了用于简单和可变产品的自定义字段的代码,该代码在



我的问题是,我试图在购物车,结帐,发票和电子邮件中的商品标题下显示商品代码。 / p>

我已经在互联网上阅读了各种教程来帮助我,但我并不明智,因为每个教程都以不同的方式进行相似的操作。大多数教程假定用户已通过前端输入数据,但我的数据是预设的。



我曾经帮助过我的教程是:





 <?php 
//添加商品代码的WooCommerce自定义字段
函数vp_add_commodity_code(){
$ args = array(
'id'=>'vp_commodity_code ',
'label'=> __('商品代码, woocommerce),
占位符 => __(在此处输入商品代码, woocommerce),
desc_tip =>是的,
'description'=> __(此字段代表产品的商品代码。, woocommerce),
));
woocommerce_wp_text_input($ args);
}
add_action(‘woocommerce_product_options_sku’,‘vp_add_commodity_code’);


//将商品代码保存到WooCommerce数据库
函数vp_save_commodity_code($ post_id){
//获取商品代码
$ sku = isset( $ _POST ['vp_commodity_code'])吗? sanitize_text_field($ _POST [‘vp_commodity_code’]):’;

//获取产品
$ product = wc_get_product($ post_id);

//保存商品代码自定义字段
$ product-> update_meta_data('vp_commodity_code',$ sku);
$ product-> save();
}
add_action(‘woocommerce_process_product_meta’,‘vp_save_commodity_code’);

//在前端显示商品代码
函数vp_display_commodity_code(){
global $ post;
//检查商品代码值
$ product = wc_get_product($ post-> ID);
$ title = $ product-> get_meta(‘vp_commodity_code’);
if($ title){
//仅在我们获得字段标题
printf(
'< div class = vpcommoditycode-包装器>< strong>商品代码:< / strong>%s< / div>',
esc_html($ title)
);
}
}
add_action('woocommerce_before_add_to_cart_button','vp_display_commodity_code');


//在商品数据中添加自定义字段输入>变体>单一变体

add_action('woocommerce_variation_options_pricing','comcode_add_custom_field_to_variations',10,3);

函数comcode_add_custom_field_to_variations($ loop,$ variation_data,$ variation){
woocommerce_wp_text_input(array(
'id'=>'custom_field ['。$ loop。']' ,
'class'=>'short',
'label'=> __('Community Code','woocommerce'),
'value'=> get_post_meta($ Variation-> ID,'custom_field',true),
'placeholder'=> __('在此处输入商品代码','woocommerce'),
'desc_tip'=> true,
'description'=> __('该字段用于产品的商品代码。','woocommerce'),

);
}

//将自定义字段保存在产品变体中

add_action('woocommerce_save_product_variation','comcode_save_custom_field_variations',10,2);

函数comcode_save_custom_field_variations($ variation_id,$ i){
$ custom_field = $ _POST [’custom_field’] [$ i];
if(isset($ custom_field))update_post_meta($ variation_id,'custom_field',esc_attr($ custom_field));
}

//将自定义字段值存储到变化数据中

add_filter(``woocommerce_available_variation',comcode_add_custom_field_variation_data');

函数comcode_add_custom_field_variation_data($ variations){
$ variations ['custom_field'] ='< div class = woocommerce_custom_field>< strong>商品代码:< / strong> < span>'。 get_post_meta($ variations [’variation_id’], custom_field,true)。 ’< / span>< / div>’;
返回$ variations;
}
?>

某些PHP / WooCommerce天才可以通过提供代码或将我指向可以帮助我或命名第三方WordPress插件的教程。

解决方案

我已经重新研究并完成了您现有的代码例如,它不适用于产品变体……这将:




  • 在库存标签下的管理产品上显示自定义字段

  • 在每个变体的变体选项卡下的管理产品上显示自定义字段

  • 保存产品和产品变体的自定义字段值

  • 在单个产品页面上显示自定义字段值(同样针对每个选定的变体)

  • 在购物车和结帐页面上显示自定义字段值

  • 将自定义字段值保存为订单商品元数据

  • 在管理订单上显示自定义字段值

  • 在订单上显示自定义字段d电子邮件通知



代码:

  //管理员:添加自定义字段
add_action('woocommerce_product_options_sku','vp_add_commodity_code');
函数vp_add_commodity_code(){

woocommerce_wp_text_input(array(
'id'=>'_commodity_code',
'label'=> __('商品代码','woocommerce'),
'占位符'=> __('在此处输入商品代码','woocommerce'),
'desc_tip'=>是,
'description' => __('该字段代表产品的商品代码。','woocommerce'),
)));
}

//管理员:为简单的产品库存选项保存自定义字段值
add_action('woocommerce_admin_process_product_object','vp_product_save_commodity_code',10,1);
函数vp_product_save_commodity_code($ product){
if(isset($ _ POST ['_ commodity_code']))
$ product-> update_meta_data('_commodity_code',sanitize_text_field($ _ POST ['__ commodity_code ']));
}

//管理员:在产品变化选项中添加自定义字段,定价
add_action('woocommerce_variation_options_pricing','vp_add_variation_commodity_code',10,3);
函数vp_add_variation_commodity_code($ loop,$ variation_data,$ variation){

woocommerce_wp_text_input(array(
'id'=>'_commodity_code ['。$ loop。']' ,
'label'=> __('商品代码','woocommerce'),
'占位符'=> __('在此处输入商品代码','woocommerce'),
'desc_tip'=> true,
'description'=> __('该字段用于产品的商品代码。','woocommerce'),
'value'=> get_post_meta($ variation-> ID,'_commodity_code',true)
));
}

//管理员:保存产品变体选项定价中的自定义字段值
add_action('woocommerce_save_product_variation','save_barcode_variations',10,2);
函数save_barcode_variations($ variation_id,$ i){
if(isset($ _ POST ['_ commodity_code'] [$ i])){
update_post_meta($ variation_id,'_commodity_code',sanitize_text_field ($ _POST ['_ commodity_code'] [$ i]));
}
}

//前端:在产品
上显示商品代码add_action('woocommerce_before_add_to_cart_button','vp_product_display_commodity_code');
函数vp_product_display_commodity_code(){
全球$ product;

if($ value = $ product-> get_meta('_commodity_code')){
echo'< div class = vp-ccode-wrapper>< strong> '。 __(商品代码, woocommerce)。
’:< / strong>’。esc_html($ value)。’< / div>’;
}
}

//前端:显示产品变体的商品代码
add_filter(‘woocommerce_available_variation’,’vp_variation_display_commodity_code’,10,3);
函数vp_variation_display_commodity_code($ data,$ product,$ variation){

if($ value = $ variation-> get_meta('_commodity_code')){
$ data [ 'price_html']。='< p class = vp-ccode>< small>< strong>'。 __(商品代码, woocommerce)。
’:< / strong>’。esc_html($ value)。’< / small>< / p>’;
}

返回$ data;
}

//前端:在购物车上显示商品代码
add_filter('woocommerce_cart_item_name','vp_cart_display_commodity_code',10,3);
函数vp_cart_display_commodity_code($ item_name,$ cart_item,$ cart_item_key){
if(!is_cart())
返回$ item_name;

if($ value = $ cart_item ['data']-> get_meta('_ commodity_code')){
$ item_name。='< br>< small class = vp-ccode< strong>'。 __(商品代码, woocommerce)。
’:< / strong> ’。 esc_html($ value)。 ‘< / small>’;
}
返回$ item_name;
}

//前端:结帐时显示商品代码
add_filter(``woocommerce_checkout_cart_item_quantity, vp_checkout_display_commodity_code,10,3);
函数vp_checkout_display_commodity_code($ item_qty,$ cart_item,$ cart_item_key){
if($ value = $ cart_item ['data']-> get_meta('_ commodity_code')){
$ item_qty 。='< br>< small class = vp-ccode>< strong>'。 __(商品代码, woocommerce)。
’:< / strong> ’。 esc_html($ value)。 ‘< / small>’;
}
返回$ item_qty;
}

//保存商品代码以订购商品(并在管理订单上显示)
add_filter(‘woocommerce_checkout_create_order_line_item’,‘vp_order_item_save_commodity_code’,10,4);
函数vp_order_item_save_commodity_code($ item,$ cart_item_key,$ cart_item,$ order){
if($ value = $ cart_item ['data']-> get_meta('_ commodity_code')){
b $ item-> update_meta_data('_commodity_code',esc_attr($ value));
}
返回$ item_qty;
}

//前端&电子邮件:显示订单上的商品代码
add_action( woocommerce_order_item_meta_start, vp_order_item_display_commodity_code,10、4);
函数vp_order_item_display_commodity_code($ item_id,$ item,$ order,$ plain_text){
//不在管理员上
// if(is_admin())return;

if($ value = $ item-> get_meta(‘_ commodity_code’)){
$ value =‘< strong>’。 __(商品代码, woocommerce)。 ’:< / strong> ’。 esc_attr($ value);

//订单上
if(is_wc_endpoint_url())
echo‘< div class = vp-ccode>< small>’。 $ value。 ‘< / small>< / div>’;
//在电子邮件上
其他
echo‘< div style = font-size:11px; padding-top:6px>’。 $ value。 ‘< / div>’;
}
}

代码会出现在您活跃孩子的function.php文件中主题(或活动主题)。经过测试,可以正常工作。


Currently using WordPress 5.1.1 and WooCommerce 3.5.7. My WooCommerce store has around 500 products, made up of simple and variable products.

Each product naturally has a SKU, but each product also has a unique ID code called 'Commodity Code'. I sell specific products to a specific industry.

I have added the code for the Custom Fields for Simple and Variable product in my functions.php file, and this works great at the moment.

My problem is, I have trying to get the 'Commodity Code' to appear under the product title in the Cart, Check Out, invoice and email.

I have read various tutorials on the internet to help me, but I am none the wiser as each tutorial does similar things in different ways. Most of the tutorials assume a user has entered the data via the front-end but my data is preset.

Tutorials I have used to help me are:

<?php
// Add WooCommerce Custom Field for Commodity Code
function vp_add_commodity_code() {
  $args = array(
    'id' => 'vp_commodity_code',
    'label' => __( 'Commodity Code', 'woocommerce' ),
    'placeholder' => __( 'Enter Commodity Code here', 'woocommerce' ),
    'desc_tip' => true,
    'description' => __( 'This field is for the Commodity Code of the product.', 'woocommerce' ),
  );
  woocommerce_wp_text_input( $args );
}
add_action( 'woocommerce_product_options_sku', 'vp_add_commodity_code' );


// Save Commodity Code into WooCommerce Database
function vp_save_commodity_code( $post_id ) {
  // grab the Commodity Code
  $sku = isset( $_POST[ 'vp_commodity_code' ] ) ? sanitize_text_field( $_POST[ 'vp_commodity_code' ] ) : '';

  // grab the product
  $product = wc_get_product( $post_id );

  // save the Commodity Code custom field
  $product->update_meta_data( 'vp_commodity_code', $sku );
  $product->save();
}
add_action( 'woocommerce_process_product_meta', 'vp_save_commodity_code' );

// Display Commodity Code on the Frontend
function vp_display_commodity_code() {
    global $post;
    // Check for the Commodity Code value
    $product = wc_get_product( $post->ID );
    $title = $product->get_meta( 'vp_commodity_code' );
    if( $title ) {
    // Only display the field if we've got a value for the field title
    printf(
    '<div class="vpcommoditycode-wrapper"><strong>Commodity Code: </strong>%s</div>',
    esc_html( $title )
    );
    }
   }
   add_action( 'woocommerce_before_add_to_cart_button', 'vp_display_commodity_code' );


// Add custom field input @ Product Data > Variations > Single Variation

add_action( 'woocommerce_variation_options_pricing', 'comcode_add_custom_field_to_variations', 10, 3 ); 

function comcode_add_custom_field_to_variations( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input( array(
    'id' => 'custom_field[' . $loop . ']',
    'class' => 'short',
    'label' => __( 'Community Code', 'woocommerce' ),
    'value' => get_post_meta( $variation->ID, 'custom_field', true ),
    'placeholder' => __( 'Enter Commodity Code here', 'woocommerce' ),
    'desc_tip' => true,
    'description' => __( 'This field is for the Commodity Code of the product.', 'woocommerce' ),
)
);
}

// Save custom field on product variation

add_action( 'woocommerce_save_product_variation', 'comcode_save_custom_field_variations', 10, 2 );

function comcode_save_custom_field_variations( $variation_id, $i ) {
$custom_field = $_POST['custom_field'][$i];
if ( isset( $custom_field ) ) update_post_meta( $variation_id, 'custom_field', esc_attr( $custom_field ) );
}

// Store custom field value into variation data

add_filter( 'woocommerce_available_variation', 'comcode_add_custom_field_variation_data' );

function comcode_add_custom_field_variation_data( $variations ) {
$variations['custom_field'] = '<div class="woocommerce_custom_field"><strong>Commodity Code: </strong><span>' . get_post_meta( $variations[ 'variation_id' ], 'custom_field', true ) . '</span></div>';
return $variations;
}
?>

Can some kind PHP/WooCommerce genius help me here please by either providing the code or point me to a tutorial that would help me or name a third party WordPress Plugin that will do this.

解决方案

I have revisited and completed your existing code as for example it was not working on product variations… This will:

  • Display a custom field on admin product under inventory tab
  • Display a custom field on admin product under variations tab for each variation
  • Save the custom field value for products and product variations
  • Display the custom field value on single product pages (also for each selected variation)
  • Display the custom field value on cart and checkout pages
  • Save the custom field value as order item meta data
  • Display the custom field value on admin orders
  • Display the custom field on orders and email notifications

The code:

// Admin: Add custom field
add_action('woocommerce_product_options_sku', 'vp_add_commodity_code' );
function vp_add_commodity_code(){

    woocommerce_wp_text_input( array(
        'id'          => '_commodity_code',
        'label'       => __('Commodity Code', 'woocommerce' ),
        'placeholder' => __('Enter Commodity Code here', 'woocommerce' ),
        'desc_tip'    => true,
        'description' => __('This field is for the Commodity Code of the product.', 'woocommerce' ),
    ) );
}

// Admin: Save custom field value for simple product inventory options
add_action('woocommerce_admin_process_product_object', 'vp_product_save_commodity_code', 10, 1 );
function vp_product_save_commodity_code( $product ){
    if( isset($_POST['_commodity_code']) )
        $product->update_meta_data( '_commodity_code', sanitize_text_field($_POST['_commodity_code']) );
}

// Admin: Add custom field in product variations options pricing
add_action( 'woocommerce_variation_options_pricing', 'vp_add_variation_commodity_code', 10, 3 );
function vp_add_variation_commodity_code( $loop, $variation_data, $variation ){

   woocommerce_wp_text_input( array(
        'id'          => '_commodity_code['.$loop.']',
        'label'       => __('Commodity Code', 'woocommerce' ),
        'placeholder' => __('Enter Commodity Code here', 'woocommerce' ),
        'desc_tip'    => true,
        'description' => __('This field is for the Commodity Code of the product.', 'woocommerce' ),
        'value'       => get_post_meta( $variation->ID, '_commodity_code', true )
    ) );
}

// Admin: Save custom field value from product variations options pricing
add_action( 'woocommerce_save_product_variation', 'save_barcode_variations', 10, 2 );
function save_barcode_variations( $variation_id, $i ){
    if( isset($_POST['_commodity_code'][$i]) ){
        update_post_meta( $variation_id, '_commodity_code', sanitize_text_field($_POST['_commodity_code'][$i]) );
    }
}

// Frontend: Display Commodity Code on product
add_action( 'woocommerce_before_add_to_cart_button', 'vp_product_display_commodity_code' );
function vp_product_display_commodity_code() {
    global $product;

    if( $value = $product->get_meta( '_commodity_code' ) ) {
        echo '<div class="vp-ccode-wrapper"><strong>' . __("Commodity Code", "woocommerce") .
        ': </strong>'.esc_html( $value ).'</div>';
    }
}

// Frontend: Display Commodity Code on product variations
add_filter( 'woocommerce_available_variation', 'vp_variation_display_commodity_code', 10, 3 );
function vp_variation_display_commodity_code( $data, $product, $variation ) {

    if( $value = $variation->get_meta( '_commodity_code' ) ) {
        $data['price_html'] .= '<p class="vp-ccode"><small><strong>' . __("Commodity Code", "woocommerce") .
        ': </strong>'.esc_html( $value ).'</small></p>';
    }

    return $data;
}

// Frontend: Display Commodity Code on cart
add_filter( 'woocommerce_cart_item_name', 'vp_cart_display_commodity_code', 10, 3 );
function vp_cart_display_commodity_code( $item_name, $cart_item, $cart_item_key ) {
    if( ! is_cart() )
        return $item_name;

    if( $value = $cart_item['data']->get_meta('_commodity_code') ) {
        $item_name .= '<br><small class="vp-ccode"><strong>' . __("Commodity Code", "woocommerce") .
            ':</strong> ' . esc_html( $value ) . '</small>';
    }
    return $item_name;
}

// Frontend: Display Commodity Code on checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'vp_checkout_display_commodity_code', 10, 3 );
function vp_checkout_display_commodity_code( $item_qty, $cart_item, $cart_item_key ) {
    if( $value = $cart_item['data']->get_meta('_commodity_code') ) {
        $item_qty .= '<br><small class="vp-ccode"><strong>' . __("Commodity Code", "woocommerce") .
            ':</strong> ' . esc_html( $value ) . '</small>';
    }
    return $item_qty;
}

// Save Commodity Code to order items (and display it on admin orders)
add_filter( 'woocommerce_checkout_create_order_line_item', 'vp_order_item_save_commodity_code', 10, 4 );
function vp_order_item_save_commodity_code( $item, $cart_item_key, $cart_item, $order ) {
    if( $value = $cart_item['data']->get_meta('_commodity_code') ) {
        $item->update_meta_data( '_commodity_code', esc_attr( $value ) );
    }
    return $item_qty;
}

// Frontend & emails: Display Commodity Code on orders
add_action( 'woocommerce_order_item_meta_start', 'vp_order_item_display_commodity_code', 10, 4 );
function vp_order_item_display_commodity_code( $item_id, $item, $order, $plain_text ) {
    // Not on admin
    //if( is_admin() ) return;

    if( $value = $item->get_meta('_commodity_code') ) {
        $value = '<strong>' . __("Commodity Code", "woocommerce") . ':</strong> ' . esc_attr( $value );

        // On orders
        if( is_wc_endpoint_url() )
            echo '<div class="vp-ccode"><small>' . $value . '</small></div>';
        // On Emails
        else
            echo '<div style="font-size:11px;padding-top:6px">' . $value . '</div>';
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

这篇关于WooCommerce:随处添加/显示产品或版本自定义字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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