Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter junkpad92

    (@junkpad92)

    Here is my current code:

    /**
     * Add the field to the checkout
     */
    add_action( 'woocommerce_after_order_notes', 'datepicker_checkout_field' );
    
    function datepicker_checkout_field( $checkout ) {
    
        echo '<div id="datepicker_checkout_field"><h2>' . __('Lieferdatum') . '</h2>';
    
        woocommerce_form_field( 'order_datepicker', array(
            'type'          => 'text',
            'required'  	=> true,
            'class'         => array('my-field-class form-row-wide'),
            'id'			=> 'order_datepicker',
            'label'         => __('W?hlen Sie Ihr Lieferdatum aus'),
            'placeholder'   => __('Datum ausw?hlen'),
            'custom_attributes'	=> array( 'readonly' => 'true' )
        ), $checkout->get_value( 'order_datepicker' ));
    
        echo '</div>';
    
    }
    
    /**
     * Process the checkout
     */
    add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
    
    function my_custom_checkout_field_process() {
        // Check if set, if its not set add an error.
        if ( ! $_POST['order_datepicker'] ) {
            wc_add_notice( __( 'Bitte geben Sie ein Lieferdatum an.' ), 'error' );
        }
    }
    
    /**
     * Update the order meta with field value
     */
    add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
    
    function my_custom_checkout_field_update_order_meta( $order_id ) {
        if ( ! empty( $_POST['order_datepicker'] ) ) {
            update_post_meta( $order_id, 'Lieferdatum', sanitize_text_field( $_POST['order_datepicker'] ) );
        }
    }
    
    /**
     * Display field value on the order edit page
     */
    add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
    
    function my_custom_checkout_field_display_admin_order_meta($order){
        echo '<p><strong>'.__('Lieferdatum').':</strong> ' . get_post_meta( $order->id, 'Lieferdatum', true ) . '</p>';
    }
    
    /**
    * Add the field to order emails
    **/
    add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
    
    function my_custom_checkout_field_order_meta_keys( $keys )
    {
           $keys[] = "Lieferdatum";
           return $keys;
    }
    
    /* ----------- MINIMUM ORDER AMOUNT ------------- */
    
    add_action( 'woocommerce_checkout_process', 'wc_minimum_order_amount' );
    add_action( 'woocommerce_before_cart' , 'wc_minimum_order_amount' );
    
    function wc_minimum_order_amount() {
        // Which shipping method is chosen?
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    		return;
    
        $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    	$chosen_shipping = $chosen_methods[0]; 
    
        // Set this variable to specify a minimum order value
        if( $chosen_shipping == 'local_pickup' ) {
        	$minimum = 45;
        } else {
    	    $minimum = 150;
        }
    
        if ( WC()->cart->total < $minimum ) {
    
            if( is_cart() ) {
    
                wc_print_notice(
                    sprintf( 'Der Mindestbestellwert betr?gt %s. Derzeit betr?gt Ihr Bestellwert %s.' ,
                        wc_price( $minimum ),
                        wc_price( WC()->cart->total )
                    ), 'error'
                );
    
            } else {
    
                wc_add_notice(
                    sprintf( 'Der Mindestbestellwert betr?gt %s. Derzeit betr?gt Ihr Bestellwert %s.' ,
                        wc_price( $minimum ),
                        wc_price( WC()->cart->total )
                    ), 'error'
                );
    
            }
        }
    
    }
    
    /* ----------------------------------------------- */
    
    /* ---------- CUSTOM SURCHARGE ----------- */
    
    function woocommerce_custom_surcharge() {
      global $woocommerce;
    
    	if ( is_admin() && ! defined( 'DOING_AJAX' ) )
    		return;
    
    	$country = array('AT', 'US');
    	$percentage = 0.1;
    
    	if ( in_array( $woocommerce->customer->get_shipping_country(), $country ) ) :
    		$surcharge = ( $woocommerce->cart->cart_contents_total + $woocommerce->cart->shipping_total ) * $percentage;
    		$woocommerce->cart->add_fee( 'Feiertagszuschlag 10 %', $surcharge, true, 'standard' );
    	endif;
    
    }
    
    add_action( 'woocommerce_cart_calculate_fees','woocommerce_custom_surcharge' );
    
    /* --------------------------------------- */

    Thank you very much for helping!

    Did you ever get this answered?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add fee dynamically depending on textfield’ is closed to new replies.