在 Dokan 供应商插件上添加新产品后如何禁用重定向

How do I disable re-direct after adding a new product on Dokan Vendor Plugin

最近我制作了一个显示 Dokan "Add Product" 表单的自定义短代码,但现在每次我添加新产品时,我都会被重定向到 Dokan 仪表板中的产品编辑页面。我的 php 技能非常有限,因为这是我第一次完成任何编码,但我很快就掌握了东西。

有人可以帮我解决这个问题吗?

这是短代码显示的代码的结尾(在提交按钮之后)。

 </form>

            <?php } else { ?>

                <?php dokan_seller_not_enabled_notice(); ?>

            <?php } ?>

        <?php } else { ?>

            <?php do_action( 'dokan_can_post_notice' ); ?>

        <?php } ?>
    </div>

    <?php

        /**
         *  dokan_after_new_product_inside_content_area hook
         *
         *  @since 2.4
         */
        do_action( 'dokan_after_new_product_inside_content_area' );
    ?>

</div> <!-- #primary .content-area -->

<?php

    /**
     *  dokan_dashboard_content_after hook
     *  dokan_after_new_product_content_area hook
     *
     *  @since 2.4
     */
    do_action( 'dokan_dashboard_content_after' );
    do_action( 'dokan_after_new_product_content_area' );
?>

编辑此文件public_html/wp-content/plugins/dokan-lite/classes/template-products.php

do_action( 'dokan_new_product_added', $product_id, $post_data );

                    if ( current_user_can( 'dokan_edit_product' ) ) {
                        $redirect = dokan_edit_product_url( $product_id );
               } else {
                        $redirect = dokan_get_navigation_url( 'products' );
                    }

只需将 $redirect 的值替换为您的自定义值 url,如下所示

$redirect = ("https://www.wordpress.com/dashboard/products/");

对于编辑产品页面后的重定向替换下面相同的内容

do_action( 'dokan_product_updated', $post_id );

            //$edit_url = dokan_edit_product_url( $post_id );

            $redirect = apply_filters( 'dokan_add_new_product_redirect', dokan_edit_product_url( $post_id ), $post_id );

            wp_redirect( add_query_arg( array( 'message' => 'success' ), $redirect ) );
            exit;

使用新版本的 Dokan,您可以使用上面的代码在文件 dokan-lite/includes/Dashboard/Templates 中进行调整!

将此代码添加到您的子主题的 functions.php。

首先,我们删除默认的 ajax 操作,然后添加具有相同功能的自定义操作,但修改了 $redirect 值。

if ( wp_doing_ajax() ) {
    remove_action( 'wp_ajax_dokan_create_new_product', array( 'WeDevs\Dokan\Ajax', 'create_product' ) );
    add_action( 'wp_ajax_dokan_create_new_product', 'dokan_ajax_create_product' );
}
function dokan_ajax_create_product() {
    check_ajax_referer( 'dokan_reviews' );

    if ( ! current_user_can( 'dokan_add_product' ) ) {
        wp_send_json_error( __( 'You have no permission to do this action', 'dokan-lite' ) );
    }

    $submited_data = isset( $_POST['postdata'] ) ? wp_unslash( $_POST['postdata'] ) : ''; //phpcs:ignore

    parse_str( $submited_data, $postdata );

    $response = dokan_save_product( $postdata );

    if ( is_wp_error( $response ) ) {
        wp_send_json_error( $response->get_error_message() );
    }

    if ( is_int( $response ) ) {
        /* if ( current_user_can( 'dokan_edit_product' ) ) {
            $redirect = dokan_edit_product_url( $response );
        } else { */
            $redirect = dokan_get_navigation_url( 'products' );
        /* } */

        wp_send_json_success( $redirect );
    } else {
        wp_send_json_error( __( 'Something wrong, please try again later', 'dokan-lite' ) );
    }
}

并且,对于 non-ajax 个请求:

add_action( 'dokan_add_new_product_redirect', 'custom_dokan_add_new_product_redirect', 10, 2 );
function custom_dokan_add_new_product_redirect( $redirect, $product_id ) {
    $redirect = dokan_get_navigation_url( 'products' );
    return $redirect;
}