保存产品自定义字段并将其显示在购物车页面中 [英] Saving a product custom field and displaying it in cart page

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

问题描述

使用WooCommerce,我正在我的functions.php文件中进行一些自定义,以在购物车页面上获得自定义字段值.

With WooCommerce and I am doing some customization in my functions.php file, to get a custom field value on Cart page.

在添加到购物车之前,我添加了一个自定义字段:

I add a custom field before add to cart:

function add_name_on_tshirt_field() {
  echo '<table class="variations" cellspacing="0">
      <tbody>
          <tr>
          <td class="label"><label for="color">Name On T-Shirt</label></td>
          <td class="value">
              <input type="text" name="name-on-tshirt" value="" />
          </td>
      </tr>                             
      </tbody>
  </table>';
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_name_on_tshirt_field' );

function render_meta_on_cart_item( $title = null, $cart_item = null, $cart_item_key = null ) {
    if( $cart_item_key && is_cart() ) {
        echo $title. '<dl class="">
                 <dt class="">Name On T-Shirt : </dt>
                 <dd class=""><p>'. WC()->session->get( $cart_item_key.'_name_on_tshirt') .'</p></dd>           
              </dl>';
    }else {
        echo $title;
    }

  /*  $d=WC();
    echo "<pre>";
    print_r($d);*/
}
add_filter( 'woocommerce_cart_item_name', 'render_meta_on_cart_item', 1, 3 );


function tshirt_order_meta_handler( $item_id, $values, $cart_item_key ) 
{
    wc_add_order_item_meta( $item_id, "name_on_tshirt", WC()->session->get( $cart_item_key.'_name_on_tshirt') );    
}
add_action( 'woocommerce_add_order_item_meta', 'tshirt_order_meta_handler', 1, 3 );

我想在购物车页面上使用此自定义值,我也使用了钩子

And i want this custom value on cart page i also used hook

add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );

但是我无法从此自定义字段中获得任何价值.

这怎么可能?我在做什么错了?

谢谢

推荐答案

您的代码中存在很多错误和遗漏

There is a lot of errors and missing things in your code

1)设置:

如果要在客户订单和电子邮件通知上正确显示标签"T恤上的名称" ,则需要为此标签名称创建一个属性(在产品">属性"下):

If you want to display correctly the label "Name On T-Shirt" on customer Orders and email notifications, you need to create an attribute for this label name (under Products > Attributes):

创建产品属性:

为此属性添加一些值:

和…

然后保存...

2)代码:

// Add the custom field to product pages
add_action( 'woocommerce_before_add_to_cart_button', 'add_nmy_custom_product_field', 10, 0 );
function add_nmy_custom_product_field() {
    ?>
        <table class="variations" cellspacing="0">
            <tbody>
                <tr>
                    <td class="label">
                        <label for="color"><?php _e('Name On T-Shirt', 'woocommerce'); ?></label>
                    </td>
                    <td class="value">
                        <input type="text" name="name-on-tshirt" value="" />
                    </td>
                </tr>
            </tbody>
        </table>
    <?php
}

// Save the custom product field data in Cart item
add_filter( 'woocommerce_add_cart_item_data', 'save_in_cart_my_custom_product_field', 10, 2 );
function save_in_cart_my_custom_product_field( $cart_item_data, $product_id ) {
    if( isset( $_POST['name-on-tshirt'] ) ) {
        $cart_item_data[ 'name-on-tshirt' ] = $_POST['name-on-tshirt'];

        // When add to cart action make an unique line item
        $cart_item_data['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'custom_data', $_POST['name-on-tshirt'] );
    }
    return $cart_item_data;
}

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

    $custom_items = array();

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

    if( $custom_field_value = $cart_item['name-on-tshirt'] )
        $custom_items[] = array(
            'name'      => __( 'Name On T-Shirt', 'woocommerce' ),
            'value'     => $custom_field_value,
            'display'   => $custom_field_value,
        );

    return $custom_items;
}

// Add the the custom product field as item meta data in the order
add_action( 'woocommerce_add_order_item_meta', 'tshirt_order_meta_handler', 10, 3 );
function tshirt_order_meta_handler( $item_id, $cart_item, $cart_item_key ) {
    $custom_field_value = $cart_item['name-on-tshirt'];
    if( ! empty($custom_field_value) )
        wc_update_order_item_meta( $item_id, 'pa_name-on-tshirt', $custom_field_value );
}

此代码会出现在您活动的子主题(或主题)的function.php文件中,也可能会出现在任何插件文件中.

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

此代码有效,并且已针对WooCommerce版本从2.5到3.0+进行了测试

This code works and is tested for WooCommerce version from 2.5 to 3.0+

这篇关于保存产品自定义字段并将其显示在购物车页面中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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