Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Woo Point Rewards by Category
- * Reward buyes with points for buying products.
- * @version 1.0
- */
- function mycred_woo_payout_reward_per_category( $order_id ) {
- // Make sure we do not crash our site if myCRED gets disabled
- if ( ! function_exists( 'mycred' ) ) return;
- // Setup
- $payouts = array(
- 'category-one' => 10,
- 'category-two' => 15,
- 'category-three' => 20
- );
- // Get Order
- $order = wc_get_order( $order_id );
- // Get items in order
- $items = $order->get_items();
- $reward = 0;
- // Loop through each item to get the category they are in
- foreach ( $items as $item ) {
- $product_id = absint( $item['product_id'] );
- // Get all the categories this product is in
- $categories = wp_get_object_terms( $product_id, 'product_cat', array( 'fields' => 'slugs' ) );
- // Default payout is 10%
- $percentage_to_pay = 10;
- if ( ! empty( $categories ) ) {
- // Assuming each product is only in maximum 1 category!
- $category_slug = $categories[0];
- // Find where our category slug fits in our $payouts setup
- if ( array_key_exists( $category_slug, $payouts ) )
- $percentage_to_pay = $payouts[ $category_slug ];
- }
- // Calculate reward = x (percentage) of ( product price x quantity )
- $reward += ( ( $item['line_subtotal'] * $item['qty'] ) * ( 1 + ( $percentage_to_pay / 100 ) ) );
- }
- // Zero points = no payout
- if ( $reward == 0 ) return;
- // Load myCRED
- $mycred = mycred();
- // Make sure we only payout once per order
- if ( ! $mycred->has_entry( 'reward', $order_id, $order->user_id ) )
- $mycred->add_creds(
- 'reward',
- $order->user_id,
- $reward,
- 'Points reward for store purchase',
- $order_id,
- array( 'ref_type' => 'post' )
- );
- }
- add_action( 'woocommerce_payment_complete', 'mycred_woo_payout_reward_per_category' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement