在WOO商务定制插件开发的特定页面上显示WC_ADD通知

时间:2020-10-05 作者:Fakhar alam

我是新的WordPress插件开发人员。我正在开发一个插件,其中我创建了一个自定义购物车页面,比如woo commerce购物车。我还添加了wc add notice,当商品从购物车页面中删除时,它会在此页面上显示一个通知,以及当我刷新店铺/拱形页面上的店铺显示时。我不知道怎么处理。对不起,英语不好,提前谢谢你。

            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\' );
            }
        }
        ?>

1 个回复
SO网友:Fakhar alam

我下面解决这个问题的是代码。

         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\' );
            }
        }
    }