Snippets Collections
/**
 * 
 * 
 * PRIORITIES : 
 * who comes first ? 
 * 6 + 1  
 * Promo >= 49
 * 
 *  
 */

// // Function to check if user has allowed role
function belair_user_has_allowed_role() {
    $allowed_roles = array( 'pro' );
    $user = wp_get_current_user();

    if ( array_intersect( $allowed_roles, $user->roles ) ) {
        return true;
    }

    return false;
}

function belair_calculate_cart_total_excluding_tax() {
    $cart_total = 0;

    // Get cart contents
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $product = $cart_item['data'];
        $price_excluding_tax = wc_get_price_excluding_tax( $product );

        // Calculate total excluding tax
        $cart_total += $price_excluding_tax * $cart_item['quantity'];
    }

    return $cart_total;
}
 

function belair_check_for_wholesale_prices_in_cart() {
	$cart = WC()->cart->get_cart();
	$found = false;

	foreach ( $cart as $cart_item_key => $cart_item ) {
		
// 		echo "<pre>";
// 		print_r($cart_item['wwp_data']);
// 		echo "</pre>";
		
		if ( isset( $cart_item['wwp_data'] ) && is_array( $cart_item['wwp_data'] ) ) {
			if ( $cart_item['wwp_data']['wholesale_priced'] === 'yes' ) {
				// The cart item contains the specified array
				$found = true;
				break; // Stop the loop since we found a matching item
			}
		}
	}
	
	return $found;
}


// Hook to apply or remove promo code based on total and user role
function belair_apply_or_remove_promo_code_based_on_total_and_user_role() { 
	// Check if user has allowed role
	if ( belair_user_has_allowed_role() ) {
		
		$cart_total = belair_calculate_cart_total_excluding_tax();
		$promo_code = '49ht30'; // use lowercase letters
		$coupon_applied = in_array(  $promo_code, WC()->cart->get_applied_coupons() );
		$tarifs_revendeurs_applied =  belair_check_for_wholesale_prices_in_cart();
		
// 		error_log(print_r([
// 			'cart_total' => $cart_total,
// 			'coupon_applied' => $coupon_applied,
// 			'tarifs_revendeurs_applied' => $tarifs_revendeurs_applied,
// 		]));
		
		// Prevent recursive recalculations
		remove_action('woocommerce_before_calculate_totals', 'belair_apply_or_remove_promo_code_based_on_total_and_user_role', 999);

	 
		// Adjust promo code based on cart total
		if ( $cart_total >= 49 && !$tarifs_revendeurs_applied )  {
			
// 			error_log("We are in coupon territory");
			
			if(!$coupon_applied){
				
			// Apply promo code
			WC()->cart->apply_coupon( $promo_code );
				$message = "Vous avez atteint 49€ HT de commande, votre réduction de 30% a été appliquée.";
				wc_add_notice($message, 'success');
			}
			
		} elseif ( $cart_total < 49 && $coupon_applied ) {
			// Remove promo code
			WC()->cart->remove_coupon( $promo_code );
			
// 			wc_add_notice("Below coupon territory", 'success');
			
			$message = "Votre panier n'atteint pas les 49€ HT pour bénéficier de -30%; veuillez ajouter des produits à votre commande.";
			wc_add_notice($message, 'success');
		} elseif ( $tarifs_revendeurs_applied ){
			
			 
// 			wc_add_notice("Above coupon territory", 'success');
			
			// Remove coupon as now Wholesaleprices apply and we don't want both at once
			WC()->cart->remove_coupon( $promo_code );
		
		}
		
		
	}

    // Reattach hook
    add_action('woocommerce_before_calculate_totals', 'belair_apply_or_remove_promo_code_based_on_total_and_user_role', 999);

}

// Hook into WooCommerce actions
add_action( 'woocommerce_before_calculate_totals', 'belair_apply_or_remove_promo_code_based_on_total_and_user_role', 999 );

// add_action( 'woocommerce_before_calculate_totals', 'belair_apply_or_remove_promo_code_based_on_total_and_user_role', 999 );




add_filter( 'woocommerce_package_rates', 'conditionally_show_shipping_method_based_on_coupon', 10, 2 );
function conditionally_show_shipping_method_based_on_coupon( $rates, $package ) {
    $required_coupon = '49ht30'; // Change this to your coupon code
    $targeted_shipping_method_id = 'service_point_shipping_method:52'; // Adjust this to match the method you want to restrict

    // Check if the required coupon is applied
    if ( ! in_array( strtolower( $required_coupon ), WC()->cart->get_applied_coupons() ) ) {
        foreach ( $rates as $rate_id => $rate ) {
            if ( $rate_id === $targeted_shipping_method_id ) {
                unset( $rates[ $rate_id ] );
            }
        }
    }

    return $rates;
}

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension