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

时间:2015-05-21 13:17:42

标签: php wordpress woocommerce

我创建了自定义元字段,根据简单产品和变量类型产品的用户角色添加自定义价格。

    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;
    }

}

我使用上面的代码根据简单类型产品的用户角色来改变前端的价格,并且它可以正常工作。它显示更改价格,当我点击添加到购物车按钮时,它在购物车中添加更改价格。

如果是变量产品,我使用下面的代码

   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));
    }
}

}

从这个变化价格根据用户角色改变但问题是,当我点击添加到购物车按钮时,它在购物车中为所有可变产品添加0.00价格

所以,如果您有任何想法,请解决此问题。

谢谢&问候 苏雷什库马尔

3 个答案:

答案 0 :(得分:0)

尝试使用此挂钩 -

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

答案 1 :(得分:0)

感谢您的支持,我使用了以下代码,并解决了我的问题。

add_filter(' woocommerce_get_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') {
            $custome_price = get_post_meta( $productd->id, $category.'_price',true);
            return $custome_price;
    }
    else 
    {
        return $price;
    }
}
 if (  $productd->product_type == 'variation' || $productd->product_type == 'variable' ) {

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

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

}

答案 2 :(得分:0)

class Program
{
    static void Main(string[] args)
    {
        var basePath = GetDotNetCoreBasePath();
        Console.WriteLine();
        Console.ReadLine();
    }
    static String GetDotNetCoreBasePath()
    {
        Process process = new Process
        {
            StartInfo =
            {
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                CreateNoWindow = true,
                FileName = "dotnet",
                Arguments = "--info"
            }
        };
        process.Start();
        process.WaitForExit();
        if (process.HasExited)
        {
            string output = process.StandardOutput.ReadToEnd();
            if (String.IsNullOrEmpty(output) == false)
            {
                var reg = new Regex("Base Path:(.+)");
                var matches = reg.Match(output);
                if (matches.Groups.Count >= 2)
                    return matches.Groups[1].Value.Trim();
            }
        }
        throw new Exception("DotNet Core Base Path not found.");
    }
}