所以我使用YITH的愿望清单插件(pro)我发现他们有一个短代码来显示产品的数量在清单中使用php
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 );
}
问题是,我不想让它计算愿望清单中的产品数量。我只希望图标的颜色改为红色。当Wishlist包含产品时=>1
如何调整当前代码来完成此操作?
附在他们网站的原始代码下面
这是一个简单的wordpress + woocommerce网站。短代码确实完美地发挥了它的作用,我只是想让它在愿望列表中有产品时改变图标的颜色,而不是计算有多少产品在您的短代码中删除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 );
}