Saving a product custom field and displaying it in

2020-01-29 12:24发布

问题:

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

But I can't get any value for this custom field.

How it's possible? What I am doing wrong?

Thanks

回答1:

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+