In this question WooCommerce: Add product to cart with price override? we can alter the price for all products in the cart.
What I need is to alter only products with Specific custom type called 'auction' and not change the rest of products.
Here is the code I use::
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
I tried::
if ( $value['product_type'] == 'auction') {
$value['data']->price = $custom_price;
}
If I rememeber correctly, $value['data']
is the product object.
So you should be able to use the is_type()
method:
if( $value['data']->is_type( 'simple' ) ){
// a simple product
} elseif( $value['data']->is_type( 'variable' ) ){
// a variable product
}
Don't forget that you can always use var_dump( $cart_object )
or better still error_log( json_encode( $cart_object ) )
(requires WP_DEBUG_LOG
be enabled) to get a look at what is in the variable.
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.