Skip to content

Instantly share code, notes, and snippets.

@kilbot
Last active August 17, 2017 20:15
Show Gist options
  • Save kilbot/8cc2aa618209e5dea8e9bb8a7e60d537 to your computer and use it in GitHub Desktop.
Save kilbot/8cc2aa618209e5dea8e9bb8a7e60d537 to your computer and use it in GitHub Desktop.
Changing the product regular_price for WC REST API v2 output
<?php
// the code below goes in your functions.php file
function my_custom_prepare_shop_order_object($response, $order, $request) {
if( is_pos() ) {
$data = $response->get_data();
$data['regular_price'] = get_post_meta( $data['id'], 'wholesale_customer_wholesale_price', true );
$response->set_data($data);
}
return $response;
}
add_filter( 'woocommerce_rest_prepare_product_object', 'my_custom_prepare_product_object', 20, 3 );
add_filter( 'woocommerce_rest_prepare_product_variation_object', 'my_custom_prepare_product_object', 20, 3 );
@teammartinmiller
Copy link

teammartinmiller commented Aug 17, 2017

The code above had some problems, so I tried to troubleshoot and came up with this...it's not giving an error, but no products display in the POS.

<?php
function my_custom_prepare_product_object($response, $product, $request) {
  if( is_pos() ) {
    $data = $response->get_data();
    $data['price'] = get_post_meta( $data['id'], 'wholesale_customer_wholesale_price', true );
    $response->set_data($data);
  }
  return $response;
}

add_filter( 'woocommerce_rest_prepare_product_object', array( $this, 'my_custom_prepare_product_object' ), 20, 3 );
add_filter( 'woocommerce_rest_prepare_product_variation_object', array( $this, 'my_custom_prepare_product_object' ), 20, 3 );

?>

@kilbot
Copy link
Author

kilbot commented Aug 17, 2017

Sorry, my mistake, I copied the add_filter lines from my plugin code ... it should be a slightly different format for the functions.php file. I've just corrected the code.

@teammartinmiller
Copy link

Cool. Does there need to be the word "function" in front of line 5?

@kilbot
Copy link
Author

kilbot commented Aug 17, 2017

yes, just updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment