mardi 13 août 2019

Add donation check box only to customers from CA

I am trying to set up a donation checkbox only for Canadian customers based on their input/past shipping address. The checkbox should display only if the shipping country is Canada. Is there any way to achieve this?

add_action( 'woocommerce_before_checkout_billing_form', 'git_box' ); 
    function gift_box ( $checkout ) {
             echo '<div id="message_fields">';
    woocommerce_form_field( 'add_gift_box', array(
        'type'          => 'checkbox',
        'class'         => array('add_gift_box form-row-wide'),

        'label'         => __('Add Donation to the site? ($2)'),
        'placeholder'   => __(''),
        ), $checkout->get_value( 'add_gift_box' ));
        echo '</div>';
}
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; 
    }

    if (isset($post_data['add_gift_box'])) {
         $extracost = 2.65;
        WC()->cart->add_fee( 'Site Donation:', $extracost );
    }

}

Aucun commentaire:

Enregistrer un commentaire