Ask your WordPress questions! Pay money and get answers fast! (more info)

WooCommerce update code for version 2.0 changes WordPress

I have code that won't work if I upgrade to 2.0 I guess because the hooks have changed. What the code does is adds a custom field to the cart/order and email receipt. I'd like this updated so it works for the newest version of WooCommerce. The custom field is called 'attribute_location' I gave it this name because WooCommerce automatically removed the 'attribute_' prefix and then formats it as such "Location: text goes here". Please note this is not a add to cart option, this is just additional information being added about the product.


/**
* Add custom meta data to order
**/
function order_item_custom_meta( $item_meta, $cart_item ) {

// Add the fields
//$fields = array('attribute_date', 'attribute_time', 'attribute_location');
$fields = array('attribute_location');

foreach( $fields as $key)
{
if ( isset( $cart_item['data']->product_custom_fields[$key][0] ) )
{
$item_meta->add( $key , $cart_item['data']->product_custom_fields[$key][0] );
}
}

}


add_filter( 'woocommerce_get_item_data', 'get_item_custom_data', 10, 2 );
/**
* Get product item data
**/
function get_item_custom_data( $other_data, $cart_item ) {



// Add the fields
//$fields = array('attribute_date', 'attribute_time', 'attribute_location');
$fields = array('attribute_location');

foreach( $fields as $key)
{
if ( isset( $cart_item['data']->product_custom_fields[$key][0] ) )
{
$other_data[] = array(
'name' => str_replace( 'attribute_', '', $key),
'value' => $cart_item['data']->product_custom_fields[$key][0],
'display' => ''
);
}
}

return $other_data;

}

Answers (2)

2013-08-08

Arnav Joy answers:

what version do you have currently ?
and for what first function is used for?


Arnav Joy comments:

there must also be any hook that is used for the first function


vanwpdev comments:

Let's pretend I have just started from scratch and I'm using the latest version of WooCommerce. I need to show custom meta per product on the cart, order details, and email receipt.

I just posted the code above as a reference to show how I had done it before, but that code no longer works, so it's irrelevant.


vanwpdev comments:

Ignore my code. I need all new code.


Arnav Joy comments:

try it

<?php

add_action( 'woocommerce_add_cart_item_data', 'order_item_custom_meta', 10, 2);


/**

* Add custom meta data to order

**/

function order_item_custom_meta( $item_meta, $cart_item ) {



// Add the fields

//$fields = array('attribute_date', 'attribute_time', 'attribute_location');

$fields = array('attribute_location');



foreach( $fields as $key)

{

if ( isset( $cart_item['data']->product_custom_fields[$key][0] ) )

{

$item_meta->add( $key , $cart_item['data']->product_custom_fields[$key][0] );

}

}



}





add_filter( 'woocommerce_get_item_data', 'get_item_custom_data', 10, 2 );

/**

* Get product item data

**/

function get_item_custom_data( $other_data, $cart_item ) {







// Add the fields

//$fields = array('attribute_date', 'attribute_time', 'attribute_location');

$fields = array('attribute_location');



foreach( $fields as $key)

{

if ( isset( $cart_item['data']->product_custom_fields[$key][0] ) )

{

$other_data[] = array(

'name' => str_replace( 'attribute_', '', $key),

'value' => $cart_item['data']->product_custom_fields[$key][0],

'display' => ''

);

}

}



return $other_data;



}


vanwpdev comments:

Thanks for youyr help Arnav, your code won't work though. The first one uses the wrong hook, and the second uses an outdated method that doesn't work anymore "product_custom_fields"

2013-08-08

John Cotton answers:

To add under 2.o you need something like this:


function my_add_order_item_meta( $item_id, $cart_item ) {
woocommerce_add_order_item_meta( $item_id, $meta_key, $meta_value, $unique );
}
add_action( 'woocommerce_add_order_item_meta', 'my_add_order_item_meta', 10, 2);


To get it back for display use:

woocommerce_get_order_item_meta( $item_id, $key, $single );


vanwpdev comments:

Thanks John,

Here's the solution. I guess the voters can divide the money among everyone. I don't mean to solve my own questions, but the reality is I'm just using WP questions as a safety net.


add_action( 'woocommerce_add_order_item_meta', 'order_item_custom_meta', 10, 2 );
/**
* Add custom meta data to order
**/
function order_item_custom_meta( $item_id, $cart_item ) {

// Add the fields
$fields = array('attribute_location');

$post_data = get_post( $cart_item['product_id'] );

foreach( $fields as $key)
{
$val = get_post_meta( $post_data->ID , $key , TRUE );

if ( isset( $val ) )
{
woocommerce_add_order_item_meta( $item_id, $key, $val );
}
}

}

add_filter( 'woocommerce_get_item_data', 'get_item_custom_data', 10, 2 );
/**
* Get product item data
**/
function get_item_custom_data( $other_data, $cart_item ) {



// Add the fields
$fields = array('attribute_location');

$post_data = get_post( $cart_item['product_id'] );

foreach( $fields as $key)
{
$val = get_post_meta( $post_data->ID , $key , TRUE );

if ( isset( $val ) )
{
$other_data[] = array(
'name' => str_replace( 'attribute_', '', $key),
'value' => $val,
'display' => ''
);
}
}

return $other_data;

}