Woocommerce - Create free shipping for one item when using the UPS extension

I am using the UPS extension in woocommerce. I need to offer free shipping on some products, bypassing the UPS calculator. It seems that one good way to do this is to mark the item as "virtuaL", but then the item does not require delivery. I need to require shipping for this item. I was able to set up a validation page and make the delivery fields appear on virtual items, but then they are not saved in the actual orders.

Does anyone know how I can create a user-defined function or modify woocommerce files to make the verification process require shipping on a virtual item, and bypassing the UPS calculator all the time? (other items must be calculated for delivery)

+1
source share
4 answers

I had the same problem, the author of the plugin answered me that this is not possible, I need to write code for this.

+1
source

I just ran into this problem, maybe this will help someone. For all the products I need for free shipping, I made virtual products.

Then I went to the Woocommerce tab> "Delivery Options", check the box "Collect delivery address, even if it is not required", which is located in the "Delivery destination" section.

0
source

I also needed to do this and have a workable solution:

First go to functions.php in "Appearance"->"Editor"->"Theme Functions" and add the following code (I highly recommend using a " child theme " first, so you don’t change the theme functions.php ... or you can alternatively create a custom plugin ).

 /* Puts items with "free-shipping" shipping class into a different shipping package. */ function free_woocommerce_cart_shipping_packages( $packages ) { // Reset the packages $packages = array(); // Free items $freeshipping_items = array(); $regular_items = array(); // Sort free from regular (replace "free-shipping" below with the shipping class slug you actually use). foreach( WC()->cart->get_cart() as $item ) { if( $item['data']->needs_shipping() ) { if( $item['data']->get_shipping_class() == 'free-shipping' ) { $freeshipping_items[] = $item; } else { $regular_items[] = $item; } } } // Put inside packages: if( $freeshipping_items ) { $packages[] = array( 'ship_via' => array( 'flat_rate' ), 'contents' => $freeshipping_items, 'contents_cost' => array_sum(wp_list_pluck($freeshipping_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() ) ); } if( $regular_items ) { $packages[] = array( // 'ship_via' => array( 'usps' ), '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; } add_filter('woocommerce_cart_shipping_packages', 'free_woocommerce_cart_shipping_packages'); 

Secondly, go to "Products"->"Shipping Classes" and create a delivery class called " Free Shipping " with a slug called " free-shipping " (the bullet name is what we will bind our custom function to). Then the " Add New Shipping Class ".

Then in your "WooCommerce"->"Settings"->"Shipping"->"Flat Rate" enable the "WooCommerce"->"Settings"->"Shipping"->"Flat Rate" sending and in the " Shipping Class Costs " section put the value " 0.00 " next to the " Free Shipping " class, which we just created. In my case, I also have a " Calculation Type " set to " Per Class ". Then " Save changes ".

Then, finally, in "WooCommerce"->"Settings"->"Shipping"->"Shipping Options" in the " Shipping Methods " section, make sure that " Flat Rate " has a higher " Selection Priority " (in the column of this name) than any other delivery method you use and that it is the last on the list (you can drag to reorder using the menu icon to the left of each option). " Save changes ".

Then edit one of your products that you want to have free shipping, go to " Product Data"->"Shipping " and change the delivery class to " Free Shipping " (the new class you created). Then β€œ Update ” this product will save the changes. Then add it to your basket, and also add another item to the basket that has regular non-free delivery.

Test

You should now see that free delivery items receive their own β€œbatch" delivery line (" Shipping #1 " and " Shipping #2 ") separately from items that should still charge for shipping, even if you have some of them in the basket.

In addition, you can also try a full paid plugin called "Delivery" .

Or this free plugin may also work (no promises).

0
source

Another way that, in my opinion, can help you make the goods β€œfree” shipping (and the way I used in the past) is to create a coupon , which will depreciate the product (do not mark it as "virtual"), but not at a price.

So say, for example, that your product is $ 100, and delivery is $ 10 (total $ 110), then you create a coupon specifically for this product, which will give it a discount of $ 10 for your product and delivery will cost $ 100.

I assume that you already know how to create coupons in woocommerce (if you need to freshen up a bit, how this page will help) To make coupons applicable to certain products that you want to have free delivery, go to the "Restriction of use- > Products "and indicate the specific product that you want to have" free "(or, in this case, at a discount) shipping.

To save the user when entering this coupon code after checkout, you can add the following code to the .php functions:

 //Check for specific products then discount to adjust for 'free' shipping. function my_free_shipping(){ //variables. global $woocommerce; $coupon_code = 'free-shiping'; $products= array('1', '2'); //products (ID's) to have discounted shipping. //get the cart contents. $cart_item = $woocommerce->cart->get_cart(); //loop through the cart items looking for the products in $products. foreach ($cart_item as $key => $item){ //check if the cart items match to any of those in the array. if(in_array($item['product_id'], $products)){ //apply discount. $woocommerce->cart->add_discount(sanitize_text_field($coupon_code)); } } } add_action('init', 'my_free_shipping'); 

Please note that I have not tested this code with the UPS extension yet, but feel free to modify it and play with it.

I hope you find this post useful!

0
source

Source: https://habr.com/ru/post/1264347/


All Articles