• Resolved Steve Szasz

    (@wpclinic)


    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]

Viewing 1 replies (of 1 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @wpclinic

    The code you provided is a great start. It checks if the cart total is over $150 and if the products are eligible for free shipping. If these conditions are met, it removes the “Regular Post” shipping method and ensures that the “Express Post” method is always available. However, it does not take into account the product category, and it does not restrict the offer to customers who are from Australia and want it delivered in Australia.

    To implement this, you’ll need to modify the has_free_shipping_eligible_products function to check for the specific product category. You can use the has_term function to check if a product belongs to a certain category.

    To restrict the offer to Australian customers, you can add a condition in the customize_shipping_options function to check the customer’s shipping country:

    if ($customer_country !== 'AU') {
    return $rates;
    }

    Add this at the beginning of the function, before the loop that checks for “Express Post”.

    Please note that custom coding is not something we can assist with directly. Still, if you have any further questions on development or custom coding, don’t hesitate to reach out to some of our great resources available for support. Our WooCommerce community is brimming with skilled open-source developers who are active on the following channels:

    Additionally, if you hold a valid license for the Australia Post Shipping Method extension with us, feel free to open a support request. Our Happiness Engineers will be there to provide any further help you need regarding the Australia Post Shipping Method extension.

    I hope this helps! If you have any other questions or need further clarification, feel free to ask.

    Thanks!

Viewing 1 replies (of 1 total)
  • The topic ‘And AI helped with Woocommerce Australia Post Plugin Help’ is closed to new replies.