Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_cart_items_price', 10, 1 );
- function conditionally_change_cart_items_price( $cart_object ) {
- if ( is_admin() && ! defined( 'DOING_AJAX' ) )
- return;
- $targeted_product_id = 1107;
- // Set Here your custom price (1st purshase)
- $custom_price = 100; // First purshase for product ID 1092
- // Detecting if customer has already bought The targeted product (1092)
- if( is_user_logged_in() ){
- $customer = wp_get_current_user();
- $customer_id = $customer->ID; // customer ID
- $customer_email = $customer->email; // customer email
- if( wc_customer_bought_product( $customer_email, $customer_id, $targeted_product_id) )
- $custom_price = 0; // Set to 0 for other purchases (product ID 1092)
- }
- foreach ( $cart_object->get_cart() as $cart_item ) {
- // When targeted product is in cart we change the price
- if ( $cart_item['product_id'] == $targeted_product_id ) {
- // Woocommerce 3+ compatibility
- if ( version_compare( WC_VERSION, '3.0', '<' ) )
- $cart_item['data']->price = $custom_price;
- else
- $cart_item['data']->set_price( $custom_price );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement