如何添加允许的国家/地区列表,并定位标题中仅包含特定单词的产品,而不是变体 ID。例如,“Framed A3 Poster”产品标题中的“framed”一词。
这意味着我将避免必须针对每个单独的变体 ID,因为我实际上有 +280 个产品。
下面的代码是我正在使用的:
add_action( 'woocommerce_check_cart_items', 'check_cart_items_for_shipping' );
function check_cart_items_for_shipping() {
$allowed_variation_id = '13021'; // Here defined the allowed Variation ID
$allowed_country = 'IE'; // Here define the allowed shipping country for all variations
$shipping_country = WC()->customer->get_shipping_country();
$countries = WC()->countries->get_countries();
// Loop through cart items
foreach(WC()->cart->get_cart() as $cart_item ) {
// Check cart item for defined product Ids and applied coupon
if( $shipping_country !== $allowed_country && $cart_item['variation_id'] !== $allowed_variation_id ) {
wc_clear_notices(); // Clear all other notices
// Avoid checkout displaying an error notice
wc_add_notice( sprintf( __('The product "%s" can not be shipped to %s.'),
$cart_item['data']->get_name(),
$countries[$shipping_country]
), 'error' );
break; // stop the loop
}
}
}