php - Products from a particular category does not affect the free shipping -
i need filter products particular product category , these products (amount) not affect free shipping min amount.
so thought increase min amount special product amount.
example: free shipping 40 euro. customer buys products 35 euro , 6 euro product spacial category. total = 41 euro
normaly gets free shipping. not special product in card.
is posible?
add_filter( 'woocommerce_package_rates', 'cardno_freeshipping', 10, 2 ); function cardno_freeshipping($rates, $package){ global $woocommerce; $items = $woocommerce->cart->get_cart(); // cart items foreach($items $item => $values) { $terms = get_the_terms( $values['product_id'], 'product_cat' ); foreach ($terms $term) { if($term->slug == "the-special-category"){ //if special category if($values['variation_id']) $price = get_post_meta($values['variation_id'] , '_price', true); //if variation exist price else $price = get_post_meta($values['product_id'] , '_price', true); //else singel price $pricearray[] = $price * $values['quantity']; } } } //get vars $free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' ); $cartsubtotal = $woocommerce->cart->subtotal; if($pricearray) { $sum = array_sum($pricearray); //the reason it's not 0 lies in definition of floating point numbers. $minamount = intval($cartsubtotal*100) - intval($sum*100); $minamount = $minamount/100; //the reason it's not 0 lies in definition of floating point numbers. } if(!$minamount){ //no special products in cart // modify rates if free_shipping present if ( isset( $rates['free_shipping'] ) ) { // unset single rate/method, following. example unsets flat_rate shipping unset( $rates['flat_rate'] ); // unset methods except free_shipping, following $free_shipping = $rates['free_shipping']; $rates = array(); $rates['free_shipping'] = $free_shipping; } } if($minamount==0){ //only special products in cart // modify rates if flat_rate present if ( isset( $rates['flat_rate'] ) ) { unset( $rates['free_shipping'] ); // unset methods except flat rate, following $flat_rate = $rates['flat_rate']; $rates = array(); $rates['flat_rate'] = $flat_rate; } }else{ //normal , special products in cart if($free_shipping_settings['min_amount'] >= $minamount){ if ( isset( $rates['flat_rate'] ) ) { unset( $rates['free_shipping'] ); // unset methods except flat rate, following $flat_rate = $rates['flat_rate']; $rates = array(); $rates['flat_rate'] = $flat_rate; } }else{ if ( isset( $rates['free_shipping'] ) ) { // unset single rate/method, following. example unsets flat_rate shipping unset( $rates['flat_rate'] ); // unset methods except free_shipping, following $free_shipping = $rates['free_shipping']; $rates = array(); $rates['free_shipping'] = $free_shipping; } else if( isset( $rates['flat_rate'] ) ) { unset( $rates['free_shipping'] ); // unset methods except flat rate, following $flat_rate = $rates['flat_rate']; $rates = array(); $rates['flat_rate'] = $flat_rate; } } } return $rates; }
maybe have better idea? ;)
maybe need again...
here right code:
add_filter( 'woocommerce_package_rates', 'cardno_freeshipping', 10, 2 ); function cardno_freeshipping($rates, $package){ global $woocommerce; $items = $woocommerce->cart->get_cart(); // cart items foreach($items $item => $values) { $terms = get_the_terms( $values['product_id'], 'product_cat' ); foreach ($terms $term) { if($term->slug == "xartotainies"){ //if special category if($values['variation_id']) $price = get_post_meta($values['variation_id'] , '_price', true); //if variation exist price else $price = get_post_meta($values['product_id'] , '_price', true); //else singel price $pricearray[] = $price * $values['quantity']; } } } //get vars $free_shipping_settings = get_option( 'woocommerce_free_shipping_settings' ); $cartsubtotal = $woocommerce->cart->subtotal; if($pricearray) { $sum = array_sum($pricearray); //the reason it's not 0 lies in definition of floating point numbers. $minamount = intval($cartsubtotal*100) - intval($sum*100); $minamount = $minamount/100; //the reason it's not 0 lies in definition of floating point numbers. if($minamount==0)$minamount=0.1; //fix 0 problem } if(!$minamount){ //no special products in cart // modify rates if free_shipping present if ( isset( $rates['free_shipping'] ) ) { // unset single rate/method, following. example unsets flat_rate shipping unset( $rates['flat_rate'] ); // unset methods except free_shipping, following $free_shipping = $rates['free_shipping']; $rates = array(); $rates['free_shipping'] = $free_shipping; } }else{ if($minamount >= $free_shipping_settings['min_amount']){ if ( isset( $rates['free_shipping'] ) ) { // unset single rate/method, following. example unsets flat_rate shipping unset( $rates['flat_rate'] ); // unset methods except free_shipping, following $free_shipping = $rates['free_shipping']; $rates = array(); $rates['free_shipping'] = $free_shipping; } }else{ if ( isset( $rates['flat_rate'] ) ) { unset( $rates['free_shipping'] ); // unset methods except flat rate, following $flat_rate = $rates['flat_rate']; $rates = array(); $rates['flat_rate'] = $flat_rate; } } } return $rates; }
Comments
Post a Comment