Woocommerce 将输入字段添加到购物车中的每个项目

Woocommerce 将输入字段添加到购物车中的每个项目,woocommerce,field,cart,Woocommerce,Field,Cart,我正在尝试在cart.php文件中添加一个新字段 我实际上想插入一个URL字段,这样用户就可以为每个订单项设置一个URL 我试图使用另一篇文章中的代码,但我无法让它工作 第一个和第二个函数正在工作,但当涉及到第三个函数时,'woocommerce\u get\u item\u data'$cart\u item['url']不包含任何内容,即使我在字段中添加了一些内容并按了Update cart 加载页面时,来自第一个函数的$cart_totals[$cart_item_key]['url']正

我正在尝试在cart.php文件中添加一个新字段

我实际上想插入一个URL字段,这样用户就可以为每个订单项设置一个URL

我试图使用另一篇文章中的代码,但我无法让它工作

第一个和第二个函数正在工作,但当涉及到第三个函数时,'woocommerce\u get\u item\u data'$cart\u item['url']不包含任何内容,即使我在字段中添加了一些内容并按了Update cart

加载页面时,来自第一个函数的$cart_totals[$cart_item_key]['url']正在输出正确的值

我不知道现在该怎么办,谢谢你的帮助。 这是密码

添加字段 cart/cart.php

<td class="product-url">
    <?php
        $html = sprintf( '<div class="url"><input type="text" name="cart[%s][url]" value="%s" size="4" title="Url" class="input-text url text" /></div>', $cart_item_key, esc_attr( $values['url'] ) );
        echo $html;
    ?>
</td>
向购物车项目添加textarea字段

首先,我们只需要添加textarea字段。我们使用woocommerce\u after\u cart\u item\u name钩子,因此我们的文本区域将显示在产品名称之后

<?php
/**
 * Add a text field to each cart item
 */
function prefix_after_cart_item_name( $cart_item, $cart_item_key ) {
 $notes = isset( $cart_item['notes'] ) ? $cart_item['notes'] : '';
 printf(
 '<div><textarea class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea></div>',
 'prefix-cart-notes',
 $cart_item_key,
 $cart_item_key,
 $notes
 );
}
add_action( 'woocommerce_after_cart_item_name', 'prefix_after_cart_item_name', 10, 2 );

/**
 * Enqueue our JS file
 */
function prefix_enqueue_scripts() {
 wp_register_script( 'prefix-script', trailingslashit( plugin_dir_url( __FILE__ ) ) . 'update-cart-item-ajax.js', array( 'jquery-blockui' ), time(), true );
 wp_localize_script(
 'prefix-script',
 'prefix_vars',
 array(
 'ajaxurl' => admin_url( 'admin-ajax.php' )
 )
 );
 wp_enqueue_script( 'prefix-script' );
}
add_action( 'wp_enqueue_scripts', 'prefix_enqueue_scripts' );
´´´
At the moment, the user will be able to enter text into the field but the text won’t save. We are going to use some AJAX to save the text.

The code above not only adds the textarea to the cart item, it also enqueues a JavaScript file ready for our AJAX.

It’s assumed that you’re using the code on this page to create a new plugin. If so, you should create a new JS file with the code below and place the file in the root directory of your plugin.

However, if you’ve added the PHP above to your theme functions.php or as a snippet on your site, you’ll need to change the location of the JS file by updating line 21 of the snippet above to identify the location of the JS file.

´´´
(function($){
 $(document).ready(function(){
 $('.prefix-cart-notes').on('change keyup paste',function(){
 $('.cart_totals').block({
 message: null,
 overlayCSS: {
 background: '#fff',
 opacity: 0.6
 }
 });
 var cart_id = $(this).data('cart-id');
 $.ajax(
 {
 type: 'POST',
 url: prefix_vars.ajaxurl,
 data: {
 action: 'prefix_update_cart_notes',
 security: $('#woocommerce-cart-nonce').val(),
 notes: $('#cart_notes_' + cart_id).val(),
 cart_id: cart_id
 },
 success: function( response ) {
 $('.cart_totals').unblock();
 }
 }
 )
 });
 });
})(jQuery);

´´´

Now, when the user types anything, the contents of the text field get sent back to the server ready to be saved as meta data to the cart item.

´´´
<?php
/**
 * Update cart item notes
 */
function prefix_update_cart_notes() {
 // Do a nonce check
 if( ! isset( $_POST['security'] ) || ! wp_verify_nonce( $_POST['security'], 'woocommerce-cart' ) ) {
 wp_send_json( array( 'nonce_fail' => 1 ) );
 exit;
 }
 // Save the notes to the cart meta
 $cart = WC()->cart->cart_contents;
 $cart_id = $_POST['cart_id'];
 $notes = $_POST['notes'];
 $cart_item = $cart[$cart_id];
 $cart_item['notes'] = $notes;
 WC()->cart->cart_contents[$cart_id] = $cart_item;
 WC()->cart->set_session();
 wp_send_json( array( 'success' => 1 ) );
 exit;
}
add_action( 'wp_ajax_prefix_update_cart_notes', 'prefix_update_cart_notes' );

