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

在 Woocommerce 簡單產(chǎn)品中添加一個將更改價格的選

Add a select field that will change price in Woocommerce simple products(在 Woocommerce 簡單產(chǎn)品中添加一個將更改價格的選擇字段)
本文介紹了在 Woocommerce 簡單產(chǎn)品中添加一個將更改價格的選擇字段的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我需要在單個產(chǎn)品中添加一個帶有自定義價格的選擇,如下面的屏幕截圖所示;

I need to add a select with a custom price in the single product as seen in the screenshots below;

這個想法是,通過從 (select) 中選擇一個選項,您可以將其提高到基本價格.

The idea is that by selecting an option from (select) you can increase it to the base price.

不使用變體,因為我的想法是做更多的事情,但我需要關(guān)于如何做到這一點的幫助.

Without using the variations, since my idea is to do other things more, but I need help on how to do this.

我已經(jīng)看到了稱為附加組件"的插件,但我不想使用插件.

I've seen plugins that do it are called "Add-On" but I do not want to use a plugin.

推薦答案

要在簡單產(chǎn)品(如可變產(chǎn)品)中添加一個選擇字段,該字段將根據(jù)下拉選擇的值更新基本價格,請嘗試以下操作:

To add a select field in simple products (like in variable products) that will update base price depending on the dopdown selected value, try this:

// Frontend: custom select field in product single pages
add_action( 'woocommerce_before_add_to_cart_button', 'fabric_length_product_field' );
function fabric_length_product_field() {
    global $product;

    if( $product->is_type('variable') ) return; // Not variable products

    $domain = 'woocommerce';
    $text   = array(
        __('cards', $domain),
        __('card', $domain),
        __('Total', $domain),
        get_woocommerce_currency_symbol(),
    );

    // Select Options array
    $options = array(
        ""          => __('Select package'),
        "12.00" => "1000 {$text[0]} - {$text[3]}0.012/{$text[1]} - {$text[2]} {$text[3]}12.00",
        "15.00" => "2000 {$text[0]} - {$text[3]}0.008/{$text[1]} - {$text[2]} {$text[3]}15.00",
        "20.00" => "3000 {$text[0]} - {$text[3]}0.007/{$text[1]} - {$text[2]} {$text[3]}20.00",
        "25.00" => "4000 {$text[0]} - {$text[3]}0.006/{$text[1]} - {$text[2]} {$text[3]}25.00",
    );

    // Select field
    woocommerce_form_field('cards_pack', array(
        'type'          => 'select',
        'class'         => array('my-field-class form-row-wide'),
        'label'         => __('Cards Pack selection', $domain),
        'required'      => true,
        'options'       => $options,
    ),'');

    // Data to be transmitted to jQuery
    $base_price = (float) wc_get_price_to_display( $product );
    $prices = array(
        ''      => wc_price($base_price),
        '12.00' => wc_price($base_price + 12),
        '15.00' => wc_price($base_price + 15),
        '20.00' => wc_price($base_price + 20),
        '25.00' => wc_price($base_price + 25),
    )

    // jQuery code
    ?>
    <script>
    jQuery(function($){
        var a = <?php echo json_encode($prices); ?>,
            b = 'p.price',
            c = 'select[name="cards_pack"]';

        $(c).on( 'change', function(){
            $.each( a, function( key, value ){
                if( $(c).val() == key )
                    $(b).html(value);
            });
        });
    });
    </script>
    <?php
}

// Add selected pack data as custom data to cart items
add_filter( 'woocommerce_add_cart_item_data', 'add_pack_data_to_cart_item_data', 20, 2 );
function add_pack_data_to_cart_item_data( $cart_item_data, $product_id ){
    if( ! isset($_POST['cards_pack']) )
        return $cart_item_data;

    $pack_price = (float) sanitize_text_field( $_POST['cards_pack'] );
    if( empty($pack_price) )
        return $cart_item_data;

    if($pack_price == 12.00) $cards = 1000;
    elseif($pack_price == 15.00) $cards = 2000;
    elseif($pack_price == 20.00) $cards = 3000;
    elseif($pack_price == 25.00) $cards = 4000;

    $product    = wc_get_product($product_id); // The WC_Product Object
    $base_price = (float) $product->get_price();

    // New price calculation
    $new_price = $base_price + $pack_price;

    // Prepare and save the data array
    $cart_item_data['pack_data'] = array(
        'cards'     => (int)   $cards,
        'pack'      => (int)   $pack_price,
        'new_price' => (float) $new_price,
    );
    $cart_item_data['unique_key'] = md5( microtime() . rand() ); // Make each item unique

    return $cart_item_data;
}

// Set conditionally a custom item price
add_action('woocommerce_before_calculate_totals', 'set_cutom_cart_item_price', 20, 1);
function set_cutom_cart_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    foreach (  $cart->get_cart() as $cart_item ) {
        if ( isset( $cart_item['pack_data']['new_price'] ) )
            $cart_item['data']->set_price( $cart_item['pack_data']['new_price'] );
    }
}


