在购物车和订单商品名称下显示woocommerce产品定制字段 [英] Show woocommerce product custom fields under the cart and order item name

查看:41
本文介绍了在购物车和订单商品名称下显示woocommerce产品定制字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码可以在产品编辑页面的常规标签中显示自定义字段。

I have a code that shows custom fields in the "General" tab on the product edit page.

在经理填写完这些字段后,将显示数据在存档/类别页面和单一产品页面上。

After the manager has filled these fields, the data is displayed on the Archive/Category pages and in the Single Product page.

此外,这些字段也在购物车和结帐页面上。

Also, these fields are in the cart and on the checkout page.

这是我的代码:

// Display Fields using WooCommerce Action Hook
add_action( 'woocommerce_product_options_general_product_data', 'woocom_general_product_data_custom_field' );

function woocom_general_product_data_custom_field() {
  // Create a custom text field

  // Custom Weight Field
  woocommerce_wp_text_input( 
    array( 
      'id' => '_custom_weight', 
      'label' => __( 'Weight dishes', 'woocommerce' ), 
      'placeholder' => '',
      'desc_tip' => 'true',
      'description' => __( '', 'woocommerce' ) 
    )
  );     

  // Calories Field
  woocommerce_wp_text_input( 
    array( 
      'id' => '_сalories', 
      'label' => __( 'Calories', 'woocommerce' ), 
      'placeholder' => '',
      'desc_tip' => 'false',
      'description' => __( '', 'woocommerce' ) 
    )
  );  

  // Ingredients Field
  woocommerce_wp_textarea_input( 
     array( 
       'id' => '_ingredients', 
       'label' => __( 'Ingredients', 'woocommerce' ), 
       'placeholder' => '',
       'desc_tip' => 'true',
       'description' => __( '', 'woocommerce' ) 
     )
 );
}

// Hook to save the data value from the custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_save_general_proddata_custom_field' );

// Hook callback function to save custom fields information 
function woocom_save_general_proddata_custom_field( $post_id ) {

  // Save Custom Weight Field
  $custom_weight = $_POST['_custom_weight'];
  if( ! empty( $custom_weight ) ) {
     update_post_meta( $post_id, '_custom_weight', esc_attr( $custom_weight ) );
  } else {
     delete_post_meta( $post_id, '_custom_weight' );
  }      

  // Save Calories Field
  $сalories = $_POST['_сalories'];
  if( ! empty( $сalories ) ) {
     update_post_meta( $post_id, '_сalories', esc_attr( $сalories ) );
  } else {
     delete_post_meta( $post_id, '_сalories' );
  }   

  // Save Ingredients Field
  $ingredients = $_POST['_ingredients'];
  if( ! empty( $ingredients ) ) {
     update_post_meta( $post_id, '_ingredients', esc_html( $ingredients ) );
 } else {
     delete_post_meta( $post_id, '_ingredients' );
  }                 

}

// Displaying the custom field value (on single product pages under short description)
add_action('woocommerce_before_add_to_cart_form', 'display_custom_meta_field_value', 25 );
add_action('woocommerce_after_shop_loop_item', 'display_custom_meta_field_value', 25 );
function display_custom_meta_field_value() {
global $product;

$custom_weight = get_post_meta( $product->get_id(),'_custom_weight', true );
if( ! empty( $custom_weight ) )
    echo  '<p id="value-on-single-product">' . 'Weight: ' . $custom_weight . 'g' . '</p>';

$сalories = get_post_meta( $product->get_id(),'_сalories', true );
if( ! empty( $сalories ) )
    echo  '<p id="value-on-single-product">' . 'Calories: ' . $сalories . ' kcal.' . '</p>';

$ingredients = get_post_meta( $product->get_id(),'_ingredients', true );
if( ! empty( $ingredients ) )
    echo '<p id="value-on-single-product">' . 'Ingredients: ' . $ingredients . '</p>';
}


// Render the custom product field in cart and checkout
add_filter( 'woocommerce_get_item_data', 'woocom_custom_fields_cart', 10, 2 );
function woocom_custom_fields_cart( $cart_data, $cart_item ) 
{
    $custom_items = array();

if( !empty( $cart_data ) )
    $custom_items = $cart_data;

// Get the product ID
$product_id = $cart_item['product_id'];

if( $custom_field_value = get_post_meta( $product_id, '_custom_weight', true ) )
    $custom_items[] = array(
        'name'      => __( 'Weight', 'woocommerce' ),
        'value'     => $custom_field_value,
        'display'   => $custom_field_value . 'g',
    );

return $custom_items;
}

但是很遗憾,我无法在产品名称后添加这些自定义字段

But unfortunately, it’s impossible for me to add these custom fields after the product name on the Thank You page, in the E-mail and on the order editing page.

尽管一切正常,我也怀疑上面的代码是否正确。

I also have doubts whether the above code is correct, although everything works.

p>

我会很高兴为您提供帮助!

I will be glad for your help!

推荐答案

我已经重新访问了您代码并进行了一些更改,添加了必要的代码,以在购物车,结帐,收到的订单,我的帐户订单视图以及电子邮件通知(适用于Woocommerce 3 +) 中显示您的自定义字段数据:

I have revisited your code and made some changes, adding the necessary code to display your custom fields data on cart, checkout, order received, my account order view and on email notifications (for Woocommerce 3+):

