WooCommerce 结帐单选按钮,可根据特定项目小计设置百分比费用
Posted
技术标签:
【中文标题】WooCommerce 结帐单选按钮,可根据特定项目小计设置百分比费用【英文标题】:WooCommerce checkout radio buttons that set a percentage fee based on specific items subtotal 【发布时间】:2021-07-15 04:56:33 【问题描述】:我正在尝试在 woocommerce 结帐中实施保修选项。以下代码适用于静态价格值。
// Part 1 - Display Radio Buttons
add_action( 'woocommerce_review_order_before_payment', 'custom_checkout_radio_choice' );
function custom_checkout_radio_choice()
$chosen = WC()->session->get( 'radio_chosen' );
$chosen = empty( $chosen ) ? WC()->checkout->get_value( 'radio_choice' ) : $chosen;
$chosen = empty( $chosen ) ? '0' : $chosen;
$args = array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array(
'0' => '1 Year Repair or Replace Warranty - Included',
'75' => '2 Years Extended Warranty ($75.00)',
'112.5' => '3 Years Extended Warranty ($112.50)',
),
'default' => $chosen
);
echo '<div id="checkout-radio">';
echo '<h4>Choose your Warranty</h4>';
woocommerce_form_field( 'radio_choice', $args, $chosen );
echo '</div><br>';
// Part 2 - Add Fee and Calculate Total
add_action( 'woocommerce_cart_calculate_fees', 'custom_checkout_radio_choice_fee', 20, 1 );
function custom_checkout_radio_choice_fee( $cart )
if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
$radio = WC()->session->get( 'radio_chosen' );
if ( $radio )
$cart->add_fee( 'Warranty Option', $radio );
// Part 3 - Add Radio Choice to Session
add_action( 'woocommerce_checkout_update_order_review', 'custom_checkout_radio_choice_set_session' );
function custom_checkout_radio_choice_set_session( $posted_data )
parse_str( $posted_data, $output );
if ( isset( $output['radio_choice'] ) )
WC()->session->set( 'radio_chosen', $output['radio_choice'] );
有没有办法输出基于项目小计百分比计算的保修金额选项?喜欢:
2 年保修价格为小计价格的 10%。 3 年保修为 15%。示例:例如,如果小计为 85 美元,则显示的保修选项为:
2 年延长保修期 ($8.50) /* 10% */ 3 年延长保修期 ($12.75) /* 15% */可选:我也在尝试将此功能用于特定产品或一组产品,而不是所有产品。如果有办法设置它。
【问题讨论】:
【参考方案1】:要根据特定购物车项目小计(针对一组定义的产品 ID)和单选按钮(百分比保修选项)获取自定义百分比费用,请使用以下重新访问的代码:
// Custom function to get related cart items subtotal for specific defined product Ids
function get_related_items_subtotal( $cart )
// HERE below define the related targeted products IDs in the array
$targeted_ids = array(29, 27, 28, 72, 84, 95);
$custom_subtotal = 0; // Initialize
// Loop through cart items
foreach ( $cart->get_cart() as $item )
if ( array_intersect($targeted_ids, array($item['product_id'], $item['variation_id']) ) )
$custom_subtotal += $item['line_subtotal'] + $item['line_subtotal_tax'];
return $custom_subtotal;
// 1 - Display custom checkout radio buttons fields
add_action( 'woocommerce_review_order_before_payment', 'display_custom_checkout_radio_buttons' );
function display_custom_checkout_radio_buttons()
$custom_subtotal = get_related_items_subtotal( WC()->cart );
if ( $custom_subtotal > 0 )
$value = WC()->session->get( 'warranty' );
$value = empty( $value ) ? WC()->checkout->get_value( 'warranty' ) : $value;
$value = empty( $value ) ? '0' : $value;
echo '<div id="checkout-radio">
<h4>' . __("Choose your Warranty") .'</h4>';
woocommerce_form_field( 'warranty', array(
'type' => 'radio',
'class' => array( 'form-row-wide', 'update_totals_on_change' ),
'options' => array(
'0' => __( '1 Year Repair or Replace Warranty - Included', 'woocommerce' ),
'10' => __( '2 Years Extended Warranty', 'woocommerce' ) . ' (' . strip_tags( wc_price( 10 * $custom_subtotal / 100 ) ) . ')',
'15' => __( '3 Years Extended Warranty', 'woocommerce' ) . ' (' . strip_tags( wc_price( 15 * $custom_subtotal / 100 ) ) . ')',
),
), $value );
echo '</div>';
// 2 - Customizing Woocommerce checkout radio form field
add_filter( 'woocommerce_form_field_radio', 'custom_form_field_radio', 20, 4 );
function custom_form_field_radio( $field, $key, $args, $value )
if ( ! empty( $args['options'] ) && 'warranty' === $key && is_checkout() )
$field = str_replace( '</label><input ', '</label><br><input ', $field );
$field = str_replace( '<label ', '<label style="display:inline;margin-left:8px;" ', $field );
return $field;
// 3 - Add a percentage Fee based on radio buttons for specific defined product Ids
add_action( 'woocommerce_cart_calculate_fees', 'percentage_fee_based_on_radio_buttons', 20 );
function percentage_fee_based_on_radio_buttons( $cart )
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$percentage = (float) WC()->session->get( 'warranty' );
if ( $percentage )
$custom_subtotal = get_related_items_subtotal( $cart );
if ( $custom_subtotal > 0 )
$label_text = sprintf( __('Extended Warranty %d years', 'woocommerce'), $percentage == 10 ? 2 : 3 );
$cart->add_fee( $label_text, $custom_subtotal * $percentage / 100 );
// 4 - Set chosen radio button value to a WC Session variable
add_action( 'woocommerce_checkout_update_order_review', 'chosen_input_radio_button_value_to_wc_session' );
function chosen_input_radio_button_value_to_wc_session( $posted_data )
parse_str( $posted_data, $fields );
if ( isset( $fields['warranty'] ) )
WC()->session->set( 'warranty', $fields['warranty'] );
代码进入活动子主题(或活动主题)的functions.php 文件中。经过测试并且可以工作。
【讨论】:
哇!,就像一个魅力,虽然有一个问题,当我选择“2 年延长保修”时,它仍然在订单摘要上显示“延长保修 3 年”。截图:prnt.sc/11sko9p @RosalitoUdtohan 已更新……现在它会根据所选选项显示正确的年数。 嗨@LoicTheAztec,我也在考虑根据特定产品的原始价格而不是小计来计算百分比费用。可能将价格作为 SKU 包含某些特定词的计算的基础。这可能吗?以上是关于WooCommerce 结帐单选按钮,可根据特定项目小计设置百分比费用的主要内容,如果未能解决你的问题,请参考以下文章
Wordpress - 单选按钮结帐 woocommerce 显示/隐藏必填字段