Woocommerce - added_to_cart 트리거
WooCommerce added_to_cart 트리거를 사용하여 특정 제품을 카트에 추가할 때 팝업을 트리거하려고 합니다.지금까지, 다음과 같이 성공했습니다.
jQuery('body').on('added_to_cart',function() {
alert("testing!");
});
제품이 카트에 추가되면 알림 상자가 표시됩니다.단, 특정 카테고리에 대해서만 알림이 표시되도록 합니다.그런데 장바구니에 추가한 제품이 어느 카테고리에 속하는지 어떻게 확인할 수 있나요?
카트에 추가하는 소스는, https://github.com/woothemes/woocommerce/blob/master/assets/js/frontend/add-to-cart.js 를 참조해 주세요.
이 문제의 트리거는 다음과 같습니다.
$( document.body ).trigger( 'added_to_cart', [ fragments, cart_hash, $thisbutton ] );
그래서 저도 이 문제에 부딪혔는데, 수정은 매우 간단합니다.
당신의 기능은 사실 정확합니다.그냥 포장을 하면 됩니다..ready()
기능.
코드는 다음과 같습니다.
jQuery(document).ready(function($){
$('body').on( 'added_to_cart', function(){
alert("testing!");
});
});
기본 WooCommerce html 구조를 사용하고 있다고 가정할 때, 이것은 도움이 될 것입니다.
http://demo2.woothemes.com/storefront/shop/에 있는 Storefront 데모 페이지에서 Chrome의 콘솔을 통해 테스트했습니다.카트에 'radios' 제품이 추가되고 상위 클래스에서 범주를 찾은 경우에만 경고 메시지가 표시됩니다.<li>
에 대응하고 있습니다.
$ = jQuery;
var lastCategory;
$('body').on('added_to_cart',function() {
if(lastCategory === 'radios'){
alert('a radio was added!');
}
});
$('.add_to_cart_button').on('click',function() {
lastCategory = $(this).closest('li').attr('class').split('product-cat-')[1].split(' ')[0];
});
템플릿의 "functions.php"에 이 코드를 삽입합니다.
add_action('wp_footer','SA_added_to_cart');
function SA_added_to_cart(){?>
<script type="text/javascript">
// Ready state
(function($){
$( document.body ).on( 'added_to_cart', function(){
alert("testing!");
});
})(jQuery); // "jQuery" Working with WP (added the $ alias as argument)
</script>
<?php
}
언급URL : https://stackoverflow.com/questions/34334797/woocommerce-added-to-cart-trigger
'programing' 카테고리의 다른 글
어레이 엔트리를 코드 한 줄의 확산 구문으로 바꾸시겠습니까? (0) | 2023.03.05 |
---|---|
oracle의 RANK() 함수와 DEXINK() 함수의 차이점은 무엇입니까? (0) | 2023.03.05 |
워드프레스의 바닥글 앞에 텍스트 추가 (0) | 2023.02.28 |
Angular.js: 페이지 로드 시 요소 높이 설정 (0) | 2023.02.28 |
플러그인 시스템 구조(wordpress, mybb...) (0) | 2023.02.28 |