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

在單個產(chǎn)品頁面上添加一個復選框,這會在 Woo

Add a checkbox on single product pages that adds an additional cost in Woocommerce(在單個產(chǎn)品頁面上添加一個復選框,這會在 Woocommerce 中增加額外費用)
本文介紹了在單個產(chǎn)品頁面上添加一個復選框,這會在 Woocommerce 中增加額外費用的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

在 woocommerce 中,我使用以下代碼在管理產(chǎn)品編輯頁面上顯示輸入文本字段,允許我向產(chǎn)品添加附加定價選項:

In woocommerce, I am using the following code to display an input text field on admin product edit pages, that allow me to add an additonal pricing option to the product:

add_action( 'woocommerce_product_options_pricing', 'wc_cost_product_field' );
function wc_cost_product_field() {
    woocommerce_wp_text_input( array( 'id' => 'repair_price', 'class' => 'wc_input_price short', 'label' => __( 'Repair Cost', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' ) );
}

add_action( 'save_post', 'wc_cost_save_product' );
function wc_cost_save_product( $product_id ) {

     // stop the quick edit interferring as this will stop it saving properly, when a user uses quick edit feature
     if (wp_verify_nonce($_POST['_inline_edit'], 'inlineeditnonce'))
        return;

    // If this is a auto save do nothing, we only save when update button is clicked
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( isset( $_POST['repair_price'] ) ) {
        if ( is_numeric( $_POST['repair_price'] ) )
            update_post_meta( $product_id, 'repair_price', $_POST['repair_price'] );
    } else delete_post_meta( $product_id, 'repair_price' );
}

現(xiàn)在在產(chǎn)品頁面(在前端)上,我需要一個復選框維修成本 20 美元",當產(chǎn)品自定義字段定價選項填充一些價格時會出現(xiàn)該復選框.

Now on the product page (in the front end) I need a checkbox "Repair cost $20", that will appear when the product custom field pricing option is filled with some price.

如果客戶添加此選項,則會在產(chǎn)品上增加 20 美元的額外費用,并且還應反映在購物車和結帳頁面上.

If customer add this option, it will add an additional cost of $20 on the product and it should also be reflected on the cart and checkout pages.

任何幫助將不勝感激.

推薦答案

在下面的代碼中,我重新訪問了您的實際代碼并添加了所有必要的代碼以啟用單個產(chǎn)品頁面中的復選框選項,如果產(chǎn)品已填充(對于非銷售的非可變產(chǎn)品類型).

In the following code, I have revisited a bit your actual code and added all necessary code to enable the checkbox option in single product page if the pricing option in the product is filled (for non variable products type that are not on sale).

當客戶選擇該選項時,產(chǎn)品價格會增加,當添加到購物車時,價格變化會反映在購物車和結帳頁面中(請參閱最后的屏幕截圖).>

代碼:

When the option is selected by the customer, the product price is increased and when added to cart the price change is reflected in cart and checkout pages (see the screenshots at the end).

The code:
<p class="form-row form-row-wide" id="repair_option_field" data-priority=""><span class="woocommerce-input-wrapper"><label class="checkbox">' .__("修復選項:", "Woocommerce") .' <input type="checkbox" class="input-checkbox" name="repair_option" id="repair_option" value="1">+ ' .$repair_price_html .'</label></span></p><input type="hidden" name="repair_price" value="' . $repair_price . '"><input type="hidden" name="active_price" value="' . $active_price . '"></div>';//Jquery: 更新顯示價格?><script type="text/javascript">jQuery(函數(shù)($){var cb = 'input[name="repair_option"]'pp = 'p.price';//在更改/選擇一個變體$('form.cart').on('change', cb, function(){if( $(cb).prop('checked') === true )$(pp).html('<?php echo $disp_price_sum_html; ?>');別的$(pp).html('<?php echo $active_price_html; ?>');})});<?php}//Front: 計算新商品價格并將其添加為自定義購物車商品數(shù)據(jù)add_filter('woocommerce_add_cart_item_data', 'add_custom_product_data', 10, 3);函數(shù) add_custom_product_data( $cart_item_data, $product_id, $variation_id ) {如果 (isset($_POST['repair_option']) && !empty($_POST['repair_option'])) {$cart_item_data['new_price'] = (float) ($_POST['active_price'] + $_POST['repair_price']);$cart_item_data['repair_price'] = (float) $_POST['repair_price'];$cart_item_data['active_price'] = (float) $_POST['active_price'];$cart_item_data['unique_key'] = md5(microtime().rand());}返回 $cart_item_data;}//Front:設置新計算的購物車商品價格add_action('woocommerce_before_calculate_totals', 'extra_price_add_custom_price', 20, 1);功能 extra_price_add_custom_price($cart) {如果 (is_admin() && !defined('DOING_AJAX'))返回;if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )返回;foreach($cart->get_cart() as $cart_item) {如果 (isset($cart_item['new_price']))$cart_item['data']->set_price((float) $cart_item['new_price']);}}//前:在購物車項目中顯示選項add_filter('woocommerce_get_item_data', 'display_custom_item_data', 10, 2);功能 display_custom_item_data($cart_item_data, $cart_item) {如果(isset($cart_item['repair_price'])){$cart_item_data[] = 數(shù)組('名稱' =>__("修復選項", "woocommerce"),'價值' =>strip_tags( '+ ' . wc_price( wc_get_price_to_display( $cart_item['data'], array('price' => $cart_item['repair_price'] ) ) ) ));}返回 $cart_item_data;}

// Backend: Additional pricing option custom field add_action( 'woocommerce_product_options_pricing', 'wc_cost_product_field' ); function wc_cost_product_field() { woocommerce_wp_text_input( array( 'id' => '_repair_price', 'class' => 'wc_input_price short', 'label' => __( 'Repair Cost', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')' )); } // Backend: Saving product pricing option custom field value add_action( 'woocommerce_admin_process_product_object', 'save_product_custom_meta_data', 100, 1 ); function save_product_custom_meta_data( $product ){ if ( isset( $_POST['_repair_price'] ) ) $product->update_meta_data( '_repair_price', sanitize_text_field($_POST['_repair_price']) ); } // Front: Add a text input field inside the add to cart form on single product page add_action('woocommerce_single_product_summary','add_repair_price_option_to_single_product', 2 ); function add_repair_price_option_to_single_product(){ global $product; if( $product->is_type('variable') || ! $product->get_meta( '_repair_price' ) ) return; add_action('woocommerce_before_add_to_cart_button', 'product_option_custom_field', 30 ); } function product_option_custom_field(){ global $product; $active_price = (float) $product->get_price(); $repair_price = (float) $product->get_meta( '_repair_price' ); $repair_price_html = strip_tags( wc_price( wc_get_price_to_display( $product, array('price' => $repair_price ) ) ) ); $active_price_html = wc_price( wc_get_price_to_display( $product ) ); $disp_price_sum_html = wc_price( wc_get_price_to_display( $product, array('price' => $active_price + $repair_price ) ) ); echo '<div class="hidden-field"> <p class="form-row form-row-wide" id="repair_option_field" data-priority=""> <span class="woocommerce-input-wrapper"><label class="checkbox"> ' . __("Repair Option:", "Woocommerce") . ' <input type="checkbox" class="input-checkbox " name="repair_option" id="repair_option" value="1"> + ' . $repair_price_html . '</label></span></p> <input type="hidden" name="repair_price" value="' . $repair_price . '"> <input type="hidden" name="active_price" value="' . $active_price . '"></div>'; // Jquery: Update displayed price ?> <script type="text/javascript"> jQuery(function($) { var cb = 'input[name="repair_option"]' pp = 'p.price'; // On change / select a variation $('form.cart').on( 'change', cb, function(){ if( $(cb).prop('checked') === true ) $(pp).html('<?php echo $disp_price_sum_html; ?>'); else $(pp).html('<?php echo $active_price_html; ?>'); }) }); </script> <?php } // Front: Calculate new item price and add it as custom cart item data add_filter('woocommerce_add_cart_item_data', 'add_custom_product_data', 10, 3); function add_custom_product_data( $cart_item_data, $product_id, $variation_id ) { if (isset($_POST['repair_option']) && !empty($_POST['repair_option'])) { $cart_item_data['new_price'] = (float) ($_POST['active_price'] + $_POST['repair_price']); $cart_item_data['repair_price'] = (float) $_POST['repair_price']; $cart_item_data['active_price'] = (float) $_POST['active_price']; $cart_item_data['unique_key'] = md5(microtime().rand()); } return $cart_item_data; } // Front: Set the new calculated cart item price add_action('woocommerce_before_calculate_totals', 'extra_price_add_custom_price', 20, 1); function extra_price_add_custom_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['new_price'])) $cart_item['data']->set_price((float) $cart_item['new_price']); } } // Front: Display option in cart item add_filter('woocommerce_get_item_data', 'display_custom_item_data', 10, 2); function display_custom_item_data($cart_item_data, $cart_item) { if (isset($cart_item['repair_price'])) { $cart_item_data[] = array( 'name' => __("Repair option", "woocommerce"), 'value' => strip_tags( '+ ' . wc_price( wc_get_price_to_display( $cart_item['data'], array('price' => $cart_item['repair_price'] ) ) ) ) ); } return $cart_item_data; }

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

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

基于此答案的添加: 在 Woocommerce 的訂單詳情中顯示產(chǎn)品可選成本

允許將維修價格保存作為訂單項元數(shù)據(jù)并在訂單和電子郵件通知的任何地方顯示:

// Save chosen seats to each order item as custom meta data and display order items Warrenty everywhere
add_action('woocommerce_checkout_create_order_line_item', 'save_order_item_product_waranty', 10, 4 );
function save_order_item_product_waranty( $item, $cart_item_key, $values, $order ) {
    if( isset($values['repair_price']) && $values['repair_price'] > 0 ) {
        $key = __("Repair option", "woocommerce");
        $value = strip_tags( '+ '. wc_price( wc_get_price_to_display( $values['data'], array('price' => $values['repair_price']) ) ) );
        $item->update_meta_data( $key, $value );
    }
}

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

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

這篇關于在單個產(chǎn)品頁面上添加一個復選框,這會在 Woocommerce 中增加額外費用的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(liá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(自定義結帳“下訂單按鈕輸出html)
主站蜘蛛池模板: 常州律师事务所_常州律所_常州律师-江苏乐天律师事务所 | 慈溪麦田广告公司,提供慈溪广告设计。| 厌氧工作站-通用型厌氧工作站-上海胜秋科学仪器有限公司 | 螺杆泵_中成泵业| 生鲜配送系统-蔬菜食材配送管理系统-连锁餐饮订货配送软件-挪挪生鲜供应链管理软件 | 上海平衡机-单面卧式动平衡机-万向节动平衡机-圈带动平衡机厂家-上海申岢动平衡机制造有限公司 | 聚氨酯催化剂K15,延迟催化剂SA-1,叔胺延迟催化剂,DBU,二甲基哌嗪,催化剂TMR-2,-聚氨酯催化剂生产厂家 | 鲁尔圆锥接头多功能测试仪-留置针测试仪-上海威夏环保科技有限公司 | 污泥烘干机-低温干化机-工业污泥烘干设备厂家-焦作市真节能环保设备科技有限公司 | 安徽集装箱厂-合肥国彩钢结构板房工程有限公司 | 球磨机,节能球磨机价格,水泥球磨机厂家,粉煤灰球磨机-吉宏机械制造有限公司 | 自动化改造_智虎机器人_灌装机_贴标机-上海圣起包装机械 | 冷却塔风机厂家_静音冷却塔风机_冷却塔电机维修更换维修-广东特菱节能空调设备有限公司 | 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 | 煤机配件厂家_刮板机配件_链轮轴组_河南双志机械设备有限公司 | 环讯传媒,永康网络公司,永康网站建设,永康小程序开发制作,永康网站制作,武义网页设计,金华地区网站SEO优化推广 - 永康市环讯电子商务有限公司 | 建筑资质代办_工程施工资质办理_资质代办公司_北京众聚企服 | 交流伺服电机|直流伺服|伺服驱动器|伺服电机-深圳市华科星电气有限公司 | 重庆磨床过滤机,重庆纸带过滤机,机床伸缩钣金,重庆机床钣金护罩-重庆达鸿兴精密机械制造有限公司 | 一体化污水处理设备_生活污水处理设备_全自动加药装置厂家-明基环保 | 间苯二酚,间苯二酚厂家-淄博双和化工 | 江西自考网 | 金环宇|金环宇电线|金环宇电缆|金环宇电线电缆|深圳市金环宇电线电缆有限公司|金环宇电缆集团 | 【德信自动化】点胶机_全自动点胶机_自动点胶机厂家_塑料热压机_自动螺丝机-深圳市德信自动化设备有限公司 | 板式换热器_板式换热器价格_管式换热器厂家-青岛康景辉 | 上海办公室装修,办公楼装修设计,办公空间设计,企业展厅设计_写艺装饰公司 | 北京翻译公司_同传翻译_字幕翻译_合同翻译_英语陪同翻译_影视翻译_翻译盖章-译铭信息 | 24位ADC|8位MCU-芯易德科技有限公司 | 西安文都考研官网_西安考研辅导班_考研培训机构_西安在职考研培训 | 带式过滤机厂家_价格_型号规格参数-江西核威环保科技有限公司 | 防伪溯源|防窜货|微信二维码营销|兆信_行业内领先的防伪防窜货数字化营销解决方案供应商 | 仓储笼_仓储货架_南京货架_仓储货架厂家_南京货架价格低-南京一品仓储设备制造公司 | LED太阳能中国结|发光红灯笼|灯杆造型灯|节日灯|太阳能灯笼|LED路灯杆装饰造型灯-北京中海轩光电 | 桁架楼承板-钢筋桁架楼承板-江苏众力达钢筋楼承板厂 | ET3000双钳形接地电阻测试仪_ZSR10A直流_SXJS-IV智能_SX-9000全自动油介质损耗测试仪-上海康登 | 东莞办公家具厂家直销-美鑫【免费3D效果图】全国办公桌/会议桌定制 | 润滑油加盟_润滑油厂家_润滑油品牌-深圳市沃丹润滑科技有限公司 琉璃瓦-琉璃瓦厂家-安徽盛阳新型建材科技有限公司 | 泵阀展|阀门展|水泵展|流体机械展 -2025上海国际泵管阀展览会flowtech china | 右手官网|右手工业设计|外观设计公司|工业设计公司|产品创新设计|医疗产品结构设计|EMC产品结构设计 | 地源热泵一体机,地源热泵厂家-淄博汇能环保设备有限公司 | COD分析仪|氨氮分析仪|总磷分析仪|总氮分析仪-圣湖Greatlake |