designbymerovingi

myCRED: Award points for WooCommerce purchase

Jan 21st, 2014
806
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1. /**
  2.  * Award myCRED Points for WooCommerce Products
  3.  * Will check for a custom post meta "mycred" for each
  4.  * product in an order and award points according to it's
  5.  * value.
  6.  * Place this code either in your themes functions.php file
  7.  * or in your custom plugin.
  8.  * @requires myCRED 1.1 +
  9.  * @version 1.0
  10.  */
  11. add_action( 'woocommerce_payment_complete', 'mycred_pro_add_points_for_product_purchase' );
  12. function mycred_pro_add_points_for_product_purchase( $order_id ) {
  13.     // If myCRED is not installed, bail
  14.     if ( ! function_exists( 'mycred_get_settings' ) ) return;
  15.  
  16.     // Get Order
  17.     $order = new WC_Order( $order_id );
  18.  
  19.     // Load Mycred
  20.     $mycred = mycred();
  21.  
  22.     // Get items in order
  23.     $items = $order->get_items();
  24.    
  25.     // Loop though items
  26.     foreach ( $items as $item ) {
  27.  
  28.         // Get product ID
  29.         $product_id = absint( $item['product_id'] );
  30.  
  31.         // Get our custom field
  32.         $value = get_post_meta( $product_id, 'mycred', true );
  33.  
  34.         // If no value is set, continue to the next product
  35.         if ( empty( $value ) ) continue;
  36.  
  37.         // Item quantity
  38.         $count = 0;
  39.         if ( ! empty( $item['qty'] ) )
  40.             $count += $item['qty'];
  41.         else
  42.             $count ++;
  43.  
  44.         // Prep amount
  45.         $value = $mycred->number( $value*$count );
  46.  
  47.         // Add points
  48.         $mycred->add_creds(
  49.             'store_topup',
  50.             $order->user_id,
  51.             $value,
  52.             '%link_with_title%',
  53.             $product_id,
  54.             array( 'ref_type' => 'post' )
  55.         );
  56.  
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment