【问题标题】:Update woocommerce shopping cart icon dynamically with ajax使用 ajax 动态更新 woocommerce 购物车图标
【发布时间】:2014-01-20 04:49:51
【问题描述】:

我已经搜索过这个问题的答案,但找不到答案。

在我的 woocommerce 商店中,我希望网站顶部有一个购物篮图标。当客户将某物放入购物车时,他们希望将此图标换成其他图标 - 只要购物车不为空,此图标(“非空”)就会一直存在。

我要的是一个 PHP 类型的 if / else 调用,它说“如果购物车是空的,则显示图标 A,否则显示图标 B”。如果有人能对此有所了解,我将不胜感激!

我可以按照这个 woocommerce 教程动态更新购物车文本,

http://docs.woothemes.com/document/show-cart-contents-total/

在我的页面中

  <?php global $woocommerce; 

 if($woocommerce->cart->get_cart_total()=='0') { ?>
<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/empty_cart.png" alt="" width="30" height="30">
 <?php }else{   ?>
 <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/full_cart.png" alt="" width="30" height="30">
    <?php } ?>


<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" >Your Cart : <?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>

在 Functions.php 中

    add_filter('add_to_cart_fragments', 'woocommerce_header_add_to_cart_fragment');



function woocommerce_header_add_to_cart_fragment( $fragments ) {

global $woocommerce;

ob_start();


if($woocommerce->cart->get_cart_total()=='0') {  ?>

<img src="<?php echo get_stylesheet_directory_uri(); ?>/images/empty_cart.png" alt="" width="30" height="30">
<?php
    }else{ ?>
    <img src="<?php echo get_stylesheet_directory_uri(); ?>/images/full_cart.png" alt="" width="30" height="30">
        <?php }
 ?>

<a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" >Your Cart : <?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-contents'] = ob_get_clean();

return $fragments;

}

目前无论购物车中有什么,它都会显示这两个图像。 您可以在以下位置查看上述实现 http://fungass.com/testing/shop/uncategorized/abc/

【问题讨论】:

    标签: ajax wordpress woocommerce cart shopping


    【解决方案1】:

    终于这样解决了,

     <?php global $woocommerce; ?>
    
      <a href="<?php echo $woocommerce->cart->get_cart_url(); ?>" ><?php if ($woocommerce->cart->cart_contents_count == 0){
            printf( '<img src="%s/images/empty.png" alt="empty" height="25" width="25">', get_stylesheet_directory_uri());
       }else{
           printf( '<img src="%s/images/full.png" alt="full" height="25" width="25">', get_stylesheet_directory_uri());
           }
       ?>  </a>
    
    <a class="cart-contents" href="<?php echo $woocommerce->cart->get_cart_url(); ?>" >
    Your Cart : <?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>
    

    【讨论】:

      【解决方案2】:

      我实际上建议您使用 cookie 和 JavaScript 来处理此功能,以获得额外的好处,即允许 缓存 插件在非 WooCommerce 页面上提供静态页面,但页面上仍有一些动态片段(例如购物车总数或只是一个填充的购物车图标)。

      PHP 方面,在 init,检查购物车总数并设置 cookie 值...

      // this should only ever fire on non-cached pages (so it SHOULD fire
      // whenever we add to cart / update cart / etc
      function update_cart_total_cookie()
      {
          global $woocommerce;
          $cart_total = $woocommerce->cart->cart_contents_count;
          setcookie('woocommerce_cart_total', $cart_total, 0, '/');
      }
      add_action('init', 'update_cart_total_cookie');
      

      JavaScript 方面,检查 cookie,然后更改图标...

      // use the custom woocommerce cookie to determine if the empty cart icon should show in the header or the full cart icon should show
      // *NOTE: I'm using the jQuery cookie plugin for convenience https://github.com/carhartl/jquery-cookie
      var cartCount = $.cookie("woocommerce_cart_total");
      if (typeof(cartCount) !== "undefined" && parseInt(cartCount, 10) > 0) {
          $(".full-cart-icon").show();
          $(".empty-cart-icon").hide();
      
          // optionally you can even use the cart total count if you want to show "(#)" after the icon
          // $(".either-cart-icon span").html("(" + cartCount + ")");
      }
      else {
          $(".full-cart-icon").hide();
          $(".empty-cart-icon").show();
      }
      

      希望这会有所帮助!玩得开心!

      【讨论】:

      • 这真的很有用。谢谢
      猜你喜欢
      • 2016-10-09
      • 1970-01-01
      • 2018-07-29
      • 1970-01-01
      • 2017-01-20
      • 2020-01-21
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      相关资源
      最近更新 更多