dongyou7739 2018-10-24 08:32
浏览 94
已采纳

使用woocommerce_add_to_cart_validation挂钩时禁用页面重新加载

When you add a product to the cart, the user fills in the fields and when you click on the Add to order button (this is essentially to add to the cart), the fields are checked (for clarity, I simplified this to the if condition ($a === 1)).

My function used with woocommerce_add_to_cart_validation hook is:

function so_validate_add_cart_item($true){
  $a = 1;
  if( $a === 1){
    $true = 0;
    return $true;
  }

}

Everything works well and the service is not added, but the page is reloaded.

By default $true = 1, but tried to return value false too, the page is overloaded.

How to stop page reloading not completely, but under a certain condition and it is desirable in this hook?

  • 写回答

1条回答 默认 最新

  • doufangmu9087 2018-10-24 09:25
    关注

    You can't avoid page reloading when using woocommerce_add_to_cart_validation filter hook.

    There is 2 cases when using woocommerce_add_to_cart_validation filter hook:

    1) Allow add to cart: You return the default filter hook argument (which is true):

    add_filter( 'woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 10, 3 );
    function filter_add_to_cart_validation( $passed, $product_id, $quantity ) {
        $a = 0; // <===
    
        if( $a === 1 ){
            $passed false; // Avoiding add to cart
    
            // (Optional) Display a custom eror notice
            wc_add_notice( __('Alert message: "add to cart avoided"', 'woocommerce' ), 'error' );
        }
        return $passed;
    }
    

    For ajax add to cart, the product will be added to cart (as by default) without reloading.

    For normal add to cart, the product will be added to cart and the page will be reloaded as by default.


    2) Avoid add to Cart: The condition in the IF statement matches, the filter hook argument is set to false and returned (optionally you can display an error message):

    add_filter( 'woocommerce_add_to_cart_validation', 'filter_add_to_cart_validation', 10, 3 );
    function filter_add_to_cart_validation( $passed, $product_id, $quantity) {
        $a = 1;
    
        // Any other value for $a than "1" will allow add to cart
        if( $a === 1){
            $passed false; // Avoid add to cart
    
            // (Optional) Display a custom eror notice
            wc_add_notice( __('Alert message: "add to cart avoided"', 'woocommerce' ), 'error' );
        }
        return $passed;
    }
    

    For ajax add to cart, customer will be redirected to the single product page (avoiding add to cart).

    For normal add to cart, the page will be reloaded as by default (avoiding add to cart).


    The only possible way to avoid page reloading should be to use a custom (Ajax) jQuery script, meaning that you should build your own validation functionality.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动