WooCommerce:向购物车中的每个项目添加输入字段 [英] WooCommerce: Add input field to every item in cart

查看:32
本文介绍了WooCommerce:向购物车中的每个项目添加输入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试为购物车中的每个项目添加一个文本输入字段,并将该用户输入提交到产品的元信息.已经2天了,我还没有成功.

I've been trying to add a single text input field to every item in the cart and submit that user input to product's meta info. It's been 2 days and I haven't succeeded yet.

我的目标是:

  1. 从用户那里获取添加到购物车的每件商品的输入.
  2. 在订单的元信息中显示该输入.
  3. 在发送给客户的确认电子邮件中显示该输入.

到目前为止,我已将模板文件复制到我的主题并在单元格中添加了一个输入字段.我在使用钩子时遇到问题,从 WooCommerce Product Gift Wrap 插件中了解了我需要的钩子如本woocommerce 问题所示.

So far, I have copied the template file to my theme and added an input field inside a cell. I'm having trouble with the hooks, learned about hooks I will need from WooCommerce Product Gift Wrap plugin as indicated in this woocommerce issue.

我添加到我的主题目录中复制的cart.php模板的代码:

Code I added to the cart.php template copied in my theme directory :

$input_url_data = '<div class="input-url"><input type="text" name="cart-url" value="" title="" class="input-text cart-url text" /></div>';

echo apply_filters( 'woocommerce_add_cart_item_data', $input_url_data, $cart_item_key );

我添加到我的主题的functions.php的代码:

Code I added to my theme's functions.php :

add_filter( 'woocommerce_add_cart_item_data','add_cart_item_data', 10, 2 );
add_filter( 'woocommerce_get_cart_item_from_session','get_cart_item_from_session', 10, 2 );
add_filter( 'woocommerce_get_item_data','get_item_data', 10, 2 );
add_filter( 'woocommerce_add_cart_item','add_cart_item', 10, 1 );
add_action( 'woocommerce_add_order_item_meta','add_order_item_meta', 10, 2 );

function add_cart_item_data( $cart_item_meta, $product_id ) {
    $input_url_key = "";
    $input_url_data['inputurl'] = $input_url_key;
    return $input_url_data;
}

function get_cart_item_from_session( $cart_item, $values ) {

if ( ! empty( $values['inputurl'] ) ) {
    $cart_item['inputurl'] = true;
}
return $cart_item;
}

function get_item_data( $item_data, $cart_item ) {

if ( ! empty( $cart_item['inputurl'] ) )
    $item_data[] = array(
    );
return $item_data;
}

function add_cart_item( $cart_item ) {
if ( ! empty( $cart_item['inputurl'] ) ) {

}
return $cart_item;
}

function add_order_item_meta( $item_id, $cart_item ) {
if ( ! empty( $cart_item['inputurl'] ) )
woocommerce_add_order_item_meta( $item_id, __( 'URL by buyer', 'custom_input_url' ), __( 'Yes', 'custom_input_url' ) );
}

关于 hook woocommerce_add_cart_item_data 的文档不是很有帮助,我一直坚持这个.我该如何继续?

Documentation about hook woocommerce_add_cart_item_data isn't very helpful and I'm stuck at this. How do I proceed?

推荐答案

有一个 wordpress 插件叫做 WC Fields Factory 用于确切目的.

There is a wordpress plugin called WC Fields Factory for the exact purpose.

您还可以通过使用以下 woocommerce 挂钩woocommerce_before_add_to_cart_buttonwoocommerce_add_to_cartwoocommerce_cart_item_name'woocommerce_add_order_item_meta' 来实现此目的代码>

You can also achieve this by using the following woocommerce hooks woocommerce_before_add_to_cart_button, woocommerce_add_to_cart, woocommerce_cart_item_name,and 'woocommerce_add_order_item_meta'

喜欢在产品页面添加文本字段

like for adding text field to product page

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' );

要在购物车项目表上显示自定义字段,请使用以下内容

For displaying custom field on cart item table use the below

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;
    }
}
add_filter( 'woocommerce_cart_item_name', 'render_meta_on_cart_item', 1, 3 );

要根据您的订单详细信息制作自定义元数据,请执行以下操作

to make your custom meta data on you order details, do some thing like this

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

关于详细的实现,我有一篇关于如何在不使用任何插件的情况下做到这一点的文章.http://sarkware.com/how-to-pass-custom-data-to-cart-line-item-in-woocommerce-without-using-plugins/

for detailed implementation, i have an article about how to do this without using any plugins. http://sarkware.com/how-to-pass-custom-data-to-cart-line-item-in-woocommerce-without-using-plugins/

这篇关于WooCommerce:向购物车中的每个项目添加输入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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