【发布时间】:2018-10-01 03:54:28
【问题描述】:
我正在尝试将此代码添加到我的 WooCommerce 设置中,该设置在我放置 PHP 的任何位置添加一个购物车链接,然后在使用 AJAX 更改购物车中的项目时更新它:https://docs.woocommerce.com/document/show-cart-contents-total/
这里是sn-ps:
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 文件中:
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 不工作。我需要将第二个 sn-p 添加到 functions.php 中吗?
感觉我应该调用函数而不是定义它?
或者我是否需要以某种方式激活 AJAX 才能使其正常工作?
【问题讨论】:
标签: php ajax wordpress woocommerce cart