duansengcha9114 2017-03-24 12:26
浏览 66

如果语句里面的开关(PHP)

I'm working with WooCommerce and I am trying to develop a way to perform price calculation based in two factors:

  • location of the customer by city
  • category

I was able to change price based on the location of the customer but if I try to filter by location and category at the same time it does not work. To perform the filter by category I add this if statement:

if( has_term( array('nike'), 'product_cat' ,$product->ID) )

But I don't understand why this does not work.

Here is the code:

$userInfo = geoip_detect2_get_info_from_current_ip();
$countryCode = $userInfo->city->name;

switch ($countryCode) {
    case 'Madrid':
        if (has_term(array('nike') , 'product_cat', $product->ID)) {
            add_filter('woocommerce_get_price', 'product_custom_price', 10, 2);
            function product_custom_price($price, $product) {
                $custom_price = $product->get_regular_price();
                return $custom_price * 1.5;
            }
        }

        break;

    case 'Barcelona':
        if (has_term(array('adidas') , 'product_cat', $product->ID)) {
            add_filter('woocommerce_get_price', 'product_custom_price', 10, 2);
            function product_custom_price($price, $product) {
                $custom_price = $product->get_regular_price();
                return $custom_price * 1.25;
            }
        }

        break;
}
  • 写回答

2条回答 默认 最新

  • dongraobei6719 2017-03-24 13:04
    关注

    You are using has_term in wrong way and you can use foreach instead

    Try this:

     $userInfo = geoip_detect2_get_info_from_current_ip();
        $countryCode = $userInfo->city->name;
        $terms = get_the_terms($product->ID, 'product_cat');
                    switch ($countryCode) {
                    case 'Madrid':
              foreach ( $terms as $term ) {
                if($term->name == 'nike') {
                       add_filter('woocommerce_get_price', 'product_custom_price', 10, 2);
                       function product_custom_price($price, $product) {
                     $custom_price = $product->get_regular_price();
                     return $custom_price * 1.5;}
                                    }
                        }
    
                    break;
                    case 'Barcelona':
    
               foreach ( $terms as $term ) {
                   if($term->name == 'adidas') {
                       add_filter('woocommerce_get_price', 'product_custom_price', 10, 2);
                        function product_custom_price($price, $product) {
                         $custom_price = $product->get_regular_price();
                          return $custom_price * 1.25;}
                                        }
                        }        
    
                    break;
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计