Advertisement
designbymerovingi

Reward Woo Order with 2 Points / Product

Apr 10th, 2016
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.13 KB | None | 0 0
  1. /**
  2.  * Pay 2 Points / Product
  3.  * Rewards a buyer with 2 points per product they purchase
  4.  * using WooCommerce.
  5.  * @version 1.0
  6.  */
  7. add_action( 'woocommerce_payment_complete', 'mycred_pro_add_points_for_product_purchase' );
  8. function mycred_pro_add_points_for_product_purchase( $order_id ) {
  9.  
  10.     if ( ! function_exists( 'mycred' ) ) return;
  11.  
  12.     $order = wc_get_order( $order_id );
  13.     if ( $order->order_total == 0.00 || $order->order_total == 0 || $order->payment_method == 'mycred' ) return;
  14.  
  15.     $point_type = 'mycred_default';
  16.     $mycred = mycred( $point_type );
  17.  
  18.     if ( $mycred->has_entry( 'shopping_reward', $order_id, $order->user_id ) ) return;
  19.  
  20.     // Calculate reward
  21.     $reward = 0;
  22.     $items = $order->get_items();
  23.     foreach ( $items as $item ) {
  24.         $reward += 2 * $item['qty'];
  25.     }
  26.  
  27.     // Add reward
  28.     $mycred->add_creds(
  29.         'shopping_reward',
  30.         $order->user_id,
  31.         $reward,
  32.         'Reward for store purchase',
  33.         $order_id,
  34.         array( 'ref_type' => 'post' ),
  35.         $point_type
  36.     );
  37.  
  38.     $message = '%s points paid as reward';
  39.     $message = str_replace( '%s', $mycred->format_creds( $reward ), $message );
  40.  
  41.     $order->add_order_note( $message, 1 );
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement