PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label shipping-method. Show all posts
Showing posts with label shipping-method. Show all posts

Thursday, February 17, 2022

[FIXED] Disable only flat rate shipping method when free shipping is available in Woocommerce

 February 17, 2022     cart, php, shipping-method, woocommerce, wordpress     No comments   

Issue

I am using Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3 lightly changed answer code to hide all shipping methods except one. The only method I want showing is a rate from the "Woocommerce Advanced Shipping" plugin.

I am using the correct rate ID etc...

Everything works fine except when a customer tries to click that shipping method, it won't stay selected. It just jumps back to free shipping.

I have tried debugging and also tried the code with a native woocommerce flat rate ID and it showed up/able to select it just fine.

add_filter( 'woocommerce_package_rates', 'conditionally_hide_shipping_methods', 100, 2 );
function conditionally_hide_shipping_methods( $rates, $package ) {

    $flat_rates_express = array( '2588' );

    $free = $flat2 = array();
    foreach ( $rates as $rate_key => $rate ) {
        // Updated Here To 
        if ( in_array( $rate->id, $flat_rates_express ) )
            $flat2[ $rate_key ] = $rate;
        if ( 'free_shipping:12' === $rate->id )
            $free[ $rate_key ] = $rate;
    }
        return ! empty( $free ) ? array_merge( $free, $flat2 ) : $rates;
}

ID I want to Keep Shown: "2588" (Custom Shipping Rate From Plugin)

How can I disable the Flat rate shipping method when free shipping is available o and keep a custom shipping rate (from a plugin)?


Solution

As you have 3 shipping methods, 1 free shipping, 1 flat rate and 1 custom '2588', it's possible to hide the flat rate shipping method when free shipping is available instead:

