dongwen3410 2015-05-21 13:17
浏览 163

在woocommerce中添加购物车时更改可变产品价格

I have created custom meta fields to add custom price according to user roles for both simple product and variable type product.

    add_filter('woocommerce_get_price','change_price', 10, 2);
    add_filter('woocommerce_get_regular_price','change_price', 10, 2);
    add_filter('woocommerce_get_sale_price','change_price', 10, 2);


    function change_price($price, $productd){

        get_currentuserinfo;
        global $current_user;
        $category = $current_user->roles[0];

  if($productd->product_type == 'simple')
    {

        if($category == 'gastronomy' || $category == 'corporate' ||     $category == 'retail' || $category == 'distributor') {
        $price = get_post_meta( $productd->id, $category.'_price',true);
      }
       return $price;
    }

}

I use above code to change price in front end according to user roles for simple type products and it works correctly . its show change price and also when I click on add to cart button its add changed price in cart.

In case of variable product I use below code

   add_filter( 'woocommerce_variation_sale_price_html', 'my_html', 10, 2);
  add_filter( 'woocommerce_variation_price_html', 'my_html', 10, 2);
  add_filter( 'woocommerce_get_variation_price_html', 'my_html', 10, 2);
   function my_html( $price, $variation ) {

        get_currentuserinfo;
        global $current_user;
         $category = $current_user->roles[0];

  if (  $variation->product_type == 'variation'  ) {

    if($category == 'gastronomy' || $category == 'corporate' || $category == 'retail' || $category == 'distributor') {
        $price = get_post_meta( $variation->variation_id, $category.'_price',true);
        return woocommerce_price($price);
       }

       else
       {
            return woocommerce_price(get_post_meta( $variation->variation_id, '_regular_price',true));
    }
}

}

from this variation price changed according to user roles but the problem is that when I clicked on add to cart button its add 0.00 price in cart for all variable product

so please if u have any idea then solve this.

Thanks & Regards Suresh Kumar

  • 写回答

3条回答 默认 最新

  • doushuo1989 2015-05-22 05:10
    关注

    Try with this hooks-

    add_filter('woocommerce_variable_sale_price_html', 'my_html' , 10, 2);
    add_filter('woocommerce_variable_price_html',  'my_html' , 10, 2);
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度