Saving a product custom field and displaying it in cart page

后端 未结 1 1962
悲哀的现实
悲哀的现实 2020-12-03 20:07

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

相关标签:
1条回答
  • 2020-12-03 20:49

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

    1) SETTINGS:

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

    Create a product attribute:

    Add some value to this attribute:

    and …

    Then save…

    2) The Code:

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

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

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

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