// Display custom data in  checkout page
add_filter( 'woocommerce_get_item_data', 'display_custom_cart_item_data', 10, 2 );
function display_custom_cart_item_data( $cart_data, $cart_item ) {
    $domain        = 'woocommerce';

    if ( isset( $cart_item['pack_data']['new_price'] ) ){
        $cart_data[] = array('name' => __( 'Cards pack', $domain ),
            'value' => $cart_item['pack_data']['cards'] );
    }
    return $cart_data;
}

代碼位于活動子主題(或活動主題)的 function.php 文件中.經(jīng)過測試和工作.

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

這篇關(guān)于在 Woocommerce 簡單產(chǎn)品中添加一個將更改價格的選擇字段的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中將自定義列添加到管理產(chǎn)品列表)
Customizing checkout quot;Place Orderquot; button output html(自定義結(jié)帳“下訂單按鈕輸出html)
Add birthday field to WooCommerce my account and admin user page(將生日字段添加到 WooCommerce 我的帳戶和管理員用戶頁面)
主站蜘蛛池模板: 泥浆在线密度计厂家-防爆数字压力表-膜盒-远传压力表厂家-江苏大亚自控设备有限公司 | 氢氧化钙设备_厂家-淄博工贸有限公司 | 锯边机,自动锯边机,双面涂胶机-建业顺达机械有限公司 | 板式换热器_板式换热器价格_管式换热器厂家-青岛康景辉 | 懂研帝_专业SCI论文润色机构_SCI投稿发表服务公司 | 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 不锈钢水管-不锈钢燃气管-卫生级不锈钢管件-不锈钢食品级水管-广东双兴新材料集团有限公司 | 全自动真空上料机_粉末真空上料机_气动真空上料机-南京奥威环保科技设备有限公司 | 机房监控|动环监控|动力环境监控系统方案产品定制厂家 - 迈世OMARA | 宁夏档案密集柜,智能密集柜,电动手摇密集柜-盛隆柜业宁夏档案密集柜厂家 | 球盟会·(中国)官方网站 | 车辆定位管理系统_汽车GPS系统_车载北斗系统 - 朗致物联 | 网站优化公司_北京网站优化_抖音短视频代运营_抖音关键词seo优化排名-通则达网络 | 航拍_专业的无人机航拍摄影门户社区网站_航拍网 | 游泳池设备安装工程_恒温泳池设备_儿童游泳池设备厂家_游泳池水处理设备-东莞市君达泳池设备有限公司 | 爱科技iMobile-专业的科技资讯信息分享网站 | 电缆桥架生产厂家_槽式/梯式_热镀锌线槽_广东东莞雷正电气 | 丁基胶边来料加工,医用活塞边角料加工,异戊二烯橡胶边来料加工-河北盛唐橡胶制品有限公司 | 建筑工程资质合作-工程资质加盟分公司-建筑资质加盟 | PCB厂|线路板厂|深圳线路板厂|软硬结合板厂|电路板生产厂家|线路板|深圳电路板厂家|铝基板厂家|深联电路-专业生产PCB研发制造 | 头条搜索极速版下载安装免费新版,头条搜索极速版邀请码怎么填写? - 欧远全 | 飞行者联盟-飞机模拟机_无人机_低空经济_航空技术交流平台 | 土壤有机碳消解器-石油|表层油类分析采水器-青岛溯源环保设备有限公司 | 临沂招聘网_人才市场_招聘信息_求职招聘找工作请认准【马头商标】 | 郑州大巴车出租|中巴车租赁|旅游大巴租车|包车|郑州旅游大巴车租赁有限公司 | 正压密封性测试仪-静态发色仪-导丝头柔软性测试仪-济南恒品机电技术有限公司 | 长城人品牌官网 | 上海小程序开发-小程序制作-上海小程序定制开发公司-微信商城小程序-上海咏熠 | 环氧乙烷灭菌器_压力蒸汽灭菌器_低温等离子过氧化氢灭菌器 _低温蒸汽甲醛灭菌器_清洗工作站_医用干燥柜_灭菌耗材-环氧乙烷灭菌器_脉动真空压力蒸汽灭菌器_低温等离子灭菌设备_河南省三强医疗器械有限责任公司 | 上海单片机培训|重庆曙海培训分支机构—CortexM3+uC/OS培训班,北京linux培训,Windows驱动开发培训|上海IC版图设计,西安linux培训,北京汽车电子EMC培训,ARM培训,MTK培训,Android培训 | 深圳APP开发公司_软件APP定制开发/外包制作-红匣子科技 | 成都租车_成都租车公司_成都租车网_众行宝 | 综合管廊模具_生态,阶梯护坡模具_检查井模具制造-致宏模具厂家 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 | 拼装地板,悬浮地板厂家,悬浮式拼装运动地板-石家庄博超地板科技有限公司 | 车间除尘设备,VOCs废气处理,工业涂装流水线,伸缩式喷漆房,自动喷砂房,沸石转轮浓缩吸附,机器人喷粉线-山东创杰智慧 | 99文库_实习生实用的范文资料文库站 | 鑫铭东办公家具一站式定制采购-深圳办公家具厂家直销 | 识禅_对禅的了解,从这里开始 | 盘式曝气器-微孔曝气器-管式曝气器-曝气盘-斜管填料 | 郑州市前程水处理有限公司 | 球盟会·(中国)官方网站 |