Skip to content

Instantly share code, notes, and snippets.

@danielbitzer
Created August 1, 2019 07:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielbitzer/f08cfbca95e7bfa42bfd28c5c40713f7 to your computer and use it in GitHub Desktop.
Save danielbitzer/f08cfbca95e7bfa42bfd28c5c40713f7 to your computer and use it in GitHub Desktop.
[Refer A Friend] Add customer's referral coupon to order objects returned from REST API
<?php
add_filter( 'woocommerce_rest_prepare_shop_order_object', 'my_automatewoo_referrals_add_advocate_coupon_to_order_api', 10, 3 );
/**
* AutomateWoo Refer A Friend - Add customer's referral coupon to order objects returned from REST API
*
* Because guest customers can't be advocates only orders by registered users will have the code added.
*
* @param \WP_REST_Response $response
* @param \WC_Admin_Order $order
* @param \WP_REST_Request $request
*
* @return \WP_REST_Response
*/
function my_automatewoo_referrals_add_advocate_coupon_to_order_api( $response, $order, $request ) {
if ( false === class_exists( 'AW_Referrals_Addon' ) ) {
return $response;
}
$data = $response->get_data();
$advocate = AutomateWoo\Referrals\Advocate_Factory::get( $order->get_user_id() );
if ( $advocate && AW_Referrals()->options()->type === 'coupon' ) {
$data['automatewoo_referral_coupon'] = $advocate->get_shareable_coupon();
}
$response->set_data( $data );
return $response;
}
@seanonthenet
Copy link

🎉🎉🎉

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