Skip to content

Instantly share code, notes, and snippets.

@marcusig
Created November 20, 2023 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcusig/eaa3f770a0f05b667ce6b1fb6724ec04 to your computer and use it in GitHub Desktop.
Save marcusig/eaa3f770a0f05b667ce6b1fb6724ec04 to your computer and use it in GitHub Desktop.
Override an order line item's SKU using the compounded SKU when using WooCommerce's REST API
<?php
/**
* Filters the REST response for shop order object
*/
add_filter( 'woocommerce_rest_prepare_shop_order_object', function( $response ) {
if ( ! function_exists( 'mkl_pc' ) ) return $response;
$configurator = null;
if ( ! isset( $response->data['line_items'] ) ) return $response;
$sku_key = mkl_pc( 'settings')->get( 'sku_label', __( 'SKU', 'product-configurator-for-woocommerce' ) );
foreach( $response->data['line_items'] as $item_id => $line_item ) {
if ( isset( $line_item['meta_data'] ) ) {
foreach( $line_item['meta_data'] as $ind => $meta_data ) {
// Add the configurator to the first line
if ( $sku_key === $meta_data['key'] ) {
$response->data['line_items'][$item_id]['sku'] = $meta_data['value'];
continue 2;
}
}
}
}
return $response;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment