Reputation: 13
I'm trying to check with my ERP if a product is in stock. So when you clic add to cart, I run a code checks if is in stock and then remove it if is out of stock. I want to show a message that said the product was remove cause outstock.
function custom_validate_stock() {
if(1==1){
$stockERP = 0;
if($stockERP < 1){
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product->id ){
WC()->cart->remove_cart_item($cart_item_key);
$removed = true;
break;
}
}
}
}
if($removed){
remove_action( 'woocommerce_cart_is_empty', 'wc_empty_cart_message', 10 );
add_action( 'woocommerce_cart_is_empty', 'custom_empty_cart_message', 10 );
}
}
}
function custom_empty_cart_message() {
$html = '<div class="col-12 offset-md-1 col-md-10"><p class="cart-empty">';
$html .= wp_kses_post( apply_filters( 'wc_empty_cart_message', __( 'Your product is out of stock, please refresh the page.', 'woocommerce' ) ) );
echo $html . '</p></div>';
}
Upvotes: 0
Views: 209
Reputation: 26319
Right after
WC()->cart->remove_cart_item($cart_item_key);
I think you could add a notice:
wc_add_notice( __( 'Thing removed.', 'your-textdomain' ), 'error' );
Upvotes: 1