WooCommerce: Add input field to every item in cart

前端 未结 3 507
灰色年华
灰色年华 2020-11-29 02:17

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 y

相关标签:
3条回答
  • 2020-11-29 02:24

    It's easy. Try searching and reading code of Woocommerce.

    This much of code got me to point where I can add Url @ Cart. And I can see it in Order review as customer and as admin.

    I can't test email because I'm lazy. I'm sorry.

    Something like this goes in templates\cart\cart.php (there is need for some more code as it's seperate column)

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

    Functions.php

    // get from session your URL variable and add it to item
    add_filter('woocommerce_get_cart_item_from_session', 'cart_item_from_session', 99, 3);
    function cart_item_from_session( $data, $values, $key ) {
        $data['url'] = isset( $values['url'] ) ? $values['url'] : '';
        return $data;
    }
    
    // this one does the same as woocommerce_update_cart_action() in plugins\woocommerce\woocommerce-functions.php
    // but with your URL variable
    // this might not be the best way but it works
    add_action( 'init', 'update_cart_action', 9);
    function update_cart_action() {
        global $woocommerce;
        if ( ( ! empty( $_POST['update_cart'] ) || ! empty( $_POST['proceed'] ) ) && $woocommerce->verify_nonce('cart')) {
            $cart_totals = isset( $_POST['cart'] ) ? $_POST['cart'] : '';
            if ( sizeof( $woocommerce->cart->get_cart() ) > 0 ) {
                foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
                    if ( isset( $cart_totals[ $cart_item_key ]['url'] ) ) {
                        $woocommerce->cart->cart_contents[ $cart_item_key ]['url'] = $cart_totals[ $cart_item_key ]['url'];
                    }
                }
            }
        }
    }
    
    // this is in Order summary. It show Url variable under product name. Same place where Variations are shown.
    add_filter( 'woocommerce_get_item_data', 'item_data', 10, 2 );
    function item_data( $data, $cart_item ) {
        if ( isset( $cart_item['url'] ) ) {
            $data['url'] = array('name' => 'Url', 'value' => $cart_item['url']);
        }
        return $data;
    }
    
    // this adds Url as meta in Order for item
    add_action ('woocommerce_add_order_item_meta', 'add_item_meta', 10, 2);
    function add_item_meta( $item_id, $values ) {
        woocommerce_add_order_item_meta( $item_id, 'Url', $values['url'] );
    }
    

    400$ is nice price.

    0 讨论(0)
  • 2020-11-29 02:35

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

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

    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/

    0 讨论(0)
  • 2020-11-29 02:49

    You can do this fairly easily with the Woocommerce Product Add-ons plugin

    From the WooThemes website:

    Allow your customers to customise your products by adding new options such as input boxes, dropdowns or checkboxes. With the Product Add-ons extension, gift messages, donations, laser engraving and any other product which may require user input in some way is now an option for your customers!

    Product add-ons supports required fields, textareas, checkboxes, radios, select boxes, custom price inputs and file upload boxes.

    I've used it before to add an additional donation field to a product purchase and display that on the thank you page/receipt email.

    It's about $50 US and will get you up and running in no time to add the text input and display the field on thank you page/email like you want. $50 is definitely worth the amount of time you'd save drying to develop this feature yourself.

    Here's the flow from the end-users perspective:

    End user enters in field data and adds product to cart End User enters in field data and adds product to cart

    When User views cart the data they entered into the custom field displays along with the product When User views cart the data they entered into the custom field displays along with the product

    After purchase, end user lands on thank you page and receives receipt email with field data included with the product item. After purchase, end user lands on thank you page and receives receipt email with field data included with the product item.

    On the backend:

    Create custom field for each product. The option is located on the Add-Ons menu tab.

    1. Create a new Addon Group
    2. Enter a group name (for easier organization)
    3. Add a new option and enter the label (this is what the end user sees and is tied to the product)
    4. Update/Publish product.

    Create custom field for each product

    For completed orders, this is what you will see in the Order Details admin:

    product order details page also displays the field

    Hope this helps and saves you a ton of time in development!

    0 讨论(0)
提交回复
热议问题