Last active
February 19, 2019 12:38
-
-
Save gabrielmerovingi/e9e1937e0cb50e2a0ee2a0094e614903 to your computer and use it in GitHub Desktop.
Example: Reward buyer with 1 point for each product in an order, once the order is marked as completed. Requires WooCommerce 3.0+
This file contains hidden or 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
/** | |
* Reward Completed Orders | |
* Will give a user 1 point for each product in an order. | |
* @version 1.0.1 | |
*/ | |
function mycred_pro_reward_completed_orders( $order_id ) { | |
if ( ! function_exists( 'mycred' ) ) return; | |
$order = wc_get_order( $order_id ); | |
$user_id = $order->get_user_id(); | |
$point_type_key = 'something'; | |
$mycred = mycred( $point_type_key ); | |
if ( ! $mycred->exclude_user( $user_id ) ) { | |
foreach ( $order->get_items() as $item ) { | |
$mycred->add_creds( | |
'book_return', | |
$user_id, | |
1, | |
'Returned book', | |
$item['product_id'], | |
array( 'ref_type' => 'post' ), | |
$point_type_key | |
); | |
} | |
} | |
} | |
add_action( 'woocommerce_order_status_completed', 'mycred_pro_reward_completed_orders' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Really cool example.
Is it possible to change this to give 1 custom point per order? (no matter what value or products)
Thanks in advance?