// Backend: Display additional product fields
add_action( 'woocommerce_product_options_general_product_data', 'add_fields_to_options_general_product_data' );
function add_fields_to_options_general_product_data() {
    // Custom Weight Field
    woocommerce_wp_text_input( array(
        'id' => '_custom_weight',
        'label' => __( 'Weight dishes', 'woocommerce' ),
    ));

    // Calories Field
    woocommerce_wp_text_input( array(
        'id' => '_сalories',
        'label' => __( 'Calories', 'woocommerce' ),
    ));

    // Ingredients Field
    woocommerce_wp_textarea_input( array(
        'id' => '_ingredients',
        'label' => __( 'Ingredients', 'woocommerce' ),
    ));
}

// Backend: Save the data value from the custom fields
add_action( 'woocommerce_admin_process_product_object', 'save_admin_product_custom_fields_values' );
function save_admin_product_custom_fields_values( $product ) {
    // Save Custom Weight Field
    if( isset( $_POST['_custom_weight'] ) ) {
        $product->update_meta_data( '_custom_weight', sanitize_text_field( $_POST['_custom_weight'] ) );
    }

    // Save Calories Field
    if( isset( $_POST['_сalories'] ) ) {
        $product->update_meta_data( '_сalories', sanitize_text_field( $_POST['_сalories'] ) );
    }

    // Save Ingredients Field
    if( isset( $_POST['_ingredients'] ) ) {
        $product->update_meta_data( '_ingredients', sanitize_textarea_field( $_POST['_ingredients'] ) );
    }
}

// Display product custom fields values on single product pages under short description and on archive pages
add_action('woocommerce_before_add_to_cart_form', 'display_custom_meta_field_value', 25 );
add_action('woocommerce_after_shop_loop_item', 'display_custom_meta_field_value', 25 );
function display_custom_meta_field_value() {
    global $product;

    if( $custom_weight = $product->get_meta('_custom_weight') )
        echo  '<p id="value-on-single-product">' . __("Weight:", "woocommerce") . ' ' . $custom_weight . 'g' . '</p>';

    if( $сalories = $product->get_meta('_сalories') )
        echo  '<p id="value-on-single-product">' . __("Calories:", "woocommerce") . ' ' . $сalories . ' kcal.' . '</p>';

    if( $ingredients = $product->get_meta('_ingredients') )
        echo '<p id="value-on-single-product">' . __("Ingredients:", "woocommerce") . ' ' . $ingredients . '</p>';
}

// Add custom fields values under cart item name in cart
add_filter( 'woocommerce_cart_item_name', 'custom_cart_item_name', 10, 3 );
function custom_cart_item_name( $item_name, $cart_item, $cart_item_key ) {
    if( ! is_cart() )
        return $item_name;

    if( $value1 = $cart_item['data']->get_meta('_custom_weight') ) {
        $item_name .= '<br><span class="custom-field"><strong>' . __("Weight", "woocommerce") . ':</strong> ' . $value1 . 'g</span>';
    }

    if( $value2 = $cart_item['data']->get_meta('_сalories') ) {
        $item_name .= '<br><span class="custom-field"><strong>' . __("Calories", "woocommerce") . ':</strong> ' . $value2 . 'kcal</span>';
    }

    if( $value3 = $cart_item['data']->get_meta('_ingredients') ) {
        $item_name .= '<br><span class="custom-field"><strong>' . __("Ingredients", "woocommerce") . ':</strong> <br>' . $value3 . '</span>';
    }

    return $item_name;
}

// Display custom fields values under item name in checkout
add_filter( 'woocommerce_checkout_cart_item_quantity', 'custom_checkout_cart_item_name', 10, 3 );
function custom_checkout_cart_item_name( $item_qty, $cart_item, $cart_item_key ) {
    if( $value1 = $cart_item['data']->get_meta('_custom_weight') ) {
        $item_qty .= '<br><span class="custom-field"><strong>' . __("Weight", "woocommerce") . ':</strong> ' . $value1 . 'g</span>';
    }

    if( $value2 = $cart_item['data']->get_meta('_сalories') ) {
        $item_qty .= '<br><span class="custom-field"><strong>' . __("Calories", "woocommerce") . ':</strong> ' . $value2 . 'kcal</span>';
    }

    if( $value3 = $cart_item['data']->get_meta('_ingredients') ) {
        $item_qty .= '<br><span class="custom-field"><strong>' . __("Ingredients", "woocommerce") . ':</strong> <br>' . $value3 . '</span>';
    }

    return $item_qty;
}

// Display custom fields values on orders and email notifications
add_filter( 'woocommerce_order_item_name', 'custom_order_item_name', 10, 2 );
function custom_order_item_name( $item_name, $item ) {
    $product = $item->get_product();

    if( $value1 = $product->get_meta('_custom_weight') ) {
        $item_name .= '<br><span class="custom-field"><strong>' . __("Weight", "woocommerce") . ':</strong> ' . $value1 . 'g</span>';
    }

    if( $value2 = $product->get_meta('_сalories') ) {
        $item_name .= '<br><span class="custom-field"><strong>' . __("Calories", "woocommerce") . ':</strong> ' . $value2 . 'kcal</span>';
    }

    if( $value3 = $product->get_meta('_ingredients') ) {
        $item_name .= '<br><span class="custom-field"><strong>' . __("Ingredients", "woocommerce") . ':</strong> <br>' . $value3 . '</span>';
    }

    return $item_name;
}

代码在活动子主题(或活动主题)的function.php文件中)。经过测试并有效。

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

在购物车页面上:

在结帐页面上:

在收到订单的页面上:

关于电子邮件通知:

这篇关于在购物车和订单商品名称下显示woocommerce产品定制字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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