久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 WooCommerce Ajax 上獲取產(chǎn)品 ID、名稱和數(shù)量添加

Get product id, name and quantity on WooCommerce Ajax added to cart to display a notice(在 WooCommerce Ajax 上獲取產(chǎn)品 ID、名稱和數(shù)量添加到購物車以顯示通知)
本文介紹了在 WooCommerce Ajax 上獲取產(chǎn)品 ID、名稱和數(shù)量添加到購物車以顯示通知的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

瀏覽了大量類似的問題,但目前都沒有成功.

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)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產(chǎn)品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產(chǎn)品的總訂單數(shù))
Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和電話字段驗證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產(chǎn)品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 国产精品69久久久久水密桃 | 精品国产一区二区三区久久久蜜月 | 欧美日韩综合一区 | 免费骚视频 | 在线a视频 | 2019天天干夜夜操 | 欧洲国产精品视频 | 一区二区三区不卡视频 | 亚洲+变态+欧美+另类+精品 | 91精品午夜窝窝看片 | 一级片在线观看 | 精品久久国产 | 亚洲国产成人精品在线 | 人妖无码 | 欧美一级欧美一级在线播放 | 91麻豆蜜桃一区二区三区 | 国产激情免费视频 | 自拍偷拍亚洲欧美 | 久久日韩精品一区二区三区 | 岛国毛片在线观看 | 97精品一区二区 | 中文字幕国产精品 | 黄色高清视频 | 久草视频在线播放 | 一级黄色毛片免费 | 欧美一区二区三区小说 | 狠狠干五月天 | 国产精品高潮呻吟 | 天天爽天天操 | 亚洲欧美日韩一区二区 | 亚洲高清在线免费观看 | 国产伦精品一区二区三区高清 | 精品视频一区二区 | 亚洲精品中文字幕av | 欧美a级成人淫片免费看 | 国产精品国产成人国产三级 | 日韩视频在线观看一区二区 | 日韩欧美亚洲一区 | 久久久久久看片 | 成人精品免费视频 | 国产成人精品免费 |