pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

在 WooCommerce Ajax 上獲取產品 ID、名稱和數量添加

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

問題描述

限時送ChatGPT賬號..

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

Browsed a load of similar questions with no success so far.

我想在常規頁面上顯示一個 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.

我已經試過了

    $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();

但據我所知,購物車內容在 AJAX 調用期間不會更新.獲取產品ID的最簡單方法應該是包含它的AJAX參數,但不能通過$_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 添加的最后一個項目的產品 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,您可以輕松獲取產品 ID產品名稱以及已通過 Ajax 加入購物車的產品數量.

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),當將產品添加到購物車時,我們會顯示一個帶有產品名稱(及其 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 文件中.經測試有效.

Code goes in function.php file of your active child theme (or active theme). Tested and works.

相關:

  • 針對特定的 Woocommerce 購物車項目計數,在 ajax 添加到購物車時顯示甜蜜警報
  • Woocommerce:添加到購物車后的自定義 jquery 事件

這篇關于在 WooCommerce Ajax 上獲取產品 ID、名稱和數量添加到購物車以顯示通知的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Add programmatically a downloadable file to Woocommerce products(以編程方式將可下載文件添加到 Woocommerce 產品)
Get today#39;s total orders count for each product in Woocommerce(獲取今天 Woocommerce 中每種產品的總訂單數)
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 簡單產品中添加一個將更改價格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 东莞市踏板石餐饮管理有限公司_正宗桂林米粉_正宗桂林米粉加盟_桂林米粉加盟费-东莞市棒子桂林米粉 | 广州冷却塔维修厂家_冷却塔修理_凉水塔风机电机填料抢修-广东康明节能空调有限公司 | 美的商用净水器_美的直饮机_一级代理经销商_Midea租赁价格-厂家反渗透滤芯-直饮水批发品牌售后 | 自进式锚杆-自钻式中空注浆锚杆-洛阳恒诺锚固锚杆生产厂家 | 食品级焦亚硫酸钠_工业级焦亚硫酸钠_焦亚硫酸钠-潍坊邦华化工有限公司 | 合肥网络推广_合肥SEO网站优化-安徽沃龙First | 打包箱房_集成房屋-山东佳一集成房屋有限公司| 北京包装设计_标志设计公司_包装设计公司-北京思逸品牌设计 | 锂电池生产厂家-电动自行车航模无人机锂电池定制-世豹新能源 | 安平县鑫川金属丝网制品有限公司,防风抑尘网,单峰防风抑尘,不锈钢防风抑尘网,铝板防风抑尘网,镀铝锌防风抑尘网 | 奇酷教育-Python培训|UI培训|WEB大前端培训|Unity3D培训|HTML5培训|人工智能培训|JAVA开发的教育品牌 | 压缩空气检测_气体_水质找上海京工-服务专业、价格合理 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | 抓斗式清污机|螺杆式|卷扬式启闭机|底轴驱动钢坝|污水处理闸门-方源水利机械 | 南溪在线-南溪招聘找工作、找房子、找对象,南溪综合生活信息门户! | 磁力抛光研磨机_超声波清洗机厂家_去毛刺设备-中锐达数控 | 北京成考网-北京成人高考网 | 口信网(kousing.com) - 行业资讯_行业展会_行业培训_行业资料 | pbootcms网站模板|织梦模板|网站源码|jquery建站特效-html5模板网 | 无缝钢管-聊城无缝钢管-小口径无缝钢管-大口径无缝钢管 - 聊城宽达钢管有限公司 | 截齿|煤截齿|采煤机截齿|掘进机截齿|旋挖截齿-山东卓力截齿厂家报价 | 单锥双螺旋混合机_双螺旋锥形混合机-无锡新洋设备科技有限公司 | 学叉车培训|叉车证报名|叉车查询|叉车证怎么考-工程机械培训网 | 山东聚盛新型材料有限公司-纳米防腐隔热彩铝板和纳米防腐隔热板以及钛锡板、PVDF氟膜板供应商 | 艺术漆十大品牌_艺术涂料加盟代理_蒙太奇艺术涂料厂家品牌|艺术漆|微水泥|硅藻泥|乳胶漆 | 电脑知识|软件|系统|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网 | 沈阳真空机_沈阳真空包装机_沈阳大米真空包装机-沈阳海鹞真空包装机械有限公司 | 塑钢课桌椅、学生课桌椅、课桌椅厂家-学仕教育设备首页 | EFM 022静电场测试仪-套帽式风量计-静电平板监测器-上海民仪电子有限公司 | 医用空气消毒机-医用管路消毒机-工作服消毒柜-成都三康王 | 西安烟道厂家_排气道厂家_包立管厂家「陕西西安」推荐西安天宇烟道 | TPE塑胶原料-PPA|杜邦pom工程塑料、PPSU|PCTG材料、PC/PBT价格-悦诚塑胶 | 全自动包装机_灌装机生产厂家-迈驰包装设备有限公司 | 壹车网 | 第一时间提供新车_资讯_报价_图片_排行! | 环球电气之家-中国专业电气电子产品行业服务网站! | 海日牌清洗剂-打造带电清洗剂、工业清洗剂等清洗剂国内一线品牌 海外整合营销-独立站营销-社交媒体运营_广州甲壳虫跨境网络服务 | 定量包装秤,吨袋包装称,伸缩溜管,全自动包装秤,码垛机器人,无锡市邦尧机械工程有限公司 | 螺旋叶片_螺旋叶片成型机_绞龙叶片_莱州源泽机械制造有限公司 | 小型玉石雕刻机_家用玉雕机_小型万能雕刻机_凡刻雕刻机官网 | 购买舔盐、舔砖、矿物质盐压块机,鱼饵、鱼饲料压块机--请到杜甫机械 | 继电器模组-IO端子台-plc连接线-省配线模组厂家-世麦德 |