1
votes

I am trying to work out if it is possible to prepend a brand name to any shipping options available. I will be setting up a store whereby you can only order one brand at a time, I will have cart limitations built in to prevent mixing two brands together.

I have an external ordering system called Veeqo and without going into too much detail I need to prepend the brand name of the products in the cart to any shipping method selected so that I can filter orders by these shipping options. E.g.

BRAND-NAME UK Next Day BRAND-NAME UK 3-5 Days

Can this be done ? If so, how ?

Realise I am asking a lot here but if anyone knows of a way to do this that would be much appreciated! :)

Perhaps a function which searches the first line item for "Brand" and then prepends this value to all shipping method titles. WooCommerce would only display relevant shopping methods to them depending on country etc.

1

1 Answers

1
votes

Using a custom function hooked in woocommerce_package_rates filter hook, you will be able to prepend the cart item brand name to the shipping method label name.

As there are multiple ways to enable brands in WooCommerce, you will need to define the taxonomy used by the brand plugin that you have enabled in WooCommerce…
For the taxonomy to use, see: How to get the brand name of product in WooCommerce

add_filter( 'woocommerce_package_rates', 'prepend_brand_to_shipping_methods', 10, 2 );
function prepend_brand_to_shipping_methods( $rates, $package ){
    // HERE define the taxonomy for product brand (depend of used plugin)
    $taxonomy ='product_brand';

    // Get the first cart item
    $cart_item = reset($package['contents']);

    // Get the product brand term name
    $brand_name = wp_get_post_terms( $cart_item['product_id'], $taxonomy, ['fields' =>'names']);
    $brand_name = reset($brand_name);

    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ){
        // Changing shipping method label name
        $rates[$rate_key]->label = $brand_name . ' ' . $rate->label;
    }
    return $rates;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Refresh the shipping caches:
1). This code is already saved on your function.php file.
2). In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.