我正在尝试将此代码添加到我的WooCommerce设置中,以在我放置PHP的任何地方添加购物车链接,然后在使用AJAX更改购物车中的商品时对其进行更新:https : //docs.woocommerce.com/document/show- 购物车内容总计/
以下是片段:
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文件中:
functions.php
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中的第二个片段吗?
感觉我应该调用该函数,而不仅仅是定义它?
还是我需要以某种方式激活AJAX才能正常工作?
woocommerce_add_to_cart_fragments您的功能中缺少过滤器挂钩…
woocommerce_add_to_cart_fragments
要使其正常工作,应为:
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文件中。未经测试。