Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Award myCRED Points for WooCommerce Products
- * Will check for a custom post meta "mycred" for each
- * product in an order and award points according to it's
- * value.
- * Place this code either in your themes functions.php file
- * or in your custom plugin.
- * @requires myCRED 1.1 +
- * @version 1.0
- */
- add_action( 'woocommerce_payment_complete', 'mycred_pro_add_points_for_product_purchase' );
- function mycred_pro_add_points_for_product_purchase( $order_id ) {
- // If myCRED is not installed, bail
- if ( ! function_exists( 'mycred_get_settings' ) ) return;
- // Get Order
- $order = new WC_Order( $order_id );
- // Load Mycred
- $mycred = mycred();
- // Get items in order
- $items = $order->get_items();
- // Loop though items
- foreach ( $items as $item ) {
- // Get product ID
- $product_id = absint( $item['product_id'] );
- // Get our custom field
- $value = get_post_meta( $product_id, 'mycred', true );
- // If no value is set, continue to the next product
- if ( empty( $value ) ) continue;
- // Item quantity
- $count = 0;
- if ( ! empty( $item['qty'] ) )
- $count += $item['qty'];
- else
- $count ++;
- // Prep amount
- $value = $mycred->number( $value*$count );
- // Add points
- $mycred->add_creds(
- 'store_topup',
- $order->user_id,
- $value,
- '%link_with_title%',
- $product_id,
- array( 'ref_type' => 'post' )
- );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment