Advertisement
majeedraza

WooCommerce - Custom meta data on the cart and checkout pages

Mar 12th, 2021
503
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  1. <?php
  2. // WooCommerce - Custom meta data on the cart and checkout pages
  3.  
  4. add_filter( 'woocommerce_get_item_data', 'tp_get_custom_item_data', 10, 2 );
  5. function tp_get_custom_item_data( $item_data, $cart_item ) {
  6.   if( isset( $cart_item['size'] ) ) {  
  7.     $item_data[] = array( 'name' => 'Size', 'value' => $cart_item['size'] );
  8.   }
  9.   return $item_data;
  10. }
  11.  
  12. // add custom meta to order
  13. add_action( 'woocommerce_new_order_item', 'tp_add_custom_meta_to_order', 99, 3 );
  14. function tp_add_custom_meta_to_order( $item_id, $item, $order_id ) {
  15.   if( !isset( $item->legacy_values ) ) { // applies to order_item_shipping
  16.     return;
  17.   }
  18.   if( isset( $item->legacy_values['size'] ) ) {
  19.     wc_add_order_item_meta( $item_id, 'Size', $item->legacy_values['size'] );
  20.   }
  21. } // end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement