Hi @eltoine,
I’ve worked on the task with notices and here what I’ve came up with:
Solution 1. Simplest solution – add product to the cart via URL, e.g.:
https://example.com/product/test-product/?coupon=test&add-to-cart=2950
I.e. we are adding product 2950
via add-to-cart
argument. In this case, notice is displayed and we have a product in the cart. However, I understand, that sometimes this is not good enough, for example, when we want user to choose a variation of a variable product, so:
Solution 2. First you need update the plugin to the latest v1.2.4. After that you need to add this PHP snippet, e.g. to your (child) theme’s functions.php file:
add_action( 'alg_wc_url_coupons_after_coupon_applied', 'my_alg_wc_url_coupons_after_coupon_applied' );
if ( ! function_exists( 'my_alg_wc_url_coupons_after_coupon_applied' ) ) {
/**
* Save and clear notices after applying URL coupon.
*/
function my_alg_wc_url_coupons_after_coupon_applied() {
$all_notices = WC()->session->get( 'wc_notices', array() );
wc_clear_notices();
WC()->session->set( 'my_alg_wc_url_coupons_notices', $all_notices );
}
}
add_action( 'wp_head', 'my_alg_wc_url_coupons_notices' );
if ( ! function_exists( 'my_alg_wc_url_coupons_notices' ) ) {
/**
* Display saved notices when cart is not empty.
*/
function my_alg_wc_url_coupons_notices() {
if ( function_exists( 'WC' ) && ! WC()->cart->is_empty() && ( $delayed_notices = WC()->session->get( 'my_alg_wc_url_coupons_notices', array() ) ) && ! empty( $delayed_notices ) ) {
WC()->session->set( 'my_alg_wc_url_coupons_notices', null );
WC()->session->set( 'wc_notices', $delayed_notices );
}
}
}
I.e. this snippet will force notice to be delayed until there is some product added to the cart.
Please give it a try and let me know what you think.