仅在特定页面上启用AJAX添加到购物车按钮

仅在特定页面上启用AJAX添加到购物车按钮

本文介绍了Woocommerce-仅在特定页面上启用AJAX添加到购物车按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce商店设置中,我已经选中了以下选项:

On my Woocommerce shop settings I have checked the option:

[✓] 添加成功后重定向到购物车页面.

这对我的99%的商店来说都是好习惯.

And that's good behaviour for 99% of my shop.

在1个页面(带有自定义模板的自定义页面)上,我需要启用Ajax功能.

On 1 single page (custom page with custom template) I need to enable Ajax functionality though.

是否可以在functions.php中完成此任务?

Is there a way to accomplish this task in functions.php?

推荐答案

确定将此代码插入您的functions.php中.您必须用您要禁用重定向到购物车功能的页面名称替换变量$ your_ajax_page_slug.确保已在设置中选中启用对存档的AJAX添加到购物车按钮".

Ok insert this code into your functions.php. You have to replace the variable $your_ajax_page_slug with the name of your page that you want the redirect to cart functionality to be disabled. Ensure that you have 'Enable AJAX add to cart buttons on archives" checked in settings.

add_filter( 'woocommerce_get_script_data', 'modify_woocommerce_get_script_data', 20, 2 );

function modify_woocommerce_get_script_data ( $params, $handle ) {
    global $wp;

    $page_slug = '';
    $your_ajax_page_slug = 'your-page-slug';
    $current_url = home_url( $wp->request );

    // Break the URL by the delimiter
    $url_pieces = explode('/', $current_url);

    // Get the page slug
    if( is_array( $url_pieces ) )
        $page_slug = end( $url_pieces );

    if( $handle == 'wc-add-to-cart' && $page_slug == $your_ajax_page_slug ) {
        $params['cart_redirect_after_add'] = false;
    }

    return $params;
}

这篇关于Woocommerce-仅在特定页面上启用AJAX添加到购物车按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 09:52