Last active
September 30, 2022 13:34
-
-
Save damiencarbery/bb180349f97d3a54e75e3f665fa5177c to your computer and use it in GitHub Desktop.
Hide shipping method by product id or category - Exclude a shipping method if the cart contains certain products or product categories. https://www.damiencarbery.com/2022/10/hide-shipping-method-by-product-id-or-category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Hide shipping method by product id or category | |
Plugin URI: https://www.damiencarbery.com/2022/10/hide-shipping-method-by-product-id-or-category | |
Description: Exclude a shipping method if the cart contains certain products or product categories. | |
Author: Damien Carbery | |
Version: 0.1 | |
*/ | |
// Filter shipping methods in the checkout - do not show An Post if a Craft item is in the cart. | |
add_filter( 'woocommerce_package_rates', 'dcwd_filter_shipping_methods', 10, 2 ); | |
function dcwd_filter_shipping_methods( $rates, $package ) { | |
$method_name = 'An Post'; // This can be a partial or full name. | |
// If the product is one of the following then exclude the shipping method. | |
$products_not_excluded_method = array( | |
13083, // https://craftyletters.ie/product/smoothing-acrylic-round-discs-varied-sizes/ | |
); | |
// Go through the cart again looking for category matches. | |
$categories_not_excluded_method = array( | |
'crafts', // Crafts | |
'personalised-gifts', // Personalised Gifts | |
'hoops-inserts', // Hooks & Inserts | |
); | |
// No configuration needed beyond this point. | |
$shipping_method_key = false; | |
// Find shipping method ID. | |
foreach ( $rates as $rate_key => $rate ) { | |
//error_log( 'Rate label: ' . $rate->get_label() ); | |
if ( is_object( $rate ) && method_exists( $rate, 'get_label' ) && false !== strpos( $rate->get_label(), $method_name ) ) { | |
$shipping_method_key = $rate_key; | |
} | |
} | |
// Go through all products and check the product ID and category | |
if ( $shipping_method_key !== false ) { | |
$exclude_shipping_option = false; | |
foreach ( $package['contents'] as $key => $item ) { | |
if ( in_array( $item['product_id'], $products_not_excluded_method ) ) { | |
$exclude_shipping_option = true; | |
break; | |
} | |
} | |
foreach ( $package['contents'] as $key => $item ) { | |
$categories = get_the_terms( $item['product_id'], 'product_cat' ); | |
if ( $categories && ! is_wp_error( $categories ) && is_array( $categories ) ) { | |
foreach ( $categories as $category ) { | |
//error_log( 'Category: ' . $category->slug ); | |
if ( in_array( $category->slug, $categories_not_excluded_method ) ) { | |
$exclude_shipping_option = true; | |
break; | |
} | |
} | |
} | |
} | |
// Craft item has been found, disable the shipping method. | |
if ( $exclude_shipping_option === true ) { | |
unset( $rates[$shipping_method_key] ); | |
} | |
} | |
return $rates; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment