問題描述
瀏覽了大量類似的問題,但目前都沒有成功.
Browsed a load of similar questions with no success so far.
我想在常規(guī)頁面上顯示一個 WC 通知,命名添加到購物車的最后一個項目.
I want to display a WC notice naming the last item added to the cart on a regular page.
通知已啟動并正在運行,但是,到目前為止,我無法識別添加到購物車的最后一件商品的 ID.
Notification is up and running, however, so far I was not able to identify the ID of last item added to the cart.
我已經(jīng)試過了
$items = WC()->cart->get_cart();
$ids = array();
foreach($items as $item => $values) {
$_product = $values['data']->post;
$ids[] = $_product->ID;
}
$last_product_id = end($ids);
$added_product = wc_get_product( $last_product_id );
$added_product_name = $added_product->get_title();
但據(jù)我所知,購物車內(nèi)容在 AJAX 調(diào)用期間不會更新.獲取產(chǎn)品ID的最簡單方法應該是包含它的AJAX參數(shù),但不能通過$_GET讀取.
But as I've learned cart content does not get updated during AJAX calls. The easiest way to obtain the product ID should be the AJAX parameter containing it, but it cannot be read via $_GET.
有誰知道通過 WC hook/jQuery 添加的最后一個項目的產(chǎn)品 ID 的檢索方法嗎?
Does anyone know of a way to retrieve the product ID of the last item added via WC hook/jQuery?
推薦答案
對于 Ajax added_to_cart
委托事件.
For Ajax added_to_cart
delegated event.
使用 jQuery,您可以輕松獲取產(chǎn)品 ID、產(chǎn)品名稱以及已通過 Ajax 加入購物車的產(chǎn)品數(shù)量.
Using jQuery you can get easily the product ID, the product name, and the quantity of a product that has been added to cart with Ajax.
在此代碼示例中,使用 Sweet Alert 組件(SWAL 2),當將產(chǎn)品添加到購物車時,我們會顯示一個帶有產(chǎn)品名稱(及其 ID)的消息燈箱:>
Here in this code example using Sweet Alert component (SWAL 2), when a product is added to cart, we display a message lightbox, with the product name (and its ID):
// Add the product name as data argument to Ajax add to cart buttons
add_filter( "woocommerce_loop_add_to_cart_args", "filter_wc_loop_add_to_cart_args", 20, 2 );
function filter_wc_loop_add_to_cart_args( $args, $product ) {
if ( $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ) {
$args['attributes']['data-product_name'] = $product->get_name();
}
return $args;
}
// On Ajax added to cart, shows a lightbox with the product name (and the product id)
add_action( 'wp_footer', 'ajax_added_to_cart_popup_script' );
function ajax_added_to_cart_popup_script() {
?>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script>
<script type="text/javascript">
jQuery( function($){
// On "added_to_cart" live event
$(document.body).on('added_to_cart', function( a, b, c, d ) {
var prod_id = d.data('product_id'), // Get the product name
prod_qty = d.data('quantity'), // Get the quantity
prod_name = d.data('product_name'); // Get the product name
Swal.fire({
title: '<?php _e("Added to cart!"); ?>',
text: prod_name+' ('+prod_id+')',
showCancelButton: true,
confirmButtonColor: '#000',
cancelButtonColor: '#3085d6',
confirmButtonText: '<?php _e("View-cart"); ?>',
cancelButtonText: '<?php _e("Continue shopping"); ?>'
}).then((result) => {
if (result.value) {
window.location.href = '<?php echo wc_get_cart_url(); ?>';
}
});
});
});
</script>
<?php
}
代碼位于活動子主題(或活動主題)的 function.php 文件中.經(jīng)測試有效.
Code goes in function.php file of your active child theme (or active theme). Tested and works.
相關(guān):
- 針對特定的 Woocommerce 購物車項目計數(shù),在 ajax 添加到購物車時顯示甜蜜警報
- Woocommerce:添加到購物車后的自定義 jquery 事件
這篇關(guān)于在 WooCommerce Ajax 上獲取產(chǎn)品 ID、名稱和數(shù)量添加到購物車以顯示通知的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!