show wc_add notices on particular page in woo commerce custom plugin development

admin2025-01-07  3

I am new WordPress plugin development. I am working on a plugin where I create a custom cart page like the woo commerce cart. And I add wc add notice when an item removes from the cart page it shows a notice on this page as well as when I refresh shop show on the shop/arched page also.I don't know how to handle it. Sorry for the bad English and Thank You in advance.

            public function WhatsApp_cart_page_setting1() {
        if ( empty( WC()->session->get( 'WhatsApp_cart' ) ) ) {
            return;
        }
        if ( isset( $_GET['index_to_remove'] ) ) {
            $index        = sanitize_text_field( wp_unslash( $_GET['index_to_remove'] ) );
            $cart_session = WC()->session->get( 'WhatsApp_cart' );
            unset( $cart_session[ $index ] );
            WC()->session->set( 'WhatsApp_cart', $cart_session );
            $page = get_page_by_path( 'whatsapp-cart' );

            global $post;

            $whatsapp_slug = $post->post_name;
            if ( $whatsapp_slug === 'whatsapp-cart' ) {
                            wc_add_notice( 'Item removed from whatsapp cart successfully', 'success' );
            }
        }
        ?>

I am new WordPress plugin development. I am working on a plugin where I create a custom cart page like the woo commerce cart. And I add wc add notice when an item removes from the cart page it shows a notice on this page as well as when I refresh shop show on the shop/arched page also.I don't know how to handle it. Sorry for the bad English and Thank You in advance.

            public function WhatsApp_cart_page_setting1() {
        if ( empty( WC()->session->get( 'WhatsApp_cart' ) ) ) {
            return;
        }
        if ( isset( $_GET['index_to_remove'] ) ) {
            $index        = sanitize_text_field( wp_unslash( $_GET['index_to_remove'] ) );
            $cart_session = WC()->session->get( 'WhatsApp_cart' );
            unset( $cart_session[ $index ] );
            WC()->session->set( 'WhatsApp_cart', $cart_session );
            $page = get_page_by_path( 'whatsapp-cart' );

            global $post;

            $whatsapp_slug = $post->post_name;
            if ( $whatsapp_slug === 'whatsapp-cart' ) {
                            wc_add_notice( 'Item removed from whatsapp cart successfully', 'success' );
            }
        }
        ?>
Share Improve this question edited Oct 5, 2020 at 8:16 fuxia 107k38 gold badges255 silver badges459 bronze badges asked Oct 5, 2020 at 7:09 Fakhar alamFakhar alam 114 bronze badges 1
  • i create a cart page dynamically. $my_post = array( 'post_title' => 'WhatsApp Cart', 'post_content' => '[whatsapp_cart]', 'post_status' => 'publish', 'post_type' => 'page', ); – Fakhar alam Commented Oct 5, 2020 at 7:12
Add a comment  | 

1 Answer 1

Reset to default 0

I solve this problem below is code.

         add_action( 'woocommerce_init', array( $this, 'afwo_add_notices' ) );

             public function afwo_add_notices() {

        if ( isset( $_POST['whatsapp_cart_nonce_field'] ) && wp_verify_nonce( 
    sanitize_text_field( wp_unslash( $_POST['whatsapp_cart_nonce_field'] ) ), 
    'whatsapp_cart_field' ) ) {
                    echo '';
        }

        if ( empty( WC()->session->get( 'WhatsApp_cart' ) ) ) {
            return;
        }
        if ( isset( $_GET['index_to_remove'] ) ) {
            $index        = sanitize_text_field( wp_unslash( $_GET['index_to_remove'] 
   ) );
            $cart_session = WC()->session->get( 'WhatsApp_cart' );
            if ( isset( $cart_session[ $index ] ) ) {
                unset( $cart_session[ $index ] );
                WC()->session->set( 'WhatsApp_cart', $cart_session );

                wc_add_notice( 'Item removed from whatsapp cart successfully', 
        'success' );
            }
        }
    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260980a705.html

最新回复(0)