Last active
November 24, 2021 20:48
-
-
Save igorbenic/8691e50a666a5e740a5345f53438d12d to your computer and use it in GitHub Desktop.
How to manage Order Item Data in WooCommerce | https://www.ibenic.com/manage-order-item-meta-woocommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// WC_Checkout | |
/** | |
* Add coupon lines to the order. | |
* | |
* @param WC_Order $order Order instance. | |
* @param WC_Cart $cart Cart instance. | |
*/ | |
public function create_order_coupon_lines( &$order, $cart ) { | |
foreach ( $cart->get_coupons() as $code => $coupon ) { | |
$item = new WC_Order_Item_Coupon(); | |
// ... code removed for reading purposes | |
/** | |
* Action hook to adjust item before save. | |
* | |
* @since 3.0.0 | |
*/ | |
do_action( 'woocommerce_checkout_create_order_coupon_item', $item, $code, $coupon, $order ); | |
// Add item to order and save. | |
$order->add_item( $item ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// WC_Checkout | |
/** | |
* Add fees to the order. | |
* | |
* @param WC_Order $order Order instance. | |
* @param WC_Cart $cart Cart instance. | |
*/ | |
public function create_order_fee_lines( &$order, $cart ) { | |
foreach ( $cart->get_fees() as $fee_key => $fee ) { | |
$item = new WC_Order_Item_Fee(); | |
// ... code removed for reading purposes. | |
/** | |
* Action hook to adjust item before save. | |
* | |
* @since 3.0.0 | |
*/ | |
do_action( 'woocommerce_checkout_create_order_fee_item', $item, $fee_key, $fee, $order ); | |
// Add item to order and save. | |
$order->add_item( $item ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'woocommerce_hidden_order_itemmeta', 'hide_my_item_meta' ); | |
/** | |
* Hiding item meta | |
* | |
* @param array $hidden_meta Array of all meta data to hide. | |
* | |
* @return array | |
*/ | |
function hide_my_item_meta( $hidden_meta ) { | |
// Let's hide the meta 'something'. | |
$hidden_meta[] = 'something'; | |
return $hidden_meta; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'woocommerce_order_item_display_meta_key', 'change_order_item_meta_title', 20, 3 ); | |
/** | |
* Changing a meta title | |
* @param string $key The meta key | |
* @param WC_Meta_Data $meta The meta object | |
* @param WC_Order_Item $item The order item object | |
* @return string The title | |
*/ | |
function change_order_item_meta_title( $key, $meta, $item ) { | |
// By using $meta-key we are sure we have the correct one. | |
if ( 'something' === $meta->key ) { $key = 'SOMETHING'; } | |
return $key; | |
} | |
add_filter( 'woocommerce_order_item_display_meta_value', 'change_order_item_meta_value', 20, 3 ); | |
/** | |
* Changing a meta value | |
* @param string $value The meta value | |
* @param WC_Meta_Data $meta The meta object | |
* @param WC_Order_Item $item The order item object | |
* @return string The title | |
*/ | |
function change_order_item_meta_value( $value, $meta, $item ) { | |
// By using $meta-key we are sure we have the correct one. | |
if ( 'something' === $meta->key ) { $value = 'SOMETHING'; } | |
return $value; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'woocommerce_order_item_get_formatted_meta_data', 'change_formatted_meta_data', 20, 2 ); | |
/** | |
* Filterting the meta data of an order item. | |
* @param array $meta_data Meta data array | |
* @param WC_Order_Item $item Item object | |
* @return array The formatted meta | |
*/ | |
function change_formatted_meta_data( $meta_data, $item ) { | |
$new_meta = array(); | |
foreach ( $meta_data as $id => $meta_array ) { | |
// We are removing the meta with the key 'something' from the whole array. | |
if ( 'something' === $meta_array->key ) { continue; } | |
$new_meta[ $id ] = $meta_array; | |
} | |
return $new_meta; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// class WC_Checkout | |
/** | |
* Add line items to the order. | |
* | |
* @param WC_Order $order Order instance. | |
* @param WC_Cart $cart Cart instance. | |
*/ | |
public function create_order_line_items( &$order, $cart ) { | |
foreach ( $cart->get_cart() as $cart_item_key => $values ) { | |
/** | |
* Filter hook to get initial item object. | |
* | |
* @since 3.1.0 | |
*/ | |
$item = apply_filters( 'woocommerce_checkout_create_order_line_item_object', new WC_Order_Item_Product(), $cart_item_key, $values, $order ); | |
// ... code removed for reading purposes | |
/** | |
* Action hook to adjust item before save. | |
* | |
* @since 3.0.0 | |
*/ | |
do_action( 'woocommerce_checkout_create_order_line_item', $item, $cart_item_key, $values, $order ); | |
// Add item to order and save. | |
$order->add_item( $item ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'woocommerce_checkout_create_order_shipping_item', 'order_shipping_item', 20, 4 ); | |
/** | |
* @param $item | |
* @param $package_key | |
* @param $package | |
*/ | |
function order_shipping_item( &$item, $package_key, $package, $order ) { | |
// Let's assume that we have a textarea field as a shipping note on the checkout field. | |
// This can be a note for the delivery company, for example: "Deliver between 12:00 and 19:00" | |
$note = isset( $_POST['shipping_note'] ) ? sanitize_text_field( $_POST['shipping_note'] ) : ''; | |
if ( $note ) { | |
$item->add_meta_data( '_note', $note, true ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// WC_Checkout | |
/** | |
* Add shipping lines to the order. | |
* | |
* @param WC_Order $order Order Instance. | |
* @param array $chosen_shipping_methods Chosen shipping methods. | |
* @param array $packages Packages. | |
*/ | |
public function create_order_shipping_lines( &$order, $chosen_shipping_methods, $packages ) { | |
foreach ( $packages as $package_key => $package ) { | |
if ( isset( $chosen_shipping_methods[ $package_key ], $package['rates'][ $chosen_shipping_methods[ $package_key ] ] ) ) { | |
$shipping_rate = $package['rates'][ $chosen_shipping_methods[ $package_key ] ]; | |
$item = new WC_Order_Item_Shipping(); | |
// ... code removed for reading purposes. | |
/** | |
* Action hook to adjust item before save. | |
* | |
* @since 3.0.0 | |
*/ | |
do_action( 'woocommerce_checkout_create_order_shipping_item', $item, $package_key, $package, $order ); | |
// Add item to order and save. | |
$order->add_item( $item ); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'woocommerce_order_item_display_meta_key', 'change_shipping_note_title', 20, 3 ); | |
/** | |
* Changing the shipping note title | |
* @param string $key The meta key | |
* @param WC_Meta_Data $meta The meta object | |
* @param WC_Order_Item $item The order item object | |
* @return string The title | |
*/ | |
function change_shipping_note_title( $key, $meta, $item ) { | |
// By using $meta-key we are sure we have the correct one. | |
if ( '_note' === $meta->key ) { $key = __( 'Note', 'your_textdomain'); } | |
return $key; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// WC_Checkout | |
/** | |
* Add tax lines to the order. | |
* | |
* @param WC_Order $order Order instance. | |
* @param WC_Cart $cart Cart instance. | |
*/ | |
public function create_order_tax_lines( &$order, $cart ) { | |
foreach ( array_keys( $cart->get_cart_contents_taxes() + $cart->get_shipping_taxes() + $cart->get_fee_taxes() ) as $tax_rate_id ) { | |
if ( $tax_rate_id && apply_filters( 'woocommerce_cart_remove_taxes_zero_rate_id', 'zero-rated' ) !== $tax_rate_id ) { | |
$item = new WC_Order_Item_Tax(); | |
// ... code removed for reading purposes | |
/** | |
* Action hook to adjust item before save. | |
* | |
* @since 3.0.0 | |
*/ | |
do_action( 'woocommerce_checkout_create_order_tax_item', $item, $tax_rate_id, $order ); | |
// Add item to order and save. | |
$order->add_item( $item ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment