php yith wishlist pluggin -标题图标改变颜色时,有项目在wishlist

64jmpszr  于 2023-01-19  发布在  PHP
关注(0)|答案(1)|浏览(89)

所以我使用YITH Wishlist插件(Pro),我发现他们有一个简短的代码来显示使用PHP的Wishlist中的产品数量

if ( defined( 'YITH_WCWL' ) && ! function_exists( 
'yith_wcwl_get_items_count' ) ) {
  function yith_wcwl_get_items_count() {
    ob_start();
    ?>
  <a href="<?php echo esc_url( YITH_WCWL()->get_wishlist_url() ); ?>">
    <span class="yith-wcwl-items-count">
      <i class="yith-wcwl-icon fa fa-heart-o"><?php echo esc_html( 
yith_wcwl_count_all_products() ); ?></i>
    </span>
  </a>
<?php
return ob_get_clean();
  }

  add_shortcode( 'yith_wcwl_items_count', 'yith_wcwl_get_items_count' );
}

    if ( defined( 'YITH_WCWL' ) && ! function_exists( 
'yith_wcwl_ajax_update_count' ) ) {
  function yith_wcwl_ajax_update_count() {
   wp_send_json( array(
  'count' => yith_wcwl_count_all_products()
   ) );
  }

  add_action( 'wp_ajax_yith_wcwl_update_wishlist_count', 
'yith_wcwl_ajax_update_count' );
  add_action( 'wp_ajax_nopriv_yith_wcwl_update_wishlist_count', 
'yith_wcwl_ajax_update_count' );
}

if ( defined( 'YITH_WCWL' ) && ! function_exists( 
'yith_wcwl_enqueue_custom_script' ) ) {
  function yith_wcwl_enqueue_custom_script() {
    wp_add_inline_script(
      'jquery-yith-wcwl',
      "
        jQuery( function( $ ) {
      $( document ).on( 'added_to_wishlist removed_from_wishlist', 
function() {
        $.get( yith_wcwl_l10n.ajax_url, {
          action: 'yith_wcwl_update_wishlist_count'
        }, function( data ) {
          $('.yith-wcwl-items-count').children('i').html( data.count );
        } );
      } );
    } );
      "
    );
      }

      add_action( 'wp_enqueue_scripts', 
'yith_wcwl_enqueue_custom_script', 20 );
    }

问题是,我不想让它计算愿望清单中的产品数量。2我只想让图标颜色在愿望清单包含产品=〉1时变为“红色
我如何调整当前代码以实现此目的?
我附上下面的原始代码从他们的网站
这是一个简单的wordpress + woocommerce网站。短代码确实完美地工作,它的设置做什么,我只是想让它改变图标的颜色时,有产品在愿望列表中,而不是计数有多少产品

yxyvkwin

yxyvkwin1#

在你的简码中删除yith_wcwl_count_all_products()函数,因为你不想显示它。然后我们需要改变js脚本,如果我们有项目在我们的愿望列表中添加类或删除它。
完整代码
在js部分中,将$('.yith-wcwl-icon').addClass('red');$('.yith-wcwl-icon').removeClass('red');编辑为您想要使用的类。

if ( defined( 'YITH_WCWL' ) && ! function_exists( 'yith_wcwl_get_items_count' ) ) {
    function yith_wcwl_get_items_count() {
      ob_start();
      ?>
        <a href="<?php echo esc_url( YITH_WCWL()->get_wishlist_url() ); ?>">
          <span class="yith-wcwl-items-count">
            <i class="yith-wcwl-icon fa fa-heart-o"></i>
          </span>
        </a>
      <?php
      return ob_get_clean();
    }
  
    add_shortcode( 'yith_wcwl_items_count', 'yith_wcwl_get_items_count' );
  }
  
  if ( defined( 'YITH_WCWL' ) && ! function_exists( 'yith_wcwl_ajax_update_count' ) ) {
    function yith_wcwl_ajax_update_count() {
      wp_send_json( array(
        'count' => yith_wcwl_count_all_products()
      ) );
    }
  
    add_action( 'wp_ajax_yith_wcwl_update_wishlist_count', 'yith_wcwl_ajax_update_count' );
    add_action( 'wp_ajax_nopriv_yith_wcwl_update_wishlist_count', 'yith_wcwl_ajax_update_count' );
  }
  
  if ( defined( 'YITH_WCWL' ) && ! function_exists( 'yith_wcwl_enqueue_custom_script' ) ) {
    function yith_wcwl_enqueue_custom_script() {
      wp_add_inline_script(
        'jquery-yith-wcwl',
        "
          jQuery( function( $ ) {
            $( document ).on( 'added_to_wishlist removed_from_wishlist', function() {
              $.get( yith_wcwl_l10n.ajax_url, {
                action: 'yith_wcwl_update_wishlist_count'
              }, function( data ) {
                if(data.count > 0) {
                    //replace red with class you prefer
                    $('.yith-wcwl-icon').addClass('red');
                } else {
                    //if you change the class you add update the class you remove too
                    $('.yith-wcwl-icon').removeClass('red');
                }
              } );
            } );
          } );
        "
      );
    }
  
    add_action( 'wp_enqueue_scripts', 'yith_wcwl_enqueue_custom_script', 20 );
  }

相关问题