Add tax for dealers shipping to California
/**
* @snippet Add tax for dealers shipping to California
* @author André Le Comte
*/
function add_CA_cart_tax() {
global $woocommerce;
$taxable_states = array('CA');
$taxable_roles = array('customer');
$user_data = get_userdata( get_current_user_id() );
$user_roles = $user_data->roles;
if( in_array('customer', $user_roles) && in_array( WC()->customer->shipping_state, $taxable_states) ) {
$surcharge = 0.083 * WC()->cart->subtotal; // 8.3% tax based on shipping location
$woocommerce->cart->add_fee( __('California tax', 'woocommerce'), $surcharge );
}
}
add_action( 'woocommerce_cart_calculate_fees', 'add_CA_cart_tax' );
Add ship to store option for dealers with less-than-minimum fee
/**
* @snippet Add Ship to Store field at checkout if user is logged in as a dealer
* @author André Le Comte
**/
add_action( 'woocommerce_after_order_notes', 'custom_checkout_field' );
function custom_checkout_field( $checkout ) {
//Check if user is signed in as a dealer i.e., user's role is 'customer'
$user_is_dealer = is_user_a_dealer( customer );
//User is dealer so show additional fields
if ( $user_is_dealer === true ) {
echo '<div id="my_custom_checkout_field"><h3>' . __( 'Ship to store' ) . '</h3><p style="margin: 0 0 8px;">Ship to store</p>';
woocommerce_form_field( 'ship_to_store_checkbox', array(
'type' => 'checkbox',
'class' => array( 'ship-to-store-checkbox form-row-wide' ),
'label' => __( 'Yes' ),
), $checkout->get_value( 'ship_to_store_checkbox' ) );
echo '</div>';
}
}
/**
* @snippet Check if user is logged in as a dealer
* @author André Le Comte
*/
function is_user_a_dealer( $user_roles ) {
//Check to see if user is logged in as a dealer
global $woocommerce;
//flag not dealer
$user_is_dealer = false;
$user_data = get_userdata( get_current_user_id() );
$user_roles = $user_data->roles;
if( in_array('customer', $user_roles) ) {
//user is logged in as a dealer!
$user_is_dealer = true;
}
}
return $user_is_dealer;
}