Skip to content

Instantly share code, notes, and snippets.

@cubehrends
Last active December 25, 2023 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cubehrends/afe659ad39d682c9ae62b8d1742cdf28 to your computer and use it in GitHub Desktop.
Save cubehrends/afe659ad39d682c9ae62b8d1742cdf28 to your computer and use it in GitHub Desktop.
WooCommerce - Restrict Shipping Zone by Shipping Class
<?php
// Adding notice to product availability text
function wdt_extend_availability( $availability, $product ) {
$target_shipping_class = 'dropshipping';
if ( $target_shipping_class == $product->get_shipping_class() ) {
$availability .= '<p class="shipping-class-notice">Diesen Artikel versenden wir <b>nur innerhalb Deutschlands</b>.</p>';
}
return $availability;
}
add_filter('woocommerce_get_availability_text', 'wdt_extend_availability', 10, 2);
// Restricitng Shipping Class & Shipping Zone combination in Cart
function wdt_restrict_shipping( $available_methods, $package ) {
// Set the shipping class and shipping zone you want to restrict
$restricted_shipping_class = 'dropshipping';
$restrict_to_zone = 1;
// Shipping zone
$shipping_zone = wc_get_shipping_zone( $package );
// Get zone ID
$zone_id = $shipping_zone->get_id();
if ( $zone_id != $restrict_to_zone ) {
foreach( $package['contents'] as $cart_item ) {
if ( $cart_item['data']->get_shipping_class() == $restricted_shipping_class ) {
wc_clear_notices();
wc_add_notice( __('Den Artikel <b>'.$cart_item['data']->get_title().'</b> versenden wir <b>nur innerhalb Deutschlands</b>.'), 'error' );
unset( $available_methods );
break;
}
}
}
return $available_methods;
}
add_filter('woocommerce_package_rates', 'wdt_restrict_shipping', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment