WooCommerce 3 中的条件产品价格购物车问题 [英] Conditional product prices cart issue in WooCommerce 3

查看:51
本文介绍了WooCommerce 3 中的条件产品价格购物车问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改了一个函数来为我的一些会员创建自定义价格,即正常价格为 1 美元,但如果您是铜牌会员,则为 2 美元,银牌会员为 3 美元,等等.

I modified a function to create custom prices for some of my members i.e. the normal price is $1 but if you're a bronze member it's $2, a silver member $3, etc.

店铺和单品页面价格有变动.但是,将产品添加到购物车后,价格将恢复为原始金额.是否应该包含额外的代码,以便在结帐和结算的整个过程中准确更改价格?

The prices are changed on the shop and single product page. When the product is added to the cart, however, the price reverts to the original amount. Is there additional code I should be including to have the price accurately changed all the way through checkout and billing?

// Variations (of a variable product)
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 99, 3 );
function custom_variation_price( $price, $variation, $product ) {

global $product;
$id = $product->get_id();

$user_id = get_current_user_id();
$plan_id = 1628;

  if ( wc_memberships_is_user_member( $user_id, $plan_id )  ) {

  $new = $price * 2;  

  return ($new);
  }

}

推荐答案

使用您的代码,您只需更改显示的变化价格范围.因此,您将需要更多:

With your code you are just changing the displayed variation price range. So you will need a bit more for this:

// Simple, grouped and external products
add_filter('woocommerce_product_get_price', 'custom_price', 90, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 90, 2 );

// Product variations (of a variable product)
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 90, 2 );

// Variable product price ramge
add_filter('woocommerce_variation_prices_price', 'custom_variation_price', 90, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variation_price', 90, 3 );

function custom_price( $price, $product ) {
    // Only logged in users
    if ( ! is_user_logged_in() ) return $price; 

    // HERE the defined plan ID
    $plan_id = 1628;

    if ( wc_memberships_is_user_member( get_current_user_id(), $plan_id )  ) {
        $price *= 2; // set price x 2
    }
    return $price;
}

function custom_variation_price( $price, $variation, $product ) {
    // Only logged in users
    if ( ! is_user_logged_in() ) return $price; 

    // HERE the defined plan ID
    $plan_id = 1628;

    if ( wc_memberships_is_user_member( get_current_user_id(), $plan_id )  ) {
        $price *= 2; // set price x 2
    }
    return $price;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.

在 woocommerce 3+ 上测试并运行

Tested and works on woocommerce 3+

现在购物车中的自定义价格也将反映

Now custom prices in cart will be also reflected

这篇关于WooCommerce 3 中的条件产品价格购物车问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