首页 > 解决方案 > 将折扣添加到 woocomerce 购物车页面 INCL。税

问题描述

我正在尝试根据这个问题修改代码:

// Display the checkout field in cart page totals section
add_action( 'woocommerce_cart_totals_before_order_total',   'display_priority_fee_checkbox_field', 20 );
function display_priority_fee_checkbox_field(){
echo '<tr class="installment-section">
<th>'.__("Priority Dispatch").'</th><td>';

woocommerce_form_field( 'priority_fee', array(
    'type'          => 'checkbox',
    'class'         => array('form-row-wide'),
    'label'         => __(' $20.00'),
), WC()->session->get('priority_fee') ? '1' : '' );

echo '<div class="tooltip">?
<span class="tooltiptext">'.__("By selecting this option... ").'</span>
</div></td>';
}

// Remove "(optional)" text from the field
add_filter( 'woocommerce_form_field' ,  'remove_optional_txt_from_priority_fee_checkbox', 10, 4 );
function remove_optional_txt_from_priority_fee_checkbox( $field, $key, $args, $value ) {
// Only on checkout page for Order notes field
if( 'priority_fee' === $key ) {
    $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
    $field = str_replace( $optional, '', $field );
}
return $field;
}

// jQuery :: Ajax script
add_action( 'wp_footer', 'priority_fee_js_script' );
function priority_fee_js_script() {
// On Order received page, remove the wc session variable if it exist
if ( is_wc_endpoint_url('order-received')
    && WC()->session->__isset('priority_fee') ) :

    WC()->session->__unset('priority_fee');

// On Cart page: jQuert script
elseif ( is_cart() ) :

?>
<script type="text/javascript">
jQuery( function($){
    if (typeof woocommerce_params === 'undefined')
        return false;

    var c = 'input[name=priority_fee]';

    $(document.body).on( 'click change', c, function(){
        console.log('click');
        var fee = $(c).is(':checked') ? '1' : '';

        $.ajax({
            type: 'POST',
            url: woocommerce_params.ajax_url,
            data: {
                'action': 'priority_fee',
                'priority_fee': fee,
            },
            success: function (response) {
                setTimeout(function(){
                    $(document.body).trigger('added_to_cart');
                }, 500);
            },
        });
    });
});
</script>
<?php
endif;
}

// Get Ajax request and saving to WC session
add_action( 'wp_ajax_priority_fee', 'priority_fee_ajax_receiver' );
add_action( 'wp_ajax_nopriv_priority_fee', 'priority_fee_ajax_receiver' );
function priority_fee_ajax_receiver() {
if ( isset($_POST['priority_fee']) ) {
    $priority_fee = $_POST['priority_fee'] ? true : false;
    // Set to a WC Session variable
    WC()->session->set('priority_fee', $priority_fee );

    echo $priority_fee ? '1' : '0';
    die();
}
}

// Add a custom calculated fee conditionally
add_action( 'woocommerce_cart_calculate_fees', 'set_priority_fee' );
function set_priority_fee( $cart ){
if ( is_admin() && ! defined('DOING_AJAX') )
    return;

if ( WC()->session->get('priority_fee') ) {
    $item_count  = $cart->get_cart_contents_count();
    $fee_label   = 'Discount';
    $fee_amount  = -2;
    $cart->add_fee( $fee_label, $fee_amount );
}
}

但是折扣没有出现。此外,我需要将折扣应用于小计,以便重新计算税款(代码不这样做,它应用税后折扣)。

如何在选中复选框时设置负费用(折扣),并且在计税前应用折扣?

编辑:好的,我通过更改最后一部分来处理税收:

// Set the discount
add_action( 'woocommerce_cart_calculate_fees', 'checkout_set_discount', 20, 1 );
function checkout_set_discount( $cart ) {
if ( ( is_admin() && ! defined('DOING_AJAX') )  )
    return;


$subtotal   = WC()->cart->get_subtotal();
$percentage = 1;
$discount   = $subtotal * $percentage / 100;

if( WC()->session->get('priority_fee') ) {
    $cart->add_fee( sprintf( __( 'Discount', 'woocommerce') ),  -$discount );
}
}

但这仅适用于基于百分比的折扣。我怎样才能改用固定折扣?

标签: phpwordpresswoocommerce

解决方案


推荐阅读