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

WooCommerce 折扣:買一送一 50% 的折扣通知

WooCommerce discount: buy one get one 50% off with a notice(WooCommerce 折扣:買一送一 50% 的折扣通知)
本文介紹了WooCommerce 折扣:買一送一 50% 的折扣通知的處理方法,對大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時(shí)送ChatGPT賬號..

在我之前的問題之后WooCommerce 折扣:買一送一個(gè) 50% 的折扣我想在購物車中添加自定義通知,每當(dāng)特定產(chǎn)品(不是所有產(chǎn)品)被添加到購物車時(shí).

After my previous question WooCommerce discount: buy one get one 50% off I want to add custom notice to the cart, whenever a particular product(not all the products) gets added to the cart.

我想先檢查數(shù)量,如果是 1,那么我想顯示增加該產(chǎn)品數(shù)量的通知.我自己從互聯(lián)網(wǎng)上想出了一些東西,但我認(rèn)為我的解決方案不正確:

I want to check the quantity first, if it's 1 then I want to display a notice to increase the quantity of that product. I have figured something by myself from the internet and I don't think my solution is right:

add_action( 'wp', 'sp_custom_notice' );
function sp_custom_notice() {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    
    //to display notice only on cart page
    if ( ! is_cart() ) {
        return;
    }

    global $product;

    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
    if($product_id == 15730){
        //check for quantify if equal to 1 in cart


        wc_clear_notices();
        wc_add_notice( __("Add one more to get 50% off on 2nd product"), 'notice');
    }
}

如果有人能幫助我,那就太好了.

It will be great if anyone can help me on this.

推薦答案

更新 2020 年 7 月

如果您仍在使用我的回答中的代碼 對于您之前的問題,您也可以對其進(jìn)行一些更改以使其正常工作:

If you are still using the code from my answer to your previous question, you can change it a bit to get this working too:

add_action('woocommerce_cart_calculate_fees', 'add_custom_discount_2nd_at_50', 10, 1 );
function add_custom_discount_2nd_at_50( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) 
        return;

    // YOUR SETTINGS:
    $targeted_product_id = 40; // Set HERE your targeted product ID

    // Initializing variables
    $discount = $qty_notice = 0;
    $items_prices = array();

    // Loop through cart items
    foreach ( $cart->get_cart() as $key => $cart_item ) {
        if( in_array( $targeted_product_id, [$cart_item['product_id'], $cart_item['variation_id']] ) ){
            $quantity = (int) $cart_item['quantity'];
            $qty_notice += $quantity;
            for( $i = 0; $i < $quantity; $i++ ) {
                $items_prices[] = floatval( $cart_item['data']->get_price());
            }
        }
    }

    $count_items = count($items_prices); // Count items

    rsort($items_prices); // Sorting prices descending order

    if( $count_items > 1 ) {
        foreach( $items_prices as $key => $price ) {
            if( $key % 2 == 1 )
                $discount -= number_format( $price / 2, 2 );
        }
    }

    // Applying the discount
    if( $discount != 0 ){
        $cart->add_fee('Buy one get one 50% off', $discount );

        // Displaying a custom notice (optional)
        wc_clear_notices(); // clear other notices on checkout page.
        if( ! is_checkout() ){
            wc_add_notice( __("You get 50% of discount on the 2nd item"), 'notice');
        }
    }
    //  Display a custom notice on cart page when quantity is equal to 1.
    elseif( $qty_notice == 1 ){
        wc_clear_notices(); // clear other notices on checkout page.
        if( ! is_checkout() ){
            wc_add_notice( __( "Add one more to get 50% off on 2nd item" ), 'notice');
        }
    }
}

代碼進(jìn)入您的活動子主題(或活動主題)的functions.php文件經(jīng)過測試并有效.

注意:wc_add_notice()函數(shù)絕對不需要回顯

