简体   繁体   中英

WooCommerce override cart prices

i am currently working with WooCommerce. the following function hooks into "woocommerce_before_calculate_totals" and changes the value of the product. 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 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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