add_filter( 'woocommerce_package_rates', 'free_shipping_disable_flat_rate', 1000, 2 );
function free_shipping_disable_flat_rate( $rates, $package ) {
    // Here your free shipping rate Id
    $free_shipping_rate_id = 'free_shipping:12';

    // When your Free shipping method is available
    if ( array_key_exists( $free_shipping_rate_id, $rates ) ) {
        // Loop through shipping methods rates
        foreach ( $rates as $rate_key => $rate ) {
            // Removing "Flat rate" shipping method
            if ( 'flat_rate' === $rate->method_id ){
                unset($rates[$rate_key]);
            }
        }
    }
    return $rates;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Refresh the shipping caches:

  1. This code is already saved on your function.php file.
  2. In a shipping zone settings, disable / save any shipping method, then enable back / save.
    You are done and you can test it.


Answered By - LoicTheAztec
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Tuesday, February 8, 2022

[FIXED] Reset previous chosen shipping method in Woocommerce checkout page

 February 08, 2022     checkout, php, shipping-method, woocommerce, wordpress     No comments   

Issue

Currently, I am clearing the default shipping selection method using this filter:

add_filter( 'woocommerce_shipping_chosen_method', '__return_false', 99);

However, this only clears it during the initial session of the customer. Once the customer chooses an option even once, it remembers the selection for the future.

I am trying to get the checkout to force the customer to pick a shipping option every time they visit the checkout even in the same session. Is it possible to run this filter every time the checkout page is loaded?


Solution

You can reset the last chosen shipping method using in checkout page (for logged in customers):

 delete_user_meta( get_current_user_id(), 'shipping_method' );

And also remove the chosen shipping method from session data:

WC()->session->__unset( 'chosen_shipping_methods' );

In a hooked function like:

add_action( 'template_redirect', 'reset_previous_chosen_shipping_method' );
function reset_previous_chosen_shipping_method() {
    if( is_checkout() && ! is_wc_endpoint_url() && is_user_logged_in() 
    && get_user_meta( get_current_user_id(), 'shipping_method', true ) ) {
        delete_user_meta( get_current_user_id(), 'shipping_method' );
        WC()->session->__unset( 'chosen_shipping_methods' );
    }
}

Or you can also set a default shipping method for everybody in checkout page:

add_action( 'template_redirect', 'reset_previous_chosen_shipping_method' );
function reset_previous_chosen_shipping_method() {
    if( is_checkout() && ! is_wc_endpoint_url() && is_user_logged_in() ) {
        WC()->session->set( 'chosen_shipping_methods', array('flat_rate:14') );
    }
}

To find out the shipping methods rate Ids to be used, you can inspect the shipping method radio buttons in cart or checkout pages, with your browser inspector like:

enter image description here

Code goes on function.php file of your active child theme (or active theme). It should works.



Answered By - LoicTheAztec
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

[FIXED] Sort WooCommerce shipping options by ascending cost with free shipping at the end

 February 08, 2022     cart, php, shipping-method, woocommerce, wordpress     No comments   

Issue

I'm trying to sort woocommerce shipping options. I want to sort from lowest to highest price, but I need the free shipping options last.

I'm working from WooCommerce: Sort Shipping Costs from Low to High

How do I put 0 last?

add_filter( 'woocommerce_package_rates' , 'businessbloomer_sort_shipping_methods', 10, 2 );
   
function businessbloomer_sort_shipping_methods( $rates, $package ) {
    
    if ( empty( $rates ) ) return;
   
    if ( ! is_array( $rates ) ) return;
    
    uasort( $rates, function ( $a, $b ) { 
        if ( $a == $b ) return 0;
        return ( $a->cost < $b->cost ) ? -1 : 1; 
    } );
    
    return $rates;
   
    // NOTE: BEFORE TESTING EMPTY YOUR CART
       
}

Solution

The following will sort shipping options by cost from lower to higher with zero cost and empty cost last.

add_filter( 'woocommerce_package_rates' , 'sort_shipping_method_by_cost_zero_empty_cost_last', 10, 2 );
function sort_shipping_method_by_cost_zero_empty_cost_last( $rates, $package ) {
    if ( empty( $rates ) || ! is_array( $rates ) ) return;

    // Sort shipping methods based on cost
    uasort( $rates, function ( $a, $b ) {
        if ( $a == $b ) return 0;
        return ( $a->cost < $b->cost ) ? -1 : 1;
    } );

    $free = $zero = []; // Initializing

    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ) {
        // For "free shipping" methods
        if ( 'free_shipping' === $rate->method_id ) {
            // set them on a separated array
            $free[$rate_key] = $rate;

            // Remove "Free shipping" method from $rates array
            unset($rates[$rate_key]);
        } 
        // For other shipping rates with zero cost
        elseif ( $rate->cost == 0 ) {
            // set them on a separated array
            $zero[$rate_key] = $rate;

            // Remove the current method from $rates array
            unset($rates[$rate_key]);
        }
    }

    // Merge zero cost and "free shipping" methods at the end if they exist
    return ! empty( $free ) || ! empty( $zero ) ? array_merge($rates, $zero, $free) : $rates;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Refresh the shipping caches:
1). This code is already saved on your function.php file.
2). In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.


Now the same thing ** with zero cost and empty cost first**:

add_filter( 'woocommerce_package_rates' , 'sort_shipping_method_by_cost_empty_zero_cost_first', 10, 2 );
function sort_shipping_method_by_cost_empty_zero_cost_first( $rates, $package ) {
    if ( empty( $rates ) || ! is_array( $rates ) ) return;

    // Sort shipping methods based on cost
    uasort( $rates, function ( $a, $b ) {
        if ( $a == $b ) return 0;
        return ( $a->cost < $b->cost ) ? -1 : 1;
    } );

    $free = $zero = []; // Initializing

    // Loop through shipping rates
    foreach ( $rates as $rate_key => $rate ) {
        // For "free shipping" methods
        if ( 'free_shipping' === $rate->method_id ) {
            // set them on a separated array
            $free[$rate_key] = $rate;

            // Remove "Free shipping" method from $rates array
            unset($rates[$rate_key]);
        } 
        // For other shipping rates with zero cost
        elseif ( $rate->cost == 0 ) {
            // set them on a separated array
            $zero[$rate_key] = $rate;

            // Remove the current method from $rates array
            unset($rates[$rate_key]);
        }
    }

    // Merge zero cost and "free shipping" methods at the end if they exist
    return ! empty( $free ) || ! empty( $zero ) ? array_merge($free, $zero, $rates) : $rates;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Refresh the shipping caches:
1). This code is already saved on your function.php file.
2). In a shipping zone settings, disable / save any shipping method, then enable back / save.
You are done and you can test it.



