Last active
December 25, 2023 17:32
-
-
Save cubehrends/afe659ad39d682c9ae62b8d1742cdf28 to your computer and use it in GitHub Desktop.
WooCommerce - Restrict Shipping Zone by Shipping Class
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 | |
// 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