2016-06-15 59 views
5

Ho recentemente aggiornato a WooCommerce 2.6 nel mio negozio e hanno aggiornato il loro sistema di spedizione. Prima ho usato questo per nascondere l'opzione di spedizione pagato quando un valore di ordine specifico è stato raggiunto e la spedizione gratuita è stata innescata:WooCommerce 2.6 - Nascondere la spedizione a pagamento quando la spedizione gratuita viene attivata raggiungendo l'importo specifico

/** 
* woocommerce_package_rates is a 2.1+ hook 
*/ 
add_filter('woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2); 

/** 
* Hide shipping rates when free shipping is available 
* 
* @param array $rates Array of rates found for the package 
* @param array $package The package array/object being shipped 
* @return array of modified rates 
*/ 
function hide_shipping_when_free_is_available($rates, $package) { 

    // Only modify rates if free_shipping is present 
    if (isset($rates['free_shipping'])) { 

     // To unset a single rate/method, do the following. This example unsets flat_rate shipping 
     unset($rates['flat_rate']); 

     // To unset all methods except for free_shipping, do the following 
     $free_shipping   = $rates['free_shipping']; 
     $rates     = array(); 
     $rates['free_shipping'] = $free_shipping; 
    } 

    return $rates; 
} 

Anche se questo non funziona più. Ho bisogno di una nuova correzione e non sono davvero in codice.

Qualcuno ha una soluzione a questo?

La soluzione di cui sopra è stato da questo sito:
Hide other shipping methods when FREE SHIPPING is available

Sto indovinando che alcuni parametri sono cambiati da quando hanno aggiornato i metodi di trasporto.

Spero che qualcuno là fuori sappia come risolvere il problema.

risposta

3

Prova a sostituire lo snippet esistente con quello riportato di seguito. I dettagli di questo frammento sono descritti in this article. Fammi sapere se questo può essere migliorato.

add_filter('woocommerce_package_rates', 'xa_hide_shipping_rates_when_free_is_available', 10, 2); 

function xa_hide_shipping_rates_when_free_is_available($rates, $package) 
{ 
    global $woocommerce; 
    $version = "2.6"; 
    if (version_compare($woocommerce->version, $version, ">=")) { 
     foreach($rates as $key => $value) { 
      $key_part = explode(":", $key); 
      $method_title = $key_part[0]; 
      if ('free_shipping' == $method_title) { 
       $free_shipping = $rates[$key]; 
       // Unset all rates. 
       $rates = array(); 
       // Restore free shipping rate. 
       $rates[$key] = $free_shipping; 
       return $rates; 
      } 
     } 
    } 
    else { 
     if (isset($rates['free_shipping'])) { 
      // Below code is for unsetting single shipping method/option. 
      // unset($rates['flat_rate']); 
      $free_shipping = $rates['free_shipping']; 
      // Unset all rates. 
      $rates = array(); 
      // Restore free shipping rate. 
      $rates['free_shipping'] = $free_shipping; 
     } 
    } 

    return $rates; 
} 
+0

è questo codice di lavoro? Perché non riesco a vedere i cambiamenti nel mio woo 2.6.7 – vladimir

0

Ok il seguente codice consentirà di prelievo locale con trasporto libero:

// ##### WOOCOMMERCE - HIDE OTHER SHIPPING METHODS WHEN FREE SHIPPING IS AVAILABLE ##### 
add_filter('woocommerce_package_rates', 'hide_shipping_when_free_is_available', 10, 2); 
function hide_shipping_when_free_is_available($rates, $package) { 
    $free_yn = 0; 
    $pickup_yn = 0; 
    foreach($rates as $key => $value) { 
     $key_part = explode(":", $key); 
     $method_title = $key_part[0]; 
     if ('free_shipping' == $method_title) { 
      // check if free shipping rate exists 
      $free_yn = 1; 
      $free_shipping = $rates[$key]; 
      $free_key = $key; 
     } 
     if ('local_pickup' == $method_title) { 
      // check if local pickup rate exists 
      $pickup_yn = 1; 
      $local_pickup = $rates[$key]; 
      $pickup_key = $key; 
     } 
    } 
    if ($free_yn == 1) { 
     // Unset all rates. 
     $rates = array(); 
     // Restore free shipping rate. 
     $rates[$free_key] = $free_shipping; 
     if ($pickup_yn == 1) { 
      // Restore local pickup rate. 
      $rates[$pickup_key] = $local_pickup; 
     } 
     return $rates; 
    } 
    return $rates; 
} 
0

condizione che siano stati rimossi i metodi di spedizione legacy (metodi di spedizione devono essere impostate utilizzando nuove zone marittime), è possibile utilizzare il seguente snippet per rimuovere tutti gli altri metodi di spedizione quando è disponibile la spedizione gratuita. (WooCommerce 2.6+):

/** 
    * Hide shipping rates when free shipping is available. 
    * Updated to support WooCommerce 2.6 Shipping Zones. 
    * 
    * @param array $rates Array of rates found for the package. 
    * @return array 
    */ 
    function my_hide_shipping_when_free_is_available($rates) { 
    $free = array(); 
    foreach ($rates as $rate_id => $rate) { 
     if ('free_shipping' === $rate->method_id) { 
     $free[ $rate_id ] = $rate; 
     break; 
     } 
    } 
    return ! empty($free) ? $free : $rates; 
    } 
    add_filter('woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100); 

Dal updated docs