Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_price', 10, 1 );
  2. function conditionally_change_cart_items_price( $cart_object ) {
  3.  
  4. if ( is_admin() && ! defined( 'DOING_AJAX' ) )
  5. return;
  6.  
  7.  
  8. $targeted_product_id = 1107;
  9.  
  10. // Set Here your custom price (1st purshase)
  11. $custom_price = 100; // First purshase for product ID 1092
  12.  
  13. // Detecting if customer has already bought The targeted product (1092)
  14. if( is_user_logged_in() ){
  15. $customer = wp_get_current_user();
  16. $customer_id = $customer->ID; // customer ID
  17. $customer_email = $customer->email; // customer email
  18.  
  19. if( wc_customer_bought_product( $customer_email, $customer_id, $targeted_product_id) )
  20. $custom_price = 0; // Set to 0 for other purchases (product ID 1092)
  21. }
  22.  
  23. foreach ( $cart_object->get_cart() as $cart_item ) {
  24. // When targeted product is in cart we change the price
  25. if ( $cart_item['product_id'] == $targeted_product_id ) {
  26. // Woocommerce 3+ compatibility
  27. if ( version_compare( WC_VERSION, '3.0', '<' ) )
  28. $cart_item['data']->price = $custom_price;
  29. else
  30. $cart_item['data']->set_price( $custom_price );
  31. }
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement