And AI helped with Woocommerce Australia Post Plugin Help
-
Hi, I need some help with a request from a client who is using the Australia Post Shipping plugin from Woocommerce. Basically what he has requested is: He wants us to setup the checkout so that if a customer purchases over $150 of products from a certain product category, they will get those shipped free. But if it is under $150 the Standard Aust Post rate applies. However, in all cases whether it be under $150 or Free he wants Express Post to be viewable and selectiable just in case the customer wants the products sent Express Post. His other request is that this offer only applies to customers who are from Australia and want it delivered in Australia. So does anyone have any idea how to do this. I asked Chat GTP to help and he gave me this to add to the function.php file of the theme:
add_filter('woocommerce_package_rates', 'customize_shipping_options', 10, 2); function customize_shipping_options($rates, $package) { $free_shipping_class = 'free-shipping-eligible'; // Replace with your shipping class slug $cart_total = WC()->cart->cart_contents_total; $customer_country = WC()->customer->get_shipping_country(); $express_post_found = false; $regular_post_id = 'shipping_method_0_australia_postaus_parcel_regular'; $express_post_id = 'shipping_method_0_australia_postaus_parcel_express'; // Check if "Express Post" is available and note its presence foreach ($rates as $rate) { if ($rate->id === $express_post_id) { $express_post_found = true; break; } } // Check if cart total is over 150AUD and contains eligible products for free shipping $eligible_for_free_shipping = $cart_total >= 150 && has_free_shipping_eligible_products(); // If eligible for free shipping, unset "Regular Post" if ($eligible_for_free_shipping) { foreach ($rates as $rate_id => $rate) { if ($rate->id === $regular_post_id) { unset($rates[$rate_id]); break; // We only need to remove "Regular Post" once } } } // If "Express Post" is not available, add it to the rates if (!$express_post_found) { $rates[$express_post_id] = array( 'id' => $express_post_id, 'label' => 'Express Post', 'cost' => 0, // The cost for "Express Post" can't be modified, set it to 0 'method_id' => 'australia_post', // Use 'australia_post' as the method_id, this is the common ID for Australia Post methods ); } return $rates; } // Helper function to check if cart contains eligible products for free shipping function has_free_shipping_eligible_products() { foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) { if (get_post_meta($cart_item['product_id'], '_shipping_class', true) === 'free-shipping-eligible') { return true; } } return false; }
Can anyone assist with this?
The page I need help with: [log in to see the link]
- The topic ‘And AI helped with Woocommerce Australia Post Plugin Help’ is closed to new replies.