[英]Alter Cart Contents for Specific Product Type in WooCommerce?
In this question WooCommerce: Add product to cart with price override? 在此问题中WooCommerce:将产品添加到具有价格优先权的购物车? 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. 如果我正确地记住,则
$value['data']
是产品对象。
So you should be able to use the is_type()
method: 因此,您应该能够使用
is_type()
方法:
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. 不要忘记,您总是可以使用
var_dump( $cart_object )
或更佳的error_log( json_encode( $cart_object ) )
(需要启用WP_DEBUG_LOG
)来查看变量中的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.