当前位置: 首页 > 知识库问答 >
问题:

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

宓跃
2023-03-14

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

    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个答案

终逸仙
2023-03-14
function m_varient_price( $price, $variation ) {
if (  $variation->product_type == 'variation'  ) {
$user = $user ? new WP_User( $user ) : wp_get_current_user();
$role = $user->roles[0];
if($role == 'distributor'){$pricex = get_post_meta( $variation->variation_id, '_dist_field',true);}
else if($role == 'reseller'){$price = get_post_meta( $variation->variation_id, '_rese_field',true);}  
else{ $pricex = $price;}
}
return $pricex;
}

add_filter( 'woocommerce_product_variation_get_regular_price', 'm_varient_price' , 99, 2 );  
add_filter( 'woocommerce_product_variation_get_sale_price', 'm_varient_price' , 99, 2 );
add_filter( 'woocommerce_product_variation_get_price', 'm_varient_price', 99, 2 );
苗征
2023-03-14

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

添加_过滤器('woocommerce_get_price','change_price',10,2);

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

}

华化
2023-03-14

试试这个钩子-

add_filter('woocommerce_variable_sale_price_html', 'my_html' , 10, 2);
add_filter('woocommerce_variable_price_html',  'my_html' , 10, 2);
 类似资料:
  • 我正在重写AJAX函数,以便使用WooCommerce添加产品,这样我也可以将属性发送到购物车(这不是开箱即用的)。 在通过AJAX添加变量产品后,我按照更新WooCommerce购物车的说明进行了操作——这正是我想要的——但它并不完全有效。 当我注销时,一切都很好。但是,当我登录Wordpress时,PHP覆盖不起作用/被忽略。我没有收到任何错误,也没有向购物车添加任何产品。我尝试了许多不同的方

  • 问题内容: 我已经组装好了将链接添加到购物车中的可变产品的链接,就像这样,但是我对如何“刷新”购物车而不重新加载页面感到困惑。我的猜测是,我没有正确地向购物车请求中添加此AJAX,因此未调用woocommerce_add_to_cart_fragments(我相信我的购物车HTML会在此处放置刷新)。 如果有人能指出我正确的方向,我将不胜感激。Variable / AJAX / WooCommer

  • 我已经组装了一个链接,像这样将一个可变产品添加到我的购物车,但是我不知道如何在不重新加载页面的情况下“刷新”购物车。我的猜测是,我没有正确地将此AJAX添加到购物车请求,因此,woocommerce_add_to_cart_fragments(我相信我的购物车超文本标记语言将被放置以供刷新)没有被调用。 如果有人能给我指出正确的方向,我将不胜感激。Variable/AJAX/WooCommerce

  • 我使用WooCommerce Wordpress中的以下代码,通过一个模板,使用add_to_cart($product_id)功能添加了许多产品。 现在,我希望通过此模板为每个产品添加自定义价格。现在购物车中的价格和数据库中的价格一样,但我想通过这一点添加我的自定义价格。有人能帮我做同样的事吗。谢谢

  • 我使用“从Woocommerce中的functions.php文件中添加到购物车按钮上显示价格”的答案代码,在简单的产品中添加价格。 但我想改进这个功能,动态显示选择的变异价格内按钮。在变体产品页面上使用此代码,它只显示最低的价格。有什么解决办法吗?

  • 目标:在一个按钮点击中添加多个可变产品到购物车。 我试着将这段代码放入循环中,但它不起作用。 但是这种方法不能解决问题,因为我需要在一个按钮点击中添加几个产品变体。 所有的建议都将不胜感激。多谢了。

  • 我经营一家Woocommerce商店,该商店也提供免费产品(_常规价格=0)。客户必须选择数量并将其添加到购物车中,然后下订单才能收到产品。但这并不是Woocommerce的工作原理,它隐藏了所有价格为0的产品的“添加到购物车”链接。并且不会在购物车页面中显示它们。有没有解决这个问题的办法?非常感谢。