Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[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

seanonthenet commented Aug 1, 2019

🎉🎉🎉

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