重定向到另一个页面而不是显示通知

时间:2017-03-19 11:56:37

标签: javascript php wordpress forms

现在,当通过表单成功添加产品时,它只会停留在同一页面上,并显示通知"产品已添加" - 但是我想在成功添加产品后重定向到另一个页面,例如example.com/user。我不知道怎么能实现这个目标?

产品表单控制器:

public function process_submit() { 

        if ( ! isset( $_POST[ '_wcv-save_product' ] ) || !wp_verify_nonce( $_POST[ '_wcv-save_product' ], 'wcv-save_product' ) || !is_user_logged_in() ) { 
            return; 
        }

        $can_submit_live        = WC_Vendors::$pv_options->get_option( 'can_submit_live_products' ); 
        $current_post_status    = isset( $_POST[ 'post_status' ] ) ? $_POST[ 'post_status' ] : ''; 
        $can_edit_approved      = WC_Vendors::$pv_options->get_option( 'can_edit_approved_products' ); 
        $trusted_vendor         = ( get_user_meta( get_current_user_id(), '_wcv_trusted_vendor', true ) == 'yes' ) ? true: false;
        $untrusted_vendor       = ( get_user_meta( get_current_user_id(), '_wcv_untrusted_vendor', true ) == 'yes' ) ? true: false;

        if ( $trusted_vendor ) $can_submit_live = true; 
        if ( $untrusted_vendor ) $can_submit_live = false;




    $text = array( 'notice' => '', 'type' => 'success' ); 

    if ( isset( $_POST[ 'post_id' ] ) && is_numeric( $_POST[ 'post_id' ] ) ) { 

        $post_id = $this->save_product( (int) ( $_POST[ 'post_id' ] ) ); 

        if ( $post_id ) {

            $view   = get_permalink( $post_id ); 

            if ( isset( $_POST[ 'draft_button' ] ) ) {

                if ( $can_submit_live ) { 
                    $text[ 'notice' ] = sprintf( apply_filters( 'wcv_product_draft_msg',  __( 'Product draft saved.', 'wcvendors-pro' ) ), $view );
                } else { 
                    $text[ 'notice' ] = sprintf( apply_filters( 'wcv_product_draft_saved_msg', __( 'Product draft saved, pending review.', 'wcvendors-pro' ) ), $view );
                }

            } else { 

                if ( $can_submit_live ) { 
                    $text[ 'notice' ] = sprintf( apply_filters( 'wcv_product_updated_msg', __( 'Product Updated. <a href="%s">View product.</a>', 'wcvendors-pro' ) ), $view );
                } elseif( $can_edit_approved && 'pending' !== $current_post_status && 'draft' !== $current_post_status ) {
                    $text[ 'notice' ] = sprintf( apply_filters( 'wcv_product_updated_msg', __( 'Product Updated. <a href="%s">View product.</a>', 'wcvendors-pro' ) ), $view );
                } else { 
                    $text[ 'notice' ] = sprintf( apply_filters( 'wcv_product_review_msg', __( 'Product submitted for review. <a href="%s">Preview product.</a>', 'wcvendors-pro' ) ), $view );
                }
            }


        } else { 
            $text[ 'notice' ] = apply_filters( 'wcv_product_edit_problem_msg', __( 'There was a problem editing the product.', 'wcvendors-pro' ) );
            $text[ 'type' ] = 'error'; 
        }

    } else  { 

        $post_id = $this->save_product(); 

        $view   = get_permalink( $post_id ); 


        if ( $post_id ) { 
            if ( isset( $_POST[ 'draft_button' ] ) ) { 
                if ( $can_submit_live ) { 
                    $text[ 'notice' ] = sprintf( apply_filters( 'wcv_product_draft_msg',  __( 'Product draft saved.', 'wcvendors-pro' ) ), $view );
                } else { 
                    $text[ 'notice' ] = sprintf( apply_filters( 'wcv_product_draft_saved_msg', __( 'Product draft saved, pending review.', 'wcvendors-pro' ) ), $view );
                }
            } else { 
                if ( $can_submit_live ) { 
                    $text[ 'notice' ] = sprintf( apply_filters( 'wcv_product_added_msg', __( 'Product Added. <a href="%s">View product.</a>', 'wcvendors-pro' ) ), $view );
                } else { 
                    $text[ 'notice' ] = sprintf( apply_filters( 'wcv_product_review_msg', __( 'Product submitted for review. <a href="%s">Preview product.</a>', 'wcvendors-pro' ) ), $view );
                }
            }
        } else { 
            $text[ 'notice' ] = apply_filters( 'wcv_product_add_problem_msg', __( 'There was a problem adding the product.', 'wcvendors-pro' ) );
            $text[ 'type' ] = 'error'; 
        }               
    }

    wc_add_notice( $text[ 'notice' ], $text[ 'type' ] ); 

1 个答案:

答案 0 :(得分:1)

functions.php文件中添加此代码块。

/**
 * Redirect users after add to cart.
 */
function my_custom_add_to_cart_redirect( $url ) {
    $url = get_permalink('/page'); // URL to redirect 
    return $url;
}
add_filter( 'woocommerce_add_to_cart_redirect', 'my_custom_add_to_cart_redirect' );

希望这会帮助你!!