Bug in Woocommerce, when adding a coupon

Viewed 134

Hello I use the fragment system in Woocommerce to create custom templates in order to easily reload them asynchronously. I face a problem: I created a fragment in which I retrieve the "notices" that warn the user about his actions ("Basket updated.", etc.)

$all_notices  = WC()->session->get( 'wc_notices', array() );
d($all_notices);

result of the above code

I noticed that when the "notices" concern coupons, Woocommerce removes them immediately, so I can no longer retrieve them by myself via the fragment that reloads (I retrieve an empty array). Here is the code responsible for this:

/**
 * --> class-wc-ajax.php <--
 * AJAX apply coupon on checkout page.
 */
public static function apply_coupon() {

    check_ajax_referer( 'apply-coupon', 'security' );

    if ( ! empty( $_POST['coupon_code'] ) ) {
        WC()->cart->add_discount( wc_format_coupon_code( wp_unslash( $_POST['coupon_code'] ) ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
    } else {
        wc_add_notice( WC_Coupon::get_generic_coupon_error( WC_Coupon::E_WC_COUPON_PLEASE_ENTER ), 'error' );
    }

    wc_print_notices(); //// ICI
    wp_die();
}

and

// --> wc-notice-functions.php <--
function wc_print_notices( $return = false ) {
if ( ! did_action( 'woocommerce_init' ) ) {
    wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
    return;
}

$all_notices  = WC()->session->get( 'wc_notices', array() );
$notice_types = apply_filters( 'woocommerce_notice_types', array( 'error', 'success', 'notice' ) );

// Buffer output.
ob_start();

foreach ( $notice_types as $notice_type ) {
    if ( wc_notice_count( $notice_type ) > 0 ) {
        $messages = array();

        foreach ( $all_notices[ $notice_type ] as $notice ) {
            $messages[] = isset( $notice['notice'] ) ? $notice['notice'] : $notice;
        }

        wc_get_template(
            "notices/{$notice_type}.php",
            array(
                'messages' => array_filter( $messages ), // @deprecated 3.9.0
                'notices'  => array_filter( $all_notices[ $notice_type ] ),
            )
        );
    }
}

wc_clear_notices(); //// ICI

$notices = wc_kses_notice( ob_get_clean() );

if ( $return ) {
    return $notices;
}

echo $notices; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

finally

// --> wc-notice-functions.php <--
function wc_clear_notices() {
if ( ! did_action( 'woocommerce_init' ) ) {
    wc_doing_it_wrong( __FUNCTION__, __( 'This function should not be called before woocommerce_init.', 'woocommerce' ), '2.3' );
    return;
}
WC()->session->set( 'wc_notices', null );
}

I would like to know if through a hook or a filter maybe I could either retrieve the value of the variable stored in the Woocommerce session or maybe even duplicate it in my own variable before Woocommerce deletes his? Thank you in advance ✌️

0 Answers
Related