在Woocommerce中为动态$ cart-> add_fee添加复选框并删除

时间:2018-08-06 11:37:05

标签: ajax wordpress woocommerce

现在我正在使用一个复选框,并且已经完成了这样的工作。以前对于文本框,它工作正常。因为我只需要更改值。但是现在我必须在复选框启用和禁用时添加和删除。

它是第一次工作,以后不再工作。

add_action('woocommerce_checkout_after_customer_details', 'rx_wc_reward_points_check', 10, 0);
function rx_wc_reward_points_check() {
        global $woocommerce;
    $totalamnt = $woocommerce->cart->subtotal;
    $user_id = get_current_user_id();
    $user_credit_balance = get_user_meta($user_id, 'credit_balance', true);
    // for credit point discount
    $rx_minimum_amnt            = get_option('rx_minimum_amnt');
    $rx_credit_discount_percent = get_option('rx_credit_discount_percent');
    $used_credit_points = ($rx_credit_discount_percent/100)* $user_credit_balance;
    if ( $used_credit_points ) {
        $user_credit_balance = $user_credit_balance - $used_credit_points;
    }
    if ( $totalamnt >= $rx_minimum_amnt ) {
        $reward_points .= '<div class="reward-sec rx-discount credit-balance"><span class="woocommerce-input-wrapper"><h5 for="rx-credits-points" class="credit-label-tile"> Credit Points </h5><p> You have '.$user_credit_balance.' credit points in your account.</p><input type="checkbox" class="use-credit-points" id="used-credits" checked /><span id="rx-credits-points">' . $used_credit_points . '</span><label for="rx-credits-points" class="credit-label"> Credit Points are used </label></span> </div>';
    }
    echo $reward_points;

}
// Add a credit dynamic discount based on credit points
add_action('woocommerce_cart_calculate_fees', 'rx_creditpoints_discount', 20, 1);
function rx_creditpoints_discount($cart) {
    if (is_admin() && !defined('DOING_AJAX'))
        return;
    global $woocommerce;
    $totalamnt = $woocommerce->cart->subtotal;
    // Only for targeted shipping method
    if ( WC()->session->__isset('credit_fee_used') )
        $discountpercent = (float) WC()->session->get('credit_fee_used');



    if (isset($discountpercent) && $discountpercent == 'notusedpoint') {
        $cart->add_fee(__('Credit Point Discount', 'woocommerce'), 0 );
    }
    elseif( isset($discountpercent) && $discountpercent == 'usedpoint') {
        $user_id = get_current_user_id();
        $user_credit_points = get_user_meta($user_id, 'credit_balance', true);  

        $rx_credit_discount_percent = get_option('rx_credit_discount_percent');
        $used_credit_points = ($rx_credit_discount_percent/100)* $user_credit_points;

        $rx_credit_points_conv = get_option('rx_credit_points_conv');

        $rx_credit_amnt_discount = $used_credit_points / $rx_credit_points_conv;
        $cart->add_fee(__('Credit Point Discount', 'woocommerce'), - $rx_credit_amnt_discount);     
    }
}   


add_action('wp_footer', 'rx_wc_credit_points_script');
function rx_wc_credit_points_script() {
    // Only checkout page
    if (!is_checkout())
        return;
?>
   <script type="text/javascript">
    jQuery( function($){
        $('#used-credits').on('change', function() {
            var ischecked= $(this).is(':checked');
            if (!ischecked ) {
                var checked = "notusedpoint";
            }
            else {
                var checked = "usedpoint";
            }
            console.log(checked);
            $.ajax({
                type: "post",
                url:  wc_checkout_params.ajax_url,
                data: {
                     'action' : 'rx_wc_deduct_credit',
                     'creditpointsused' : checked
                },
                success: function(response) {
                    $('body').trigger('update_checkout');
                    console.log('response: '+response); // just for testing | TO BE REMOVED
                    $('rx-credits-points').val(' is applied.');
                    $('credit-balance').addClass('applied');


                },
                error: function(error){
                    console.log('error: '+error); // just for testing | TO BE REMOVED
                }
            });
        })
    })
    </script>
    <?php
}

// Wordpress Ajax code (set ajax data in Woocommerce session)
add_action('wp_ajax_rx_wc_deduct_credit', 'rx_wc_deduct_credit');
add_action('wp_ajax_nopriv_rx_wc_deduct_credit', 'rx_wc_deduct_credit');
function rx_wc_deduct_credit() {

    if (isset($_POST['creditpointsused'])) {
        WC()->session->set('credit_fee_used', esc_attr($_POST['creditpointsused']));
        echo true;
    }
    exit();
}

// Add a credit dynamic discount based on credit points
add_action('woocommerce_cart_calculate_fees', 'rx_creditpoints_discount', 20, 1);
function rx_creditpoints_discount($cart) {
    if (is_admin() && !defined('DOING_AJAX'))
        return;
    global $woocommerce;
    $totalamnt = $woocommerce->cart->subtotal;
    // Only for targeted shipping method
    if ( WC()->session->__isset('credit_fee_used') )
        $discountpercent = (float) WC()->session->get('credit_fee_used');



    if (isset($discountpercent) && $discountpercent == 'notusedpoint') {

        $cart->add_fee(__('Credit Point Discount', 'woocommerce'), 0);
    }
    elseif( isset($discountpercent) && $discountpercent == 'usedpoint') {
        $user_id = get_current_user_id();
        $user_credit_points = get_user_meta($user_id, 'credit_balance', true);  

        $rx_credit_discount_percent = get_option('rx_credit_discount_percent');
        $used_credit_points = ($rx_credit_discount_percent/100)* $user_credit_points;

        $rx_credit_points_conv = get_option('rx_credit_points_conv');

        $rx_credit_amnt_discount = $used_credit_points / $rx_credit_points_conv;
        $cart->add_fee(__('Credit Point Discount', 'woocommerce'), - $rx_credit_amnt_discount);     
    }
}

它是第一次工作。它转换为0(“信用点折扣”)。但第二次不工作。为什么我不明白。谢谢您的帮助。

0 个答案:

没有答案