【发布时间】:2017-07-04 22:11:01
【问题描述】:
我一直在尝试做一些相当简单的事情:在用户将某些东西添加到他们的购物车后让购物车下拉并停留在那里,直到他们点击它为止。
我尝试在我的 childtheme 中通过 functions.php 添加一个 php 函数,添加自定义 javascript 代码,但它没有反应。我似乎无法使用 javascript/jquery 捕获事件。 Woocommerce 在向购物车添加内容后会重新加载页面,因为它是一种变体产品。
这是购物车的 html 代码:
<div id="shoppingcart-dropdown" class="mkd-shopping-cart-dropdown">
<ul>
<li>
<div class="mkd-item-image-holder">
<a itemprop="url" href="https://gemini-bracelets.com/collection/ladies/musthave-deluxe-vanilla-8mm/">
<img src="//gemini-bracelets.com/wp-content/uploads/2017/06/Musthave-Deluxe-Summer-Night-8mm-300x200.jpg" class="attachment-shop_thumbnail size-shop_thumbnail wp-post-image" alt="" width="300" height="200"> </a>
</div>
<div class="mkd-item-info-holder">
<h5 itemprop="name" class="mkd-product-title"><a itemprop="url" href="https://gemini-bracelets.com/collection/ladies/musthave-deluxe-vanilla-8mm/">Musthave Deluxe Summer Night 8mm</a></h5>
<span class="mkd-quantity">2</span>
<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">€</span>39,95</span> <a href="https://gemini-bracelets.com/cart/?remove_item=6c2e0a0990dacd72f16b437da82dc6cc&_wpnonce=663fbc7a93" class="remove" title="Remove this item"><span class="icon-arrows-remove"></span></a> </div>
</li>
<li>
<div class="mkd-item-image-holder">
<a itemprop="url" href="https://gemini-bracelets.com/collection/ladies/musthave-deluxe-vanilla-8mm/">
<img src="//gemini-bracelets.com/wp-content/uploads/2017/06/Musthave-Deluxe-Summer-Night-8mm-300x200.jpg" class="attachment-shop_thumbnail size-shop_thumbnail wp-post-image" alt="" width="300" height="200"> </a>
</div>
<div class="mkd-item-info-holder">
<h5 itemprop="name" class="mkd-product-title"><a itemprop="url" href="https://gemini-bracelets.com/collection/ladies/musthave-deluxe-vanilla-8mm/">Musthave Deluxe Summer Night 8mm</a></h5>
<span class="mkd-quantity">3</span>
<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">€</span>39,95</span> <a href="https://gemini-bracelets.com/cart/?remove_item=8ed888de28bd08234d727d879e5f7a36&_wpnonce=663fbc7a93" class="remove" title="Remove this item"><span class="icon-arrows-remove"></span></a> </div>
</li>
<div class="mkd-cart-bottom">
<div class="mkd-subtotal-holder clearfix">
<span class="mkd-total">TOTAL:</span>
<span class="mkd-total-amount">
<span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">€</span>199,75</span> </span>
</div>
<div class="mkd-btn-holder clearfix">
<a itemprop="url" href="https://gemini-bracelets.com/cart/" class="mkd-view-cart" data-title="VIEW CART"><span>VIEW CART</span></a>
</div>
<div class="mkd-btn-holder clearfix">
<a itemprop="url" href="https://gemini-bracelets.com/checkout/" class="mkd-view-cart" data-title="CHECKOUT"><span>CHECKOUT</span></a>
</div>
</div>
</ul>
</div>
这就是我一直在尝试用 jquery 做的事情:
window.jQuery( document ).ready( function ( $ ) {
$( 'body' ).on( 'added_to_cart', function ( event, fragments, cart_hash, $button ) {
$('div#shoppingcart-dropdown').addClass('.after-add');
$('body').on('click', function(){
$('#shoppingcart-dropdown').removeClass('.after-add');
})
} );
} );
【问题讨论】:
标签: javascript php jquery wordpress woocommerce