Thanks for your input, it was very useful. I’ve contained a slightly wider block of code within the new condition…
/**
* Check at checkout if any
*
* @access public
* @since 1.0.0
* @param Array $fields
* @param Object $errors
*/
public function ycve_variation_validate_dt( $fields, $errors ){
foreach ( WC()->cart->cart_contents as $key => $item ) {
$product_id = $item['product_id'];
$product = wc_get_product( $product_id );
// Check if the product is a variable product
if ( $product->is_type( 'variable' ) ) {
$variations = $product->get_children();
if( ! empty( $variations ) ){
foreach( $variations as $variation ){
$this->ycve_check_exp_date_to( $variation );
}
}
$newProduct = wc_get_product( $product_id );
$newVariations = $newProduct->get_children();
if ( ! in_array( $item['variation_id'], $newVariations ) ) {
$name = $item['data']->get_name();
$itemsCount = count( WC()->cart->get_cart() );
$cart_url = wc_get_cart_url();
$errorMsg = '<b>' . $name . '</b> it is really not available! already removed from cart!! <a href="' . $cart_url . '"><b>Check cart</b></a>';
if( $itemsCount > 1 ){
$errorMsg = 'Product: <b>' . $name . '</b> in your cart, it is really not available and already removed from cart!! <a href="' . $cart_url . '"><b>Check cart</b></a>';
}
$errors->add( 'validation', $errorMsg );
}
}
}//cart end
}
Plugin Author
yakacj
(@yakacj)
Hello, thanks for the bug correction! I’ll correct this in next update.