【问题标题】:WooCommerce restrict shipping to states based on categoryWooCommerce 根据类别限制运送到州
【发布时间】: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


    【解决方案1】:

    按照您的方式致电add_filter( 'woocommerce_states', 'wc_sell_only_states' );。在函数“wc_sell_only_states($states)”内部应用您在“get_prod_cat”内部执行的逻辑。如果类别匹配,则更改状态

    $states['US'] = array(
            'AK' => __( 'Arkansas', 'woocommerce' ),
            'DC' => __( 'Washington DC', 'woocommerce' ),
            'IL' => __( 'Illinois', 'woocommerce' ),
            'KY' => __( 'Kentucky', 'woocommerce' ),
            'MN' => __( 'Minnesota', 'woocommerce' ),
            'NM' => __( 'New Mexico', 'woocommerce' ),
            );
    

    否则返回原样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 2014-12-01
      • 2017-09-20
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 2021-10-06
      相关资源
      最近更新 更多