• Resolved Dakid

    (@myiah)


    Hi . so I need help with some code that i wrote in order to accomplish these task.
    1. Free shipping for threads shipping class when that particular class is over 25.00 in the cart.

    I no i had to create a package and have it add up the sub total for that shipping class and if it is over 25.00 then to apply the free shipping to those products but keep the other products in the cart at flat rate shipping.

    So that works!

    the problem is when i remove some of the threads and make sure it does not equally 25.00 it seems to break because i think the threads are not part of the cart anymore therefore they are not being accounted for in the cart if the threads are less then 25.00

    hope that makes sense

    i pasted my code.

    function threads_woocommerce_cart_shipping_packages( $packages ) {
        // Reset the packages
        $packages = array();
      
        // threads items
        $threads_items = array();
        $regular_items = array();
        
        // Sort threads from regular
        foreach ( WC()->cart->get_cart() as $item ) {
            if ( $item['data']->needs_shipping() ) {
                if ( $item['data']->get_shipping_class() == 'threads' ) {
                    $threads_items[] = $item;
                } else {
                    $regular_items[] = $item;
                }
            }
        }
        
       
    
          $threads_shipping   = array_sum( wp_list_pluck( $threads_items, 'line_total' ) );
    
    if(($threads_shipping) > 24.99) {
    	  
    	
    		
    		   $packages[] = array(
    		    'ship_via' => array( 'free_shipping' ),
    
    		             
    		    'contents' => $threads_items,
                'contents_cost' => array_sum( wp_list_pluck( $threads_items, 'line_total' ) ),
                'applied_coupons' => WC()->cart->applied_coupons,
                'destination' => array(
                    'country' => WC()->customer->get_shipping_country(),
                    'state' => WC()->customer->get_shipping_state(),
                    'postcode' => WC()->customer->get_shipping_postcode(),
                    'city' => WC()->customer->get_shipping_city(),
                    'address' => WC()->customer->get_shipping_address(),
                    'address_2' => WC()->customer->get_shipping_address_2()
    			  
    			  
    			  
                )
    
            );
    		
            } else {
    		
    
    		 if ( empty( $packages[0]['contents'] ) ) {
                    unset( $packages[0] );
                }
    		
    	  }
      
      
      
        if ( $regular_items ) {
            $packages[] = array(
    		  	'ship_via' => array( 'flat_rate' ),
    		  	
                'contents' => $regular_items,
                'contents_cost' => array_sum( wp_list_pluck( $regular_items, 'line_total' ) ),
                'applied_coupons' => WC()->cart->applied_coupons,
                'destination' => array(
                    'country' => WC()->customer->get_shipping_country(),
                    'state' => WC()->customer->get_shipping_state(),
                    'postcode' => WC()->customer->get_shipping_postcode(),
                    'city' => WC()->customer->get_shipping_city(),
                    'address' => WC()->customer->get_shipping_address(),
                    'address_2' => WC()->customer->get_shipping_address_2()
                )
            );
    
            
        }    
        
        return $packages;
      
      
    }
    
      // End of If statement for the whole threads category for free shipping
    
    add_filter( 'woocommerce_cart_shipping_packages', 'threads_woocommerce_cart_shipping_packages' );
    
    

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Dakid

    (@myiah)

    so the shipping is not being calculated or updated when you remove a threads item or add another product

    Plugin Support Ross V. a11n

    (@rossviviano)

    Automattic Happiness Engineer

    Hi there,

    Truthfully, I’d just look into the Table Rate Shipping extension if I were you, as it can achieve what you’re looking to do.

    I’m not able to assist with troubleshooting that custom code, but I’ll leave this thread open for a bit to see if anyone else wants to chime in.

    Best,

    Ross

    Thread Starter Dakid

    (@myiah)

    Thank you. I got it resolved. Thanks for your reply. I Greatly appreciate it.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Free Shipping on certain products’ is closed to new replies.