這篇關(guān)于WooCommerce 折扣:買一送一 50% 的折扣通知的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 和電話字段驗(yàn)證問題中添加自定義注冊字段)
Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產(chǎn)品中添加一個(gè)將更改價(jià)格的選擇字段)
Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 激光内雕_led玻璃_发光玻璃_内雕玻璃_导光玻璃-石家庄明晨三维科技有限公司 激光内雕-内雕玻璃-发光玻璃 | 防水接头-电缆防水接头-金属-电缆密封接头-不锈钢电缆接头 | arch电源_SINPRO_开关电源_模块电源_医疗电源-东佑源 | 阜阳成人高考_阜阳成考报名时间_安徽省成人高考网| 交流伺服电机|直流伺服|伺服驱动器|伺服电机-深圳市华科星电气有限公司 | 德国GMN轴承,GMN角接触球轴承,GMN单向轴承,GMN油封,GMN非接触式密封 | 除尘布袋_液体过滤袋_针刺毡滤料-杭州辉龙过滤技术有限公司 | 广东教师资格网-广东教师资格证考试网 | 广东燎了网络科技有限公司官网-网站建设-珠海网络推广-高端营销型外贸网站建设-珠海专业h5建站公司「了了网」 | UV-1800紫外光度计-紫外可见光度计厂家-翱艺仪器(上海)有限公司 | 四川实木门_成都实木门 - 蓬溪聚成门业有限公司 | 基业箱_环网柜_配电柜厂家_开关柜厂家_开关断路器-东莞基业电气设备有限公司 | 胜为光纤光缆_光纤跳线_单模尾纤_光纤收发器_ODF光纤配线架厂家直销_北京睿创胜为科技有限公司 - 北京睿创胜为科技有限公司 | LED太阳能中国结|发光红灯笼|灯杆造型灯|节日灯|太阳能灯笼|LED路灯杆装饰造型灯-北京中海轩光电 | 垃圾清运公司_环卫保洁公司_市政道路保洁公司-华富环境 | 成都珞石机械 - 模温机、油温机、油加热器生产厂家 | 安徽华耐泵阀有限公司-官方网站 安德建奇火花机-阿奇夏米尔慢走丝|高维|发那科-北京杰森柏汇 | 插针变压器-家用电器变压器-工业空调变压器-CD型电抗器-余姚市中驰电器有限公司 | 考勤系统_人事考勤管理系统_本地部署BS考勤系统_考勤软件_天时考勤管理专家 | 常州律师事务所_常州律所_常州律师-江苏乐天律师事务所 | 耐火浇注料-喷涂料-浇注料生产厂家_郑州市元领耐火材料有限公司 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 制氮设备_PSA制氮机_激光切割制氮机_氮气机生产厂家-苏州西斯气体设备有限公司 | 合肥活动房_安徽活动板房_集成打包箱房厂家-安徽玉强钢结构集成房屋有限公司 | 高速混合机_锂电混合机_VC高效混合机-无锡鑫海干燥粉体设备有限公司 | 气象监测系统_气象传感器_微型气象仪_气象环境监测仪-山东风途物联网 | 富森高压水枪-柴油驱动-养殖场高压清洗机-山东龙腾环保科技有限公司 | 机制砂选粉机_砂石选粉机厂家-盐城市助成粉磨科技有限公司 | PC构件-PC预制构件-构件设计-建筑预制构件-PC构件厂-锦萧新材料科技(浙江)股份有限公司 | 酒万铺-酒水招商-酒水代理 | 恒温槽_恒温水槽_恒温水浴槽-上海方瑞仪器有限公司 | Trimos测长机_测高仪_TESA_mahr,WYLER水平仪,PWB对刀仪-德瑞华测量技术(苏州)有限公司 | 苏州西朗门业-欧盟CE|莱茵UL双认证的快速卷帘门品牌厂家 | 重庆中专|职高|技校招生-重庆中专招生网 | 博莱特空压机|博莱特-阿特拉斯独资空压机品牌核心代理商 | 浙江栓钉_焊钉_剪力钉厂家批发_杭州八建五金制造有限公司 | 打造全球沸石生态圈 - 国投盛世| 骁龙云呼电销防封号系统-axb电销平台-外呼稳定『免费试用』 | 蒸汽吸附分析仪-进口水分活度仪|康宝百科 | 真石漆,山东真石漆,真石漆厂家,真石漆价格-山东新佳涂料有限公司 | 不锈钢/气体/液体玻璃转子流量计(防腐,选型,规格)-常州天晟热工仪表有限公司【官网】 |