Skip to content

Instantly share code, notes, and snippets.

@bhavik-kiri
Last active November 11, 2017 11:05
Show Gist options
  • Save bhavik-kiri/06a4d1cb27d0a32619f5a64677371e33 to your computer and use it in GitHub Desktop.
Save bhavik-kiri/06a4d1cb27d0a32619f5a64677371e33 to your computer and use it in GitHub Desktop.
<?php
/**
* Add data to cart item
*/
add_filter( 'woocommerce_add_cart_item_data', 'add_cart_item_data', 25, 2 );
function add_cart_item_data( $cart_item_meta, $product_id ) {
if ( isset( $_POST ['customer_name'] ) && isset( $_POST ['customer_message'] ) ) {
$custom_data = array() ;
$custom_data [ 'customer_name' ] = isset( $_POST ['customer_name'] ) ? sanitize_text_field ( $_POST ['customer_name'] ) : "" ;
$custom_data [ 'customer_message' ] = isset( $_POST ['customer_message'] ) ? sanitize_text_field ( $_POST ['customer_message'] ): "" ;
$cart_item_meta ['custom_data'] = $custom_data ;
}
return $cart_item_meta;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment