在 Woocommerce 结帐页面中显示一个添加费用的复选框

在 Woocommerce 结帐页面中,我尝试在结帐页面上添加一个复选框

add_action( 'woocommerce_review_order_before_order_total', 'checkout_shipping_form_packing_addition', 20 );

function checkout_shipping_form_packing_addition( ){

echo '<tr class="packing-select"><th>';

woocommerce_form_field( 'add_gift_box', array(

    'type'          => 'checkbox',

    'class'         => array('add_gift_box form-row-wide'),

    'label'         => __('Hỗ trợ cài đặt'),

    'placeholder'   => __(''),

    ));

echo '</th><td>';}


add_action( 'wp_footer', 'woocommerce_add_gift_box' );

function woocommerce_add_gift_box() {

if (is_checkout()) {

?>

<script type="text/javascript">

jQuery( document ).ready(function( $ ) {

    $('#add_gift_box').click(function(){

        jQuery('body').trigger('update_checkout');

    });

});

</script>

<?php

}}

add_action( 'woocommerce_cart_calculate_fees', 'woo_add_cart_fee' );

function woo_add_cart_fee( $cart ){

    if ( ! $_POST || ( is_admin() && ! is_ajax() ) ) {

    return;

}


if ( isset( $_POST['post_data'] ) ) {

    parse_str( $_POST['post_data'], $post_data );

} else {

    $post_data = $_POST; // fallback for final checkout (non-ajax)

}


if (isset($post_data['add_gift_box'])) {

    $sl = WC()->cart->get_cart_contents_count();

    $extracost = 50000 * $sl; // not sure why you used intval($_POST['state']) ?

    WC()->cart->add_fee( 'Hỗ trợ cài đặt x '.$sl.'', $extracost );

}}

add_filter( 'woocommerce_form_field' , 'remove_order_comments_optional_fields_label', 10, 4 );

function remove_order_comments_optional_fields_label( $field, $key, $args, $value ) {

// Only on checkout page for Order notes field

if( 'add_gift_box' === $key && is_checkout() ) {

    $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';

    $field = str_replace( $optional, '', $field );

}

return $field;

}

它不像我想要的那样工作。我做错了什么?


任何帮助表示赞赏。


梦里花落0921
浏览 189回答 1
1回答

POPMUISE

您确实需要将 Wordpress Ajax 与 WC_sessions 结合使用,以使其像“添加结帐复选框字段以在 Woocommerce 中启用百分比费用”回答线程中一样工作。这是您重新访问的代码:// Display the custom checkbow field in checkoutadd_action( 'woocommerce_review_order_before_order_total', 'fee_installment_checkbox_field', 20 );function fee_installment_checkbox_field(){&nbsp; &nbsp; echo '<tr class="packing-select"><th>';&nbsp; &nbsp; woocommerce_form_field( 'installment_fee', array(&nbsp; &nbsp; &nbsp; &nbsp; 'type'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; => 'checkbox',&nbsp; &nbsp; &nbsp; &nbsp; 'class'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> array('installment-fee form-row-wide'),&nbsp; &nbsp; &nbsp; &nbsp; 'label'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> __('Support installation'),&nbsp; &nbsp; &nbsp; &nbsp; 'placeholder'&nbsp; &nbsp;=> __(''),&nbsp; &nbsp; ), WC()->session->get('installment_fee') ? '1' : '' );&nbsp; &nbsp; echo '</th><td>';}// jQuery - Ajax scriptadd_action( 'wp_footer', 'checkout_fee_script' );function checkout_fee_script() {&nbsp; &nbsp; // Only on Checkout&nbsp; &nbsp; if( is_checkout() && ! is_wc_endpoint_url() ) :&nbsp; &nbsp; if( WC()->session->__isset('installment_fee') )&nbsp; &nbsp; &nbsp; &nbsp; WC()->session->__unset('installment_fee')&nbsp; &nbsp; ?>&nbsp; &nbsp; <script type="text/javascript">&nbsp; &nbsp; jQuery( function($){&nbsp; &nbsp; &nbsp; &nbsp; if (typeof wc_checkout_params === 'undefined')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; $('form.checkout').on('change', 'input[name=installment_fee]', function(){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var fee = $(this).prop('checked') === true ? '1' : '';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: wc_checkout_params.ajax_url,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'action': 'installment_fee',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'installment_fee': fee,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success: function (result) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $('body').trigger('update_checkout');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; });&nbsp; &nbsp; </script>&nbsp; &nbsp; <?php&nbsp; &nbsp; endif;}// Get Ajax request and saving to WC sessionadd_action( 'wp_ajax_installment_fee', 'get_installment_fee' );add_action( 'wp_ajax_nopriv_installment_fee', 'get_installment_fee' );function get_installment_fee() {&nbsp; &nbsp; if ( isset($_POST['installment_fee']) ) {&nbsp; &nbsp; &nbsp; &nbsp; WC()->session->set('installment_fee', ($_POST['installment_fee'] ? true : false) );&nbsp; &nbsp; }&nbsp; &nbsp; die();}// Add a custom calculated fee conditionallyadd_action( 'woocommerce_cart_calculate_fees', 'set_installment_fee' );function set_installment_fee( $cart ){&nbsp; &nbsp; if ( is_admin() && ! defined('DOING_AJAX') || ! is_checkout() )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; if ( did_action('woocommerce_cart_calculate_fees') >= 2 )&nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; if ( 1 == WC()->session->get('installment_fee') ) {&nbsp; &nbsp; &nbsp; &nbsp; $items_count = WC()->cart->get_cart_contents_count();&nbsp; &nbsp; &nbsp; &nbsp; $fee_label&nbsp; &nbsp;= sprintf( __( "Support installation %s %s" ), '&times;', $items_count );&nbsp; &nbsp; &nbsp; &nbsp; $fee_amount&nbsp; = 50000 * $items_count;&nbsp; &nbsp; &nbsp; &nbsp; WC()->cart->add_fee( $fee_label, $fee_amount );&nbsp; &nbsp; }}add_filter( 'woocommerce_form_field' , 'remove_optional_txt_from_installment_checkbox', 10, 4 );function remove_optional_txt_from_installment_checkbox( $field, $key, $args, $value ) {&nbsp; &nbsp; // Only on checkout page for Order notes field&nbsp; &nbsp; if( 'installment_fee' === $key && is_checkout() ) {&nbsp; &nbsp; &nbsp; &nbsp; $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';&nbsp; &nbsp; &nbsp; &nbsp; $field = str_replace( $optional, '', $field );&nbsp; &nbsp; }&nbsp; &nbsp; return $field;}代码位于活动子主题(或活动主题)的 functions.php 文件中。使用店面主题在 Woocommerce 3.6.5 版上测试并运行。
打开App,查看更多内容
随时随地看视频慕课网APP