Answered By - LoicTheAztec
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Sunday, January 2, 2022

[FIXED] Changing shipped via text in WooCommerce orders and emails for specific shipping method

 January 02, 2022     orders, php, shipping-method, woocommerce, wordpress     No comments   

Issue

I am trying to change the shipped via in emails and in order information. I have a custom radio button with customer fields that are filled out only on the checkout page. Currently WooCommerce takes the label of the radio button but not the fields.

I would like to have the fields only in customer email and in the order information under woocommerce -> orders -> order#.

I can get it to change to text but lost on how to include the custom fields on a new line, which are carrier_name and carrier_number - these are the only fields that should show in both email and order information under WooCommerce.

The carrier_name and carrier_number are only available from the checkout page, not the cart page. When the 'Custom Carrier' is not selected it should print in email the correct shipping method, which I think it does by default but just wanted to include the requirement.

The third image shows the order information under woocommerce > orders > order#, this should show only carrier_name and carrier_number.

My code:

//adjusting emails to show custom carrier name and number
add_filter( 'woocommerce_order_shipping_to_display_shipped_via', 'wdo_remove_shipping_label_thnakyou_page_cart', 10, 2 );
function wdo_remove_shipping_label_thnakyou_page_cart($label, $method) {
    $shipping_label = get_post_meta($order->id, 'carrier_name') ;
    $shipping_label = 'Just Test!!!';
    return $shipping_label;
}

1 The Carrier Info I want to show, under Custom Carrier

1 The Carrier Info I want to show, under Custom Carrier

2 What I can currently get to send in email

2 What I can currently get to send in email

3 Order Information from woocommerce -> orders -> order#

3 Order Information from woocommerce -> orders -> order#

4 Order information without the code I put in, I changed the label using php code on checkout page different from cart page "Custom Carrier (Enter Details Next Page)" to checkout page "Custom Carrier":

4 Order information without the code I put in, I changed the label using php code on checkout page different from cart page  "Custom Carrier (Enter Details Next Page)" to  checkout page "Custom Carrier"


Solution

There are some mistakes in your code like for example wrong function arguments…
Try the following instead:

// Adjusting order and emails "shipping via" to show custom carrier name and number
add_filter( 'woocommerce_order_shipping_to_display_shipped_via', 'wdo_filter_order_shipping_to_display_shipped_via', 10, 2 );
function wdo_filter_order_shipping_to_display_shipped_via( $shipped_via, $order ) {
    $carrier_name = $order->get_meta('carrier_name'); // Get carrier name

    // Targeting orders with defined "carrier name" for "Custom Carrier" shipping method
    if ( $carrier_name ) {
        $carrier_number = $order->get_meta('carrier_number'); // get carrier number
        $shipped_via = '&nbsp;<small class="shipped_via">' . sprintf( __( 'via Custom Carrier: %s (%s)', 'woocommerce' ), $carrier_name, $carrier_number ) . '</small>';
    }
    return $shipped_via;
}

Code goes in functions.php file of the active child theme (or active theme). It should works.


Important note (update):

Change specific shipping method title on WooCommerce orders after checkout answer code replace this answer as it handle the change everywhere including on admin edit orders pages too and it's a much lightweight solution.



Answered By - LoicTheAztec
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing