• Resolved oscarsk

    (@oscarsk)


    Hi
    I have this PHP-code:

    add_filter( 'woocommerce_package_rates', 'custom_shipping_settings', 10, 2 );   
    function custom_shipping_settings( $rates, $package ) {
       $shipping_class_target = 143; // shipping class ID (to find it, see screenshot below)
       $in_cart = false;
       foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
          if ( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
             $in_cart = true;
             break;
          } 
       }
       if ( $in_cart ) {
          unset( $rates['flatrate:1'] );
       }
       return $rates;
    }

    The above code checks if a particular shipping class is selected. Instead, I would like to check if a product in the cart has a price of 0.

    Is it possible?

    As a test, I have tried with:

    if ( $values[ 'data' ]->get_product_price() == 0 ) {
         echo '<script>alert("It works");</script>';
     } 

    … but it does not work either

    I really hope someone can help! ??

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘PHP if product price is 0 in cart’ is closed to new replies.