Im using the latest Woocommerce version with HPOS. To avoid orders created vie the admin panel (backend) with values i need unconditional i tried to create a function which stops creating the order if the billing postalcode is empty and no propduct is added to the order. Unfortunatle i can save / create the order if i only add the postalcode – it should not save the order if any of these two are not set.
Here my code – any idea how to fix it?
// During Order creation in the admin area - force to set at least one procduct and billing postalcode to avoid issues on the order overview screen and more
function avoid_orders_wout_b_postalcode_a_wout_products_9sadfM324($post_id, $post) {
// Get the order object
$order = wc_get_order($post_id);
// Check if the order has products
$items = $order->get_items();
if (empty($items)) {
// Add an error message to WooCommerce admin notices
WC_Admin_Notices::add_custom_notice('no_products_added_error', 'ERROR YCNC0084 - ' . __('You cannot create an order without adding any products', 'nm-framework'));
// Redirect to the edit order screen
wp_redirect(admin_url('post.php?post=' . $post_id . '&action=edit'));
exit;
}
// Check for postal code in billing and shipping address
$billing_postcode = $order->get_billing_postcode();
// $shipping_postcode = $order->get_shipping_postcode();
// if (empty($billing_postcode) && empty($shipping_postcode)) {
if ( empty($billing_postcode) ) {
// Add an error message to WooCommerce admin notices
WC_Admin_Notices::add_custom_notice('no_billing_postalcode_error', 'ERROR YCNC0024 - ' . __('You cannot create an order without a billing postal code', 'nm-framework'));
// Redirect to the edit order screen
wp_redirect(admin_url('post.php?post=' . $post_id . '&action=edit'));
exit;
}
}
add_action('woocommerce_process_shop_order_meta', 'avoid_orders_wout_b_postalcode_a_wout_products_9sadfM324', 99, 2); // must higher then 40 according to /a/59726544/11946596