Created
November 20, 2023 14:39
-
-
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
This file contains 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 | |
/** | |
* 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