更改 woocommerce 中特定产品的购物车商品数量
Change the cart item quantity for specific products in woocommerce
我可以更改某些特定产品的 WooCommerce 数量吗?
我试过:
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo "<b>".$_product->post_title.'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
}
如何获取购物车中的特定产品编号?
要更改数量,请参阅该代码之后。这是您重新访问的代码:
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data']; // Get an instance of the WC_Product object
echo "<b>".$product->get_title().'</b> <br> Quantity: '.$cart_item['quantity'].'<br>';
echo " Price: ".$product->get_price()."<br>";
}
已更新:现在要更改特定产品的数量,您需要使用挂钩在[=12=中的自定义函数] 动作挂钩:
add_action('woocommerce_before_calculate_totals', 'change_cart_item_quantities', 20, 1 );
function change_cart_item_quantities ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE below define your specific products IDs
$specific_ids = array(37, 51);
$new_qty = 1; // New quantity
// Checking cart items
foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = $cart_item['data']->get_id();
// Check for specific product IDs and change quantity
if( in_array( $product_id, $specific_ids ) && $cart_item['quantity'] != $new_qty ){
$cart->set_quantity( $cart_item_key, $new_qty ); // Change quantity
}
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。
已测试并有效
我可以更改某些特定产品的 WooCommerce 数量吗?
我试过:
global $woocommerce;
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values) {
$_product = $values['data']->post;
echo "<b>".$_product->post_title.'</b> <br> Quantity: '.$values['quantity'].'<br>';
$price = get_post_meta($values['product_id'] , '_price', true);
echo " Price: ".$price."<br>";
}
如何获取购物车中的特定产品编号?
要更改数量,请参阅该代码之后。这是您重新访问的代码:
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data']; // Get an instance of the WC_Product object
echo "<b>".$product->get_title().'</b> <br> Quantity: '.$cart_item['quantity'].'<br>';
echo " Price: ".$product->get_price()."<br>";
}
已更新:现在要更改特定产品的数量,您需要使用挂钩在[=12=中的自定义函数] 动作挂钩:
add_action('woocommerce_before_calculate_totals', 'change_cart_item_quantities', 20, 1 );
function change_cart_item_quantities ( $cart ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
return;
// HERE below define your specific products IDs
$specific_ids = array(37, 51);
$new_qty = 1; // New quantity
// Checking cart items
foreach( $cart->get_cart() as $cart_item_key => $cart_item ) {
$product_id = $cart_item['data']->get_id();
// Check for specific product IDs and change quantity
if( in_array( $product_id, $specific_ids ) && $cart_item['quantity'] != $new_qty ){
$cart->set_quantity( $cart_item_key, $new_qty ); // Change quantity
}
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。
已测试并有效