doupu0619
2018-04-20 12:17
浏览 133
已采纳

在Woocommerce中使用AJAX刷新/更新minicart

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/

Here are the snippets:

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>

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;
}

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?

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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试将此代码添加到我的WooCommerce设置中,无论我将PHP放在哪里,都会添加购物车链接 然后在使用AJAX更改购物车中的商品时更新它: https:// docs .woocommerce.com / document / show-cart-contents-total /

以下是摘录:

HTML - PHP:

 &lt; a class =“cart-customlocation”href =“&lt;?php echo wc_get_cart_url();?&gt;”  title =“&lt;?php _e('查看购物车');?&gt;”&gt;&lt;?php echo sprintf(_n('%d item','%d items',WC() - &gt;  cart-&gt; get_cart_contents_count()),WC() - &gt; cart-&gt; get_cart_contents_count());  ?&GT;  - &lt;?php echo WC() - &gt; cart-&gt; get_cart_total();  ?&gt;&lt; / a&gt; 
   
 
 

functions.php 文件中: \ n

  function woocommerce_header_add_to_cart_fragment($ fragments){
 global $ woocommerce; 
 
 ob_start(); 
 
?&gt; 
&lt; a class =“cart-customlocation”href  =“&lt;?php echo esc_url(wc_get_cart_url());?&gt;”  title =“&lt;?php _e('查看你的购物车','woothemes');?&gt;”&gt;&lt;?php echo sprintf(_n('%d item','%d items',$ woocommerce  - &gt; cart-&gt; cart_contents_count,'woothemes'),$ woocommerce-&gt; cart-&gt; cart_contents_count);?&gt;  - &lt;?php echo $ woocommerce-&gt; cart-&gt; get_cart_total();  ?&gt;&lt; / a&gt; 
&lt;?php 
 $ fragments ['a.cart-customlocation'] = ob_get_clean(); 
返回$ fragments; 
} 
   
 
 

但AJAX无效。 我需要在functions.php中添加第二个片段吗?

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

或者我是否需要通过某种方式激活AJAX才能使其正常工作?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongshi1934 2018-04-20 13:29
    已采纳

    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;
    }
    

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

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题