admin管理员组文章数量:1433154
I have a question regarding shipping restrictions in WooCommerce. My client is selling a product with 4 variants.
Out of the 4 variants, only 1 can be shipped internationally and the other 3 can't. The expected outcome is that if the customer selects the variant that is able to ship internationally then ok if any of the other 3 are selected/in the cart then not.
Is there a way I can set this up? So far I have tried the conditional shipping module for WooCommerce but could not get it working.
Any help would be appreciated.
I have a question regarding shipping restrictions in WooCommerce. My client is selling a product with 4 variants.
Out of the 4 variants, only 1 can be shipped internationally and the other 3 can't. The expected outcome is that if the customer selects the variant that is able to ship internationally then ok if any of the other 3 are selected/in the cart then not.
Is there a way I can set this up? So far I have tried the conditional shipping module for WooCommerce but could not get it working.
Any help would be appreciated.
Share Improve this question asked May 16, 2019 at 22:52 sebseb 1234 bronze badges1 Answer
Reset to default 2This is something complicated… What you could do instead is to check cart items for allowed shipping countries, displaying an error notice and avoiding checkout when the customer country doesn't match with the allowed country for all other product variations.
Below define the allowed shippable Variation ID (international) and the allowed base country code for your other product variations:
add_action( 'woocommerce_check_cart_items', 'check_cart_items_for_shipping' );
function check_cart_items_for_shipping() {
$allowed_variation_id = '513'; // Here defined the allowed Variation ID
$allowed_country = 'US'; // 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
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
本文标签: Shipping restrictions with WooCommerce variable products
版权声明:本文标题:Shipping restrictions with WooCommerce variable products 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745485439a2660360.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论