This is an old revision of the document!
/** * @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' );
/** * @snippet Add field to checkout * @author André Le Comte **/ add_action( 'woocommerce_after_order_notes', 'custom_checkout_field' ); function custom_checkout_field( $checkout ) { //Check if user is a dealer $user_is_dealer = is_user_a_dealer( 117 ); //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( $product_id ) { //Check to see if user has product in cart global $woocommerce; //flag no book in cart $book_in_cart = false; foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) { $_product = $values['data']; if ( $_product->id === $product_id ) { //book is in cart! $book_in_cart = true; } } return $book_in_cart; }