Skip to content

Instantly share code, notes, and snippets.

@gabrielmerovingi
Last active February 19, 2019 12:38
Show Gist options
  • Save gabrielmerovingi/e9e1937e0cb50e2a0ee2a0094e614903 to your computer and use it in GitHub Desktop.
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+
/**
* 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' );
@nummell
Copy link

nummell commented Feb 16, 2018

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?

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