This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
|
start:website:source [2016/10/12 08:47] andre |
start:website:source [2016/10/12 10:52] (current) andre [Add ship to store option for dealers with less-than-minimum fee] |
||
|---|---|---|---|
| Line 22: | Line 22: | ||
| <code> | <code> | ||
| /** | /** | ||
| - | * @snippet Add field to checkout | + | * @snippet Add Ship to Store field at checkout if user is logged in as a dealer |
| * @author André Le Comte | * @author André Le Comte | ||
| **/ | **/ | ||
| Line 29: | Line 29: | ||
| function custom_checkout_field( $checkout ) { | function custom_checkout_field( $checkout ) { | ||
| - | //Check if user is a dealer | + | //Check if user is signed in as a dealer i.e., user's role is 'customer' |
| - | $user_is_dealer = is_conditional_product_in_cart( 117 ); | + | $user_is_dealer = is_user_a_dealer( customer ); |
| //User is dealer so show additional fields | //User is dealer so show additional fields | ||
| Line 41: | Line 41: | ||
| 'label' => __( 'Yes' ), | 'label' => __( 'Yes' ), | ||
| ), $checkout->get_value( 'ship_to_store_checkbox' ) ); | ), $checkout->get_value( 'ship_to_store_checkbox' ) ); | ||
| - | |||
| - | woocommerce_form_field( 'ship_to_store_textbox', array( | ||
| - | 'type' => 'text', | ||
| - | 'class' => array( 'inscription-text form-row-wide' ), | ||
| - | 'label' => __( 'To whom should the inscription be made?' ), | ||
| - | ), $checkout->get_value( 'inscription_textbox' ) ); | ||
| echo '</div>'; | echo '</div>'; | ||
| Line 54: | Line 48: | ||
| /** | /** | ||
| - | * Check if Conditional Product is In cart | + | * @snippet Check if user is logged in as a dealer |
| - | * | + | * @author André Le Comte |
| - | * @param $product_id | + | |
| - | * | + | |
| - | * @return bool | + | |
| */ | */ | ||
| - | function is_conditional_product_in_cart( $product_id ) { | + | function is_user_a_dealer( $user_roles ) { |
| - | //Check to see if user has product in cart | + | //Check to see if user is logged in as a dealer |
| global $woocommerce; | global $woocommerce; | ||
| - | //flag no book in cart | + | //flag not dealer |
| - | $book_in_cart = false; | + | $user_is_dealer = false; |
| - | + | ||
| - | foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) { | + | |
| - | $_product = $values['data']; | + | |
| - | if ( $_product->id === $product_id ) { | + | $user_data = get_userdata( get_current_user_id() ); |
| - | //book is in cart! | + | $user_roles = $user_data->roles; |
| - | $book_in_cart = true; | + | if( in_array('customer', $user_roles) ) { |
| + | //user is logged in as a dealer! | ||
| + | $user_is_dealer = true; | ||
| } | } | ||
| } | } | ||
| - | return $book_in_cart; | + | return $user_is_dealer; |
| } | } | ||
| </code> | </code> | ||