function prefix_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
 foreach( $item as $cart_item_key=>$cart_item ) {
 if( isset( $cart_item['notes'] ) ) {
 $item->add_meta_data( 'notes', $cart_item['notes'], true );
 }
 }
}
add_action( 'woocommerce_checkout_create_order_line_item', 'prefix_checkout_create_order_line_item', 10, 4 );

´´´

The prefix_update_cart_notes function does a security check using the WooCommerce cart nonce then saves the content of the textarea as meta data in the cart item. You can check out this article for more information about updating cart meta for items that have already been added to the cart.

Add the custom text to the order meta
Finally, we want to pass our meta data to the order so that we can use it after the customer has checked out. The prefix_checkout_create_order_line_item function takes care of that, iterating through each item and saving notes when it finds them.


https://pluginrepublic.com/how-to-add-an-input-field-to-woocommerce-cart-items/
<?php
/**
 * Add a text field to each cart item
 */
function prefix_after_cart_item_name( $cart_item, $cart_item_key ) {
 $notes = isset( $cart_item['notes'] ) ? $cart_item['notes'] : '';
 printf(
 '<div><textarea class="%s" id="cart_notes_%s" data-cart-id="%s">%s</textarea></div>',
 'prefix-cart-notes',
 $cart_item_key,
 $cart_item_key,
 $notes
 );
}
add_action( 'woocommerce_after_cart_item_name', 'prefix_after_cart_item_name', 10, 2 );

/**
 * Enqueue our JS file
 */
function prefix_enqueue_scripts() {
 wp_register_script( 'prefix-script', trailingslashit( plugin_dir_url( __FILE__ ) ) . 'update-cart-item-ajax.js', array( 'jquery-blockui' ), time(), true );
 wp_localize_script(
 'prefix-script',
 'prefix_vars',
 array(
 'ajaxurl' => admin_url( 'admin-ajax.php' )
 )
 );
 wp_enqueue_script( 'prefix-script' );
}
add_action( 'wp_enqueue_scripts', 'prefix_enqueue_scripts' );
´´´
At the moment, the user will be able to enter text into the field but the text won’t save. We are going to use some AJAX to save the text.

The code above not only adds the textarea to the cart item, it also enqueues a JavaScript file ready for our AJAX.

It’s assumed that you’re using the code on this page to create a new plugin. If so, you should create a new JS file with the code below and place the file in the root directory of your plugin.

However, if you’ve added the PHP above to your theme functions.php or as a snippet on your site, you’ll need to change the location of the JS file by updating line 21 of the snippet above to identify the location of the JS file.

´´´
(function($){
 $(document).ready(function(){
 $('.prefix-cart-notes').on('change keyup paste',function(){
 $('.cart_totals').block({
 message: null,
 overlayCSS: {
 background: '#fff',
 opacity: 0.6
 }
 });
 var cart_id = $(this).data('cart-id');
 $.ajax(
 {
 type: 'POST',
 url: prefix_vars.ajaxurl,
 data: {
 action: 'prefix_update_cart_notes',
 security: $('#woocommerce-cart-nonce').val(),
 notes: $('#cart_notes_' + cart_id).val(),
 cart_id: cart_id
 },
 success: function( response ) {
 $('.cart_totals').unblock();
 }
 }
 )
 });
 });
})(jQuery);

´´´

Now, when the user types anything, the contents of the text field get sent back to the server ready to be saved as meta data to the cart item.

´´´
<?php
/**
 * Update cart item notes
 */
function prefix_update_cart_notes() {
 // Do a nonce check
 if( ! isset( $_POST['security'] ) || ! wp_verify_nonce( $_POST['security'], 'woocommerce-cart' ) ) {
 wp_send_json( array( 'nonce_fail' => 1 ) );
 exit;
 }
 // Save the notes to the cart meta
 $cart = WC()->cart->cart_contents;
 $cart_id = $_POST['cart_id'];
 $notes = $_POST['notes'];
 $cart_item = $cart[$cart_id];
 $cart_item['notes'] = $notes;
 WC()->cart->cart_contents[$cart_id] = $cart_item;
 WC()->cart->set_session();
 wp_send_json( array( 'success' => 1 ) );
 exit;
}
add_action( 'wp_ajax_prefix_update_cart_notes', 'prefix_update_cart_notes' );

function prefix_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
 foreach( $item as $cart_item_key=>$cart_item ) {
 if( isset( $cart_item['notes'] ) ) {
 $item->add_meta_data( 'notes', $cart_item['notes'], true );
 }
 }
}
add_action( 'woocommerce_checkout_create_order_line_item', 'prefix_checkout_create_order_line_item', 10, 4 );

´´´

The prefix_update_cart_notes function does a security check using the WooCommerce cart nonce then saves the content of the textarea as meta data in the cart item. You can check out this article for more information about updating cart meta for items that have already been added to the cart.

Add the custom text to the order meta
Finally, we want to pass our meta data to the order so that we can use it after the customer has checked out. The prefix_checkout_create_order_line_item function takes care of that, iterating through each item and saving notes when it finds them.


https://pluginrepublic.com/how-to-add-an-input-field-to-woocommerce-cart-items/