在Woocommerce中使用AJAX刷新/更新小型购物车 [英] Refresh / update minicart with AJAX in Woocommerce

查看:168
本文介绍了在Woocommerce中使用AJAX刷新/更新小型购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将此代码添加到我的WooCommerce设置中,以在我放置PHP的任何地方添加购物车链接,然后在使用AJAX更改购物车中的项目时对其进行更新: https://docs.woocommerce.com/document/show-cart-contents-total/

I’m trying to add this code to my WooCommerce setup that adds a shopping cart link wherever I put the PHP and then updates it upon changing items in the cart with AJAX: https://docs.woocommerce.com/document/show-cart-contents-total/

以下是摘要:

HTML-PHP:

<a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>

functions.php文件中:

In functions.php file:

function woocommerce_header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;

    ob_start();

    ?>
    <a class="cart-customlocation" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
    <?php
    $fragments['a.cart-customlocation'] = ob_get_clean();
    return $fragments;
}

但是AJAX无法正常工作.是我需要添加到functions.php中的第二个片段吗?

But the AJAX is not working. Is the second snippet all I need to add to the functions.php?

感觉我应该调用该函数,而不仅仅是定义它?

It feels like I should call the function and not just define it?

还是我一般需要以某种方式激活AJAX才能使其正常工作?

Or do I need to activate AJAX in general in some way to get it to work?

推荐答案

函数中缺少过滤器挂钩woocommerce_add_to_cart_fragments

The filter hook woocommerce_add_to_cart_fragments is missing from your function…

要使其正常工作,应为:

To get it work, it should be:

add_filter( 'woocommerce_add_to_cart_fragments', 'header_add_to_cart_fragment', 30, 1 );
function header_add_to_cart_fragment( $fragments ) {
    global $woocommerce;

    ob_start();

    ?>
    <a class="cart-customlocation" href="<?php echo esc_url(wc_get_cart_url()); ?>" title="<?php _e('View your shopping cart', 'woothemes'); ?>"><?php echo sprintf(_n('%d item', '%d items', $woocommerce->cart->cart_contents_count, 'woothemes'), $woocommerce->cart->cart_contents_count);?> - <?php echo $woocommerce->cart->get_cart_total(); ?></a>
    <?php
    $fragments['a.cart-customlocation'] = ob_get_clean();

    return $fragments;
}

代码进入您的活动子主题(或活动主题)的function.php文件中.未经测试.

Code goes in function.php file of your active child theme (or active theme). Untested.

这篇关于在Woocommerce中使用AJAX刷新/更新小型购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