问题
i am developing this shipping cost generator for TNT as a WordPress woo-commerce extention.the thing is if there is an error i need to show the error and restrict user from checkout.I used following method to do that. The problem is how i can remove this notice when the user entered the correct data and manage the thing done.
if (!empty($xml->ratedTransitTimeResponse->ratedProducts->ratedProduct[0]->quote->price)) {
$cost+= (float)$xml->ratedTransitTimeResponse->ratedProducts->ratedProduct[0]->quote->price;
}
else wc_add_wp_error_notices(new WP_Error(1, 'Shipping cost error'));
When user entered correct data user can proceed but still the error is there.I need to remove it! (I tried this far with lot of references so if i have miss anything please correct me.any new suggestions also appreciated)
回答1:
woo-commerce has their own predefined functions for this which i didn't know at that time.used wc_add_notice and wc_clear_notices solved the issue!
if(!empty($xml->ratedTransitTimeResponse->ratedProducts->ratedProduct[0]->quote->price)){
$cost+=(float)$xml->ratedTransitTimeResponse->ratedProducts->ratedProduct[0]->quote->price;
wc_clear_notices();
}
else
{ //wc_add_wp_error_notices(new WP_Error(1,'Shipping cost error'));
wc_clear_notices();
wc_add_notice( 'Shipping cost error', 'error' );
}
来源:https://stackoverflow.com/questions/37043382/add-or-remove-woocommerce-error-messages