【发布时间】:2017-03-21 14:54:37
【问题描述】:
我正在尝试提出一种解决方案来限制可以运送产品类别的声明。到目前为止,这是我想出的。
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,以便我可以将它用于限制状态的条件。提前致谢。
【问题讨论】:
标签: php wordpress function woocommerce