Sunday, June 19, 2022
HomeWordPress Developmentplugins - Customized subject for transport methodology in Woocommerce

plugins – Customized subject for transport methodology in Woocommerce


I want so as to add {custom} (and required) textual content subject for particular transport methodology in Woocommerce, that will likely be validated and will likely be seen in admin order pages and emails. It must be seen in cart AND in checkout web page.

I’ve made my very own little customization primarily based on reply from: https://stackoverflow.com/a/63195406/2375074 – it is mainly what i need, however the resolution is for choose enter subject and i would like easy textual content subject.

The issue is that the validation from my code does not work correctly(it all the time reveals empty error) and i’ve no details about that subject in admin pages or emails.

I might actually respect if somebody might level me to proper route with my code.

// Customized operate that deal with your settings
operate carrier_settings(){
    return array(
        'targeted_methods' => array('flat_rate:34'), // Your focused transport methodology(s) on this array
        'field_id'         => 'torder', // Subject Id
        'field_type'       => 'textual content', // Subject sort
        'field_label'      => '', // Depart empty worth if the primary possibility has a textual content (see under).
        'label_name'       => __("Dołączyć do zamówienia","woocommerce"), // for validation and as meta key for orders
    );
}

// Show the {custom} checkout subject
operate carrier_company_custom_select_field( $methodology, $index ) {
    extract( carrier_settings() ); // Load settings and convert them in variables

    $chosen  = WC()->session->get('chosen_shipping_methods'); // The chosen strategies
    $worth   = WC()->session->get($field_id);
    $worth   = WC()->session->__isset($field_id) ? $worth : WC()->checkout->get_value('_'.$field_id);
    $choices = array(); // Initializing

    if( ! empty($chosen) && $method->id === $chosen[$index] && in_array($method->id, $targeted_methods)  ) {
        echo '<div class="custom-carrier">';

        woocommerce_form_field( $field_id, array(
            'sort'     => $field_type,
            'label'    => 'Do zamówienia', // Not required if the primary possibility has a textual content.
            'class'    => array('form-row-wide ' . $field_id . '-' . $field_type ),
            'required' => true,
            'placeholder' => 'Podaj numer zamówienia',
        ), $worth );

        echo '</div>';
    }
}
add_action( 'woocommerce_after_shipping_rate', 'carrier_company_custom_select_field', 20, 2 );

// jQuery code (shopper aspect) - Ajax sender
operate carrier_company_script_js() {

    if( is_cart() || ( is_checkout() && ! is_wc_endpoint_url() ) ):
        
    extract( carrier_settings() );
    $js_variable = is_cart() ? 'wc_cart_params' : 'wc_checkout_params';

    ?>

    <script sort="textual content/javascript">
    jQuery( operate($){
        if (typeof <?php echo $js_variable; ?> === 'undefined')
            return false;

        $(doc.physique).on( 'enter', '#<?php echo $field_id; ?>', operate(){
            var worth = $(this).val();
            $.ajax({
                sort: 'POST',
                url: <?php echo $js_variable; ?>.ajax_url,
                information: {
                    'motion': 'toorderid',
                    'worth': worth
                },
            });
        });
    });
    </script>

    <?php
    endif;
}
add_action( 'wp_footer', 'carrier_company_script_js' );

// The WordPress Ajax PHP receiver
operate set_carrier_company_name() {
    if ( !empty($_POST['value']) ){

        extract( carrier_settings() );

        if( empty($_POST['value']) ) {
            $worth = 0;
            $label="Brak";
        } else {
            $worth = $label = esc_attr( $_POST['value'] );
        }

        WC()->session->set( $field_id, $worth );
        echo $label;
        die();
    }
}
add_action( 'wp_ajax_torder', 'set_carrier_company_name' );
add_action( 'wp_ajax_nopriv_torder', 'set_carrier_company_name' );

// Conditional operate for validation
operate has_carrier_field(){
    $settings = carrier_settings();
    return array_intersect(WC()->session->get( 'chosen_shipping_methods' ), $settings['targeted_methods']);
}

// Validate the {custom} choice subject
operate carrier_company_checkout_validation() {

    extract( carrier_settings() );

    if( has_carrier_field() && empty( $_POST[$field_id] ) )
        wc_add_notice(
            sprintf( __("Proszę wpisać odpowiedni numer zamówienia","woocommerce"),
            '<robust>' . $label_name . '</robust>'
        ), "error" );
}
add_action('woocommerce_checkout_process', 'carrier_company_checkout_validation');

// Save {custom} subject as order meta information
operate save_carrier_company_as_order_meta( $order ) {

    extract( carrier_settings() );

    if( has_carrier_field() && ! empty( $_POST[$field_id] ) ) {
        $order->update_meta_data( '_'.$field_id );
        WC()->session->__unset( $field_id ); // take away session variable
    }
}
add_action( 'woocommerce_checkout_create_order', 'save_carrier_company_as_order_meta', 30, 1 );

// Show {custom} subject in admin order pages
operate admin_order_display_carrier_company( $order ) {

    extract( carrier_settings() );
    $service = $order->get_meta( '_'.$field_id );

    if( ! empty($service) ) {
        echo '<p><robust>' . $label_name . '</robust>: ' . $service . '</p>';
    }
}
add_action( 'woocommerce_admin_order_data_after_shipping_address', 'admin_order_display_carrier_company', 30, 1 );

// Show service firm after transport line all over the place (orders and emails)
operate display_carrier_company_on_order_item_totals( $total_rows, $order, $tax_display ){

    extract( carrier_settings() );
    $service = $order->get_meta( '_'.$field_id ); // Get service firm

    if( ! empty($service) ) {
        $new_total_rows = [];

        // Loop via order complete rows
        foreach( $total_rows as $key => $values ) {
            $new_total_rows[$key] = $values;
            
            // Inserting the service firm below transport methodology
            if( $key === 'transport' ) {
                $new_total_rows[$field_id] = array(
                    'label' => $label_name,
                    'worth' => $service,
                );
            }
        }
        return $new_total_rows;
    }
    return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'display_carrier_company_on_order_item_totals', 1000, 3 );

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments