简体   繁体   English

WooCommerce覆盖购物车价格

[英]WooCommerce override cart prices

i am currently working with WooCommerce. 我目前正在与WooCommerce合作。 the following function hooks into "woocommerce_before_calculate_totals" and changes the value of the product. 以下函数挂接到“ woocommerce_before_calculate_totals”并更改产品的值。 This works fine with simple products, However this doesnt work with variations: 这适用于简单的产品,但是不适用于变体:

add_action( 'woocommerce_before_calculate_totals', 'wwp_variable_add_cart_price' );
function wwp_variable_add_cart_price( $cart_object ) {
$current_user = new WP_User(wp_get_current_user()->id);
$user_roles = $current_user->roles;
foreach ($user_roles as $roles) {
    if ($roles == 'administrator'){
        foreach ( $cart_object->cart_contents as $key => $value ) {
            $wholesale = get_post_meta( $value['product_id'], '_wholesale_price', true );
            if ($wholesale){    
            $value['data']->price = $wholesale;
            }
}}}}

Do i need to do something different for variations? 我需要对变化做一些不同的事情吗? Or should the above work? 还是上面的工作?

我设法通过添加以下代码行来解决此问题:

$wholesalev = get_post_meta( $value['data']->variation_id, '_wholesale_price', true );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM