Free shipping and discount 9% for all categories except two

Question

We need to do:

A discount 9% for the price of the product excludes products of a specific one category, And Free shipping for all products if the total order is 90$.
how can do that, I try this code but not work and not what I need.

function fg_add_fee($the_cart) {
  global $woocommerce;
  $category_exclude = array( 679 ); // Product category exclude
  $taxonomy = 'product_cat'; // Taxonomy for product category 
  $cartprice = floatval( preg_replace( '#[^d.]#', '', $woocommerce->cart->get_cart_total() ) );

  // Loop through all products in the Cart        
  foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    $product = $cart_item['data'];
    $price_product = WC()->cart->get_product_price( $product );
    $discount = $price_product * 0.09;
    $shipping = $the_cart->shipping_total;
  
    if (($woocommerce->session->chosen_payment_method == 'hyperpay_mada' || $woocommerce->session->chosen_payment_method == 'hyperpay' || $woocommerce->session->chosen_payment_method == 'hyperpay_stcpay' || $woocommerce->session->chosen_payment_method == 'hyperpay_applepay' ) && ($cartprice >= 90) ) {
      $woocommerce->cart->add_fee('Free Shipping', -$shipping, true, 'standard');
      $woocommerce->cart->add_fee('Discount 9%', -$discount, true, 'standard');
    } 
    
    if (($woocommerce->session->chosen_payment_method == 'hyperpay_mada' || $woocommerce->session->chosen_payment_method == 'hyperpay' || $woocommerce->session->chosen_payment_method == 'hyperpay_stcpay' || $woocommerce->session->chosen_payment_method == 'hyperpay_applepay' ) && ($cartprice >= 90) && (! has_term( $category_exclude, $taxonomy, $cart_item['product_id'] ))) {
      $woocommerce->cart->add_fee('Discount 9%', -$discount, true, 'standard');
    }
  }
}
0
khalid almallahi 4 months 0 Answers 13 views 0

Leave an answer