如何添加到购物车商品缺货? 例外情况是class-wc-cart.php中的例外:
// Stock check - only check if we're managing stock and backorders are not allowed
if ( ! $product_data->is_in_stock() ) {
throw new Exception( sprintf( __( 'You cannot add "%s" to the cart because the product is out of stock.', 'woocommerce' ), $product_data->get_name() ) );
}
我在尝试:
add_filter( 'woocommerce_add_to_cart_validation', 'woocommerce_add_cart_item_data_custom',5,6 );
function woocommerce_add_cart_item_data_custom( $product_id, $quantity, $variation_id, $variation, $cart_item_data) {
$product_data = wc_get_product( $product_id );
$cart_item_data = (array) apply_filters( 'woocommerce_add_cart_item_data', $cart_item_data, $product_id, $variation_id );
$cart_id = WC()->cart->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );
$cart_item_key = WC()->cart->find_product_in_cart( $cart_id );
// Stock check - only check if we're managing stock and backorders are not allowed
if ( ! $product_data->is_in_stock() ) {
continue;
}
return false;
}
但不工作!请帮忙!!!