如何从Wordpress中复选框操作的总数中减去金额

时间:2018-11-28 10:15:21

标签: wordpress

我已经创建了用于捐款的复选框字段,单击复选框将25卢比添加到总计中,但是如果取消选中,则不会在按单词的结帐页面上从总计中减去25卢比。

add_action('woocommerce_review_order_before_order_total','add_donation_checkbox');


function add_donation_checkbox(){


    echo "<input type='checkbox' name='donation' id='donation' checked /> Donation";

    ?>


    <script type="text/javascript">
        (function($){
            //$(".fee").hide();
            $( '#donation' ).on( 'change',  function() {

                if($("#donation").is(':checked')){
                        $(".fee").show();
                        //alert('checked');// checked
                        $('document.body').trigger('update_checkout');
                }else 
                  {
                        <?php $order = new WC_Order(); $total = $order->get_total(); ?>
                        <?php  $new_total = $total - 25; $order->set_total( $new_total );?>
                        $(".fee").hide();
                        //alert('unchecked');// unchecked

                        //$('body').trigger('update_checkout');
                        $('document.body').trigger('update_checkout');
                }       
            });
        })(jQuery);
    </script>
    <?php
}

add_action('woocommerce_cart_totals_after_shipping','checked_donation');


function checked_donation(){


    echo "<input type='checkbox' name='donation' id='donation' /> Donation";


}


// Add tax for Swiss country
add_action( 'woocommerce_cart_calculate_fees','custom_tax_surcharge_for_swiss', 10, 1 );
function custom_tax_surcharge_for_swiss($cart) {


    if ( is_admin() && ! defined('DOING_AJAX') ) return;

    $donation = 25;
    # $taxes = array_sum( $cart->taxes ); // <=== This is not used in your function


    // Add the fee (tax third argument disabled: false)

    $cart->add_fee( __( '#AlshCares Contribution INR 25 ', 'woocommerce'), $donation, false );

}

0 个答案:

没有答案