• Hello,

    There is an issue that presents itself as someone updates their zip code on the cart page to calculate shipping costs.

    I have a CSV with 41K Zip Codes, Each Zip Code has 4 variables that affect pricing: S1, S2, S3, & N. Each Product has a weight (W).

    the formula to calculate shipping cost is $ Zip Code Shipping Price = (S1*(W^n))+(S2*W)+S3

    the following code calculates the shipping price following the above formula:

    add_filter( 'woocommerce_shipping_calculator_enable_country', '__return_false' );
    add_filter( 'woocommerce_shipping_calculator_enable_city', '__return_false' );
    add_filter( 'woocommerce_shipping_calculator_enable_state', '__return_false' );
    
    function my_custom_shipping_calculator_field() {
    	global $wpdb, $session;
        if ( ! session_id() ) {
            session_start();
        }
    	
    	$zip_code = $_REQUEST['calc_shipping_postcode'];
    	$_SESSION['zip_code_number'] = $zip_code;
    	$wpdb_prefix = $wpdb->prefix;
    	$wpdb_tablename = $wpdb_prefix.'prices_shipping';
    	$result = $wpdb->get_results("SELECT * FROM $wpdb_tablename WHERE zip_code = '$zip_code' LIMIT 1");
    	$s1 = $s2 = $s3 = $n = 0;
    	foreach($result as $item){
    		$s1 = $item->s1;
    		$s2 = $item->s2;
    		$s3 = $item->s3;
    		$n = $item->n;
    	}
    
    	
    	$sum_weight_shipping = 0;
        global $woocommerce;
    	$item_cart = $woocommerce->cart->get_cart();
    	foreach($item_cart as $item => $value){
    		$product_item = wc_get_product($value["data"]->get_id());
    		$product_item_weight =(float)$value["data"]->get_weight();
    		$product_item_quantity = (int)$value['quantity'];
    
    		$weight_shipping = $product_item_weight*$product_item_quantity;
    		$sum_weight_shipping = $sum_weight_shipping + $weight_shipping;
    	}
    
    	$item_price_shipping = ($s1*pow($sum_weight_shipping,$n)) + ($s2*$sum_weight_shipping) + $s3;
    	
    	add_action( 'woocommerce_cart_calculate_fees', function($cart) use($item_price_shipping){
    		WC()->cart->add_fee( __( 'Shipping Price', 'shipping_price' ) , $item_price_shipping , true);
    	});
    	
    	if($item_price_shipping != 0){
    		$_SESSION['item_price_shipping'] = $item_price_shipping;
    		
    	}
    	
    	if($zip_code != ""){
    		WC()->customer->set_shipping_postcode(wc_clean($zip_code));
    		WC()->customer->set_billing_postcode(wc_clean($zip_code)); 
    	}
    }
    add_action( 'woocommerce_calculated_shipping', 'my_custom_shipping_calculator_field' );
    
    /*	add_action('woocommerce_checkout_create_order', function($order, $data){
    		$item_price_shipping = 50;
    		$item_fee = new WC_Order_Item_Fee();
    		$item_fee->set_name('Shipping Price');
    		$item_fee->set_amount($item_price_shipping);
    		$item_fee->set_tax_class('');
    		$item_fee->set_tax_status('none');
    		$item_fee->set_total($item_price_shipping);
    		
    		$order->add_item($item_fee);
    		$order->calculate_totals();
    		$order->save();
    	});*/
    
    /*
    */
    
    add_action('woocommerce_before_checkout_form', 'custom_checkout_calculated_shipping', 1);
    function custom_checkout_calculated_shipping() {
    	global $session;
        if ( ! session_id() ) {
            session_start();
        }
        $zip_code_shipping = WC()->customer->get_shipping_postcode();
    	$zip_code_billing = WC()->customer->get_postcode();
    	$price_shipping = $_SESSION['item_price_shipping'];
    	$zip_code = $_SESSION['zip_code_number'];
    	
    	if(($zip_code_shipping == "" && $zip_code_billing == "" && $zip_code == "") &&  !isset($price_shipping)){
    		wp_redirect("/cart");
    		$_SESSION["zip_code"] = false;
    	}else{
    		$_SESSION["zip_code"] = true;
    	}
    }
    
    add_action( 'woocommerce_cart_calculate_fees', 'prefix_add_discount_line');
    function prefix_add_discount_line( $cart ) {
    	global $session;
        if ( ! session_id() ) {
            session_start();
        }
      if(isset($_SESSION['item_price_shipping'])){
    	   $item_price_shipping = $_SESSION['item_price_shipping'];
      }else{
    	  $item_price_shipping = 0;
      }	
     
      $cart->add_fee( __( 'Shipping Price', 'shipping_price' ) , $item_price_shipping );
    }
    
    add_action('woocommerce_before_cart_contents', 'zip_code_not_found_message');
    function zip_code_not_found_message() {
     	global $session;
        if ( ! session_id() ) {
            session_start();
        }
    	if($_SESSION["zip_code"] == false){
    		    $message='<div class="zip_code_not_found_message">';
    				$message.='<h2>Shipping Zip code is required</h2>';
    				$message.='<p align="justify">Please enter your zip code.</p>';
    			$message.='</div>';
    		    echo $message;
    	}
    	
    }
    
    function js_code_shipping() {
        ?>
        <script type="text/javascript">
           jQuery(document).ready(function($){
    		   $( document.body ).on( 'updated_cart_totals', function(){
    				$("button[name='calc_shipping']").click();
    		   });
    		   $( document.body ).on( 'added_to_cart', function(){
                    $("button[name='calc_shipping']").click();
               });
    		   
    		   const text_shipping = $(".woocommerce-shipping-destination strong").text().replace("OH","Zipcode:");
    		   $(".woocommerce-shipping-destination strong").text(text_shipping);
    		   $(".woocommerce-shipping-destination strong").css("display","inline-block");
    	   });
    		
        </script>
        <?php
    }
    add_action( 'wp_footer', 'js_code_shipping' );

    this code snippet causes the page to continuously make a call via ajax after the zipcode is input:

    here is what happens in the console:

    any advice on how to fix this? this is presenting on multiple browsers and devices.

    thank you in advance!

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • potrebbe essere un errore causato dal plugin di personalizzazione mi sembra che tu stia utilizzando ELEMENTOR

    Thread Starter rease

    (@rease)

    I disabled elementor and the problem still persists. In fact, I’ve disabled all plugins except the code snippet and woocommerce and the problem still exists.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Code Snippet Causing Multiple Ajax Calls – Flickering Images in WooCommerce cart’ is closed to new replies.