问题描述
我正在尝试提出一种解决方案,以限制可以运送产品类别的声明.到目前为止,这是我想出的.
I am trying to come up with a solution to limit the stated that a product category can be shipped to. Here is what I have come up with so far.
function get_prod_cat () {
global $woocommerce;
$specialfeecat = 34; // category id for the special fee
$items = $woocommerce->cart->get_cart();
foreach ($items as $item ) {
$product = $item['data'];
$terms = get_the_terms( $product->id, 'product_cat' );
if ( $terms && ! is_wp_error( $terms ) ) :
foreach ( $terms as $term ) {
$catid = $term->term_id;
if($specialfeecat == $catid ) {
$GLOBALS['cat_id'] = $catid;
}
return $cat_id;
}
endif;
}
}
if ($cat_id == 34) {
add_filter( 'woocommerce_states', 'wc_sell_only_states' );
}
function wc_sell_only_states() {
$states['US'] = array(
'AK' => __( 'Arkansas', 'woocommerce' ),
'DC' => __( 'Washington DC', 'woocommerce' ),
'IL' => __( 'Illinois', 'woocommerce' ),
'KY' => __( 'Kentucky', 'woocommerce' ),
'MN' => __( 'Minnesota', 'woocommerce' ),
'NM' => __( 'New Mexico', 'woocommerce' ),
);
return $states;
}
我试图从第一个函数中获取变量$ cat_id,以便可以将其用于限制状态的条件.预先感谢.
I am trying to get the variable $cat_id from the first function out of it so that I can use it for the condition to limit states. Thanks in advance.
推荐答案
像完成操作一样,调用 add_filter('woocommerce_states','wc_sell_only_states');
.内部函数'wc_sell_only_states($ states)'应用您在'get_prod_cat'内部所做的逻辑.如果类别匹配,则更改状态
Call add_filter( 'woocommerce_states', 'wc_sell_only_states' );
as you have done. Inside function 'wc_sell_only_states($states)' apply your logic, which you were doing inside 'get_prod_cat'. If category matched then change states
$states['US'] = array(
'AK' => __( 'Arkansas', 'woocommerce' ),
'DC' => __( 'Washington DC', 'woocommerce' ),
'IL' => __( 'Illinois', 'woocommerce' ),
'KY' => __( 'Kentucky', 'woocommerce' ),
'MN' => __( 'Minnesota', 'woocommerce' ),
'NM' => __( 'New Mexico', 'woocommerce' ),
);
其他返回状态.
这篇关于WooCommerce将运输限制为基于类别的州的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!