// Function to modify shipping methods based on cart total and weight
function custom_modify_shipping_methods( $rates, $package ) {
// write_log('function custom_modify_shipping_methods( $rates, $package ) {');
// Get cart total
$cart_total = WC()->cart->subtotal;
// Get cart weight
$cart_weight = WC()->cart->get_cart_contents_weight();
// Check if cart total is above $800
if ( $cart_total > 800 ) {
// Loop through shipping rates
foreach ( $rates as $rate_id => $rate ) {
// Check if it's the fixed price shipping method
if ( 'flat_rate:12' === $rate_id ) {
// Set the cost to 0 for free shipping
$rates[$rate_id]->cost = 0;
// Change label text to "Free Shipping"
$rates[$rate_id]->label = __( 'משלוח חינם', 'woocommerce' );
}
}
}
// Check if cart weight is over 20kg
if ( $cart_weight > 20 ) {
// Loop through shipping rates
foreach ( $rates as $rate_id => $rate ) {
// Check if it's the fixed price shipping method
if ( 'flat_rate:12' === $rate_id ) {
// Set the cost to $50
$rates[$rate_id]->cost = 50;
// Change label text to
$rates[$rate_id]->label = __( 'עלות משלוח', 'woocommerce' );
}
}
}
return $rates;
}
add_filter( 'woocommerce_package_rates', 'custom_modify_shipping_methods', 10, 2 );