我有一些产品无法提供提货服务,我希望在添加产品时有一个启用/禁用选项,以在我的 WooCommerce 商店中禁用这些产品的本地提货选项。
我有一个代码可以启用特定类别的本地取货,但我想要一个启用禁用选项。
// Don't allow local pickup for specific category
add_action( 'woocommerce_package_rates','show_hide_local_pickup_shipping_methods', 10, 2 );
function show_hide_local_pickup_shipping_methods( $rates, $package ) {
// HERE BELOW your product categories in the array
$categories = array( 'Olive Oil' );
$term_found = false;
// Loop through cart items
foreach( $package['contents'] as $cart_item ){
if( has_term( $categories, 'product_cat', $cart_item['product_id'] ) ) {
$term_found = true;
break;
}
}
// Loop through shipping methods
foreach( $rates as $rate_key => $rate ) {
if( 'local_pickup' === $rate->method_id && ! $term_found ) {
unset($rates[$rate_key]);
}
}
return $rates;
}
有人可以帮我写代码吗?
要使“本地取货”运输方式仅适用于特定产品,请使用以下内容:
// Allow local pickup only for specific product Ids
add_action( 'woocommerce_package_rates', 'show_hide_local_pickup_shipping_methods', 10, 2 );
function show_hide_local_pickup_shipping_methods( $rates, $package ) {
// HERE BELOW the product Ids in the array allowing local pickup
$product_ids = array(12, 34, 56);
$product_found = false;
// Loop through cart items
foreach( $package['contents'] as $item ){
if( count( array_intersect($product_ids, [$item['variation_id'], $item['product_id']]) ) > 0 ) {
$product_found = true;
break;
}
}
// Loop through shipping methods
foreach( $rates as $rate_key => $rate ) {
if( 'local_pickup' === $rate->method_id && ! $product_found ) {
unset($rates[$rate_key]);
}
}
return $rates;
}
代码位于子主题的functions.php 文件中(或插件中)。应该可以。
您需要清空购物车来刷新缓存的运输方式费率。