Created
August 1, 2019 07:23
-
-
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
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 | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
🎉🎉🎉