Advertisement
designbymerovingi

woo-custom-price-with-order-update

Jan 25th, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. /**
  2. * Adjust Woo Price in Points
  3. * If an order item has the product attribute "size"
  4. * and the size is "large", deduct the extra cost the variation
  5. * enforces when paying with points.
  6. * @version 1.1
  7. */
  8. function mycredpro_adjust_point_cost_woo( $cost, $order, $is_cart, $this ) {
  9.  
  10. if ( $is_cart ) return $cost;
  11.  
  12. $items = $order->get_items();
  13.  
  14. $amount_to_deduct = 0;
  15. $product_factory = new WC_Product_Factory();
  16. foreach ( $items as $item_id => $item ) {
  17.  
  18. if ( ! isset( $item['pa_size'] ) ) continue;
  19.  
  20. $sizes_that_cost_extra = array( 'L', 'XL', 'XXL' );
  21.  
  22. if ( in_array( $item['pa_size'], $sizes_that_cost_extra ) ) {
  23.  
  24. $product = $product_factory->get_product( $item['product_id'] );
  25. $amount_to_deduct = $amount_to_deduct + ( $item['qty'] * 2 );
  26.  
  27. $order->update_product( $item_id, $product, array(
  28. 'qty' => $item['qty'],
  29. 'totals' => array(
  30. 'subtotal' => ( $product->get_price() - 2 )
  31. )
  32. ) );
  33.  
  34. }
  35.  
  36. }
  37.  
  38. if ( $amount_to_deduct > 0 ) {
  39.  
  40. $order->calculate_totals();
  41.  
  42. $cost = $cost - $amount_to_deduct;
  43.  
  44. }
  45.  
  46. return $cost;
  47.  
  48. }
  49. add_filter( 'mycred_woo_order_cost', 'mycredpro_adjust_point_cost_woo', 10, 4 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement