Support » Plugin: WooCommerce » Change shipping cost based on checkout choice

  • Resolved jonofat

    (@jonofat)


    I have a plugin which is creating a local storage entry and when you change the input choice it changes the local storage value. I want to adjust my shipping cost based on this but the code I have isn’t doing anything. I outputted the local storage value elsewhere test I was actually getting it with php and it is correct. But in this context it doesn’t work.

    add_filter( 'woocommerce_package_rates', 'woocommerce_package_rates' );
    function woocommerce_package_rates( $rates ) {
        $myvar = "<script>document.write(localStorage.getItem('delivery date'))</script>";
        if ( $myvar == '29-6-2022') {
            $test = 100;
        } else {
            $test = 5;
        }
    	foreach($rates as $key => $rate ) {
    		$rates[$key]->cost = $rates[$key]->cost + $test;
    	}
    	return $rates;
    }
    • This topic was modified 1 year, 9 months ago by jonofat.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hi @jonofat

    I have a plugin which is creating a local storage entry

    May I know which plugin are you using? It would be best if you could share a copy of your site’s System Status. You can find it via WooCommerce > Status. Select “Get system report” and then “Copy for support”. Once you’ve done that, paste it here in your response.

    Thread Starter jonofat

    (@jonofat)

    Hi there, I am using this plugin which saves the date to local storage when you select one from the date picker. Is there any particular info you want from the status info? There is a lot and I don’t want to clog up this post so if you could narrow down what you are looking for I can paste that here.

    Hi @jonofat

    Thanks for letting us know you’re using the Order Delivery Date for WooCommerce plugin.

    As we do not support third-party plugins on these forums, therefore I recommend contacting the plugin’s support team directly who may help you set the best settings for their plugin. You can open a support request here: https://wordpress.org/support/plugin/order-delivery-date-for-woocommerce/

    Please be informed that this particular forum is for questions that are directly related to the features and functionality of the core WooCommerce plugin. While we’re experts on our own products, third-party plugins are best supported by their own authors/developers.

    Thanks

    Thread Starter jonofat

    (@jonofat)

    Fair enough, could you possibly help with the below then since it is just WooCommerce with no plugin?

    I have created a custom input on the checkout page and when I change it and click outside of it the shipping should update. It only works when sometimes and is a bit erratic. Is there anything obviously wrong here or something I can do to make it work consistently?

    add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
    
    function my_custom_checkout_field( $checkout ) {
    
        echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';
    
        woocommerce_form_field( 'my_field_name', array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Fill in this field'),
            'placeholder'   => __('Enter something'),
            ), $checkout->get_value( 'my_field_name' ));
    
        echo '</div>';
    
    }
    
    add_filter( 'woocommerce_package_rates', 'update_shipping_cost_based_on_custom_field', 10, 2 );
    function update_shipping_cost_based_on_custom_field( $rates, $package ) {
    
        if ( ! $_POST || is_admin() || ! is_ajax() ) {
            return;
        }
    
        if ( isset( $_POST['post_data'] ) ) {
            parse_str( $_POST['post_data'], $post_data );
        } else {
            $post_data = $_POST;
        }
        
        $new_cost = 0;
    
        if ( isset( $post_data['my_field_name'] ) && ! empty( $post_data['my_field_name'] ) ) {
            $new_cost = (float) str_replace( ',', '.', $post_data['my_field_name'] );
        }
      
    
        foreach( $rates as $rate_key => $rate ) {
             $rates[$rate_key]->cost = $new_cost;
        }
        return $rates;
    }
    
    add_action( 'wp_footer', 'update_checkout' );
    function update_checkout() {
    
        if ( ! is_checkout() ) {
            return;
        }
    
        ?>
        <script type="text/javascript">
        jQuery( document ).ready(function( $ ) {
            jQuery('#my_field_name').change(function(){
                jQuery('body').trigger('update_checkout');
            });
        });
        </script>
        <?php
    }
    • This reply was modified 1 year, 9 months ago by jonofat.

    Hi @jonofat

    I have created a custom input on the checkout page and when I change it and click outside of it the shipping should update. It only works when sometimes and is a bit erratic.

    I’m sorry to disappoint you, but kindly be informed that support for custom coding is beyond the scope of support we are able to provide in this forum. This particular forum is for questions that are related to the WooCommerce core features.

    For help with custom code we recommend the WooCommerce Developer Resources Portal.

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there, as well.

    Lastly, for direct assistance with code customizations, we recommend consulting with the WooCommerce Customizations Partners. https://woocommerce.com/customizations/

    Nevertheless, I’m going to leave this thread open for a bit to see if anyone is able to chime in to help you out here.

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Change shipping cost based on checkout choice’ is closed to new replies.