Plugin Directory

Changeset 2800116


Ignore:
Timestamp:
10/17/2022 03:56:29 PM (3 years ago)
Author:
shiptimizeplugins
Message:

change the way we check if method is available

Location:
shiptimize-for-woocommerce/trunk/includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • shiptimize-for-woocommerce/trunk/includes/admin/class-shiptimize-shipping.php

    r2800084 r2800116  
    771771         
    772772          add_action( 'woocommerce_update_options_shipping_' . \$this->id, array( \$this, 'process_admin_options' ) );
    773 
    774           add_filter( 'woocommerce_shipping_' . \$this->id . '_is_available', array(\$this, 'method_available'), 10, 3 );
     773 
    775774        }
    776 
    777         public function method_available ( \$available, \$package, \$shipping_method ) {
    778           \$opt_exclude_classes = explode( ',', \$this->get_instance_option('excludeclasses') );
    779 
    780           if ( !\$available || empty( \$opt_exclude_classes ) ) {
    781             return \$available;
    782           }
    783 
    784           \$contains_classes_to_exclude = false;
    785           WooShiptimize::log(\$shipping_method->get_title());
    786           // Check if the package contains products that have at least one of the selected classes
    787           foreach ( \$package['contents'] as \$key => \$item ) {
    788              
    789             foreach ( \$opt_exclude_classes  as \$c2exclude ) {
    790               \$terms = get_the_terms( \$item['product_id'], 'product_shipping_class' );
    791               if ( \$terms ) {
    792                 foreach(\$terms as \$term) {
    793                   \$contains_classes_to_exclude |=  \$term->term_id == \$c2exclude ; 
    794                 }
    795               }
    796             }
    797           }
    798          
    799           WooShiptimize::log(\"contains class \" . ( \$contains_classes_to_exclude ? 1 : 0 ));
    800 
    801           return !\$contains_classes_to_exclude;
    802         }
    803775
    804776        public function validate_excludeclasses_field( \$key , \$value ) {
     
    10411013        }
    10421014
    1043         public function method_available ( \$available, \$package, \$shipping_method ) {
    1044           \$opt_exclude_classes = explode( ',', \$this->get_instance_option('excludeclasses') );
    1045 
    1046           if ( !\$available || empty( \$opt_exclude_classes ) ) {
    1047             return \$available;
    1048           }
    1049  
    1050           \$contains_classes_to_exclude = false;
    1051           WooShiptimize::log(\$shipping_method->get_title());
    1052           // Check if the package contains products that have at least one of the selected classes
    1053           foreach ( \$package['contents'] as \$key => \$item ) {
    1054              
    1055             foreach ( \$opt_exclude_classes  as \$c2exclude ) {
    1056               \$terms = get_the_terms( \$item['product_id'], 'product_shipping_class' );
    1057               if ( \$terms ) {
    1058                 foreach(\$terms as \$term) {
    1059                   \$contains_classes_to_exclude |=  \$term->term_id == \$c2exclude ; 
    1060                 }
    1061               }
    1062             }
    1063           }
    1064          
    1065           WooShiptimize::log(\"contains class \" . ( \$contains_classes_to_exclude ? 1 : 0 ));
    1066 
    1067           return !\$contains_classes_to_exclude;
    1068         }
    1069 
    10701015        public function validate_excludeclasses_field( \$key , \$value ) {
    10711016          if ( \$key === 'excludeclasses' ) {
     
    10781023         * Initialize free shipping.
    10791024         */
    1080         public function init() {
    1081           add_filter( 'woocommerce_shipping_' . \$this->id . '_is_available', array(\$this, 'method_available'), 10, 3 );
    1082 
     1025        public function init() {
    10831026          // Load the settings.
    10841027          \$this->init_form_fields();
  • shiptimize-for-woocommerce/trunk/includes/class-woo-shiptimize.php

    r2772699 r2800116  
    299299    add_filter( 'woocommerce_states', array($this, 'add_states') );
    300300
     301    add_filter( 'woocommerce_package_rates', array( $this, 'rates_filter'), 10, 2 );
     302  }
     303
     304  /**
     305   * Allow users to choose, if free shipping, then hide anything not free
     306   * Also check if exclude classes is set inside the shipping method
     307   **/
     308  public function rates_filter ( $rates, $package ) {
     309    // Check if hide not free enabled
    301310    if ( get_option('shiptimize_hide_not_free') ) {
    302       add_filter( 'woocommerce_package_rates', array( $this, 'shiptimize_hide_not_free'), 10, 2 );
    303     }
    304   }
    305 
    306   /**
    307    * Allow users to choose, if free shipping, then hide anything not free
    308    **/
    309   public function shiptimize_hide_not_free ( $rates, $package ) {
    310     $free = array();
     311      $free = array();
     312      foreach ( $rates as $id => $r) {
     313       
     314        if ($r->cost == 0) {
     315          array_push($free, $r);
     316        }
     317      }
     318
     319      if ( !empty($free) ) {
     320        $rates = $free;
     321      } 
     322    }
     323
     324    /// Check for exclude classes
     325    $newrates = array();
    311326    foreach ( $rates as $id => $r) {
     327      $options = get_option('woocommerce_' . $r->method_id . '_' . $r->instance_id . '_settings');
     328   
     329      if(isset($options['excludeclasses']) && $options['excludeclasses']) {
     330        WooShiptimize::log($r->label);
     331
     332        $opt_exclude_classes = explode( ',', $options['excludeclasses']);
     333        $contains_classes_to_exclude = false;
    312334     
    313       if ($r->cost == 0) {
    314         array_push($free, $r);
    315       }
    316     }
    317 
    318     return !empty($free) ? $free : $rates;
     335        // Check if the package contains products that have at least one of the selected classes
     336        foreach ( $package['contents'] as $key => $item ) {
     337          foreach ( $opt_exclude_classes  as $c2exclude ) {
     338            $terms = get_the_terms( $item['product_id'], 'product_shipping_class' );
     339            if ( $terms ) {
     340              foreach($terms as $term) {
     341                $contains_classes_to_exclude |=  $term->term_id == $c2exclude ; 
     342              }
     343            }
     344          }
     345        } 
     346
     347        WooShiptimize::log("contains class " . ( $contains_classes_to_exclude ? 1 : 0 ));
     348        if ( !$contains_classes_to_exclude ) {
     349          array_push($newrates,$r);
     350        }
     351      }
     352      else {
     353        array_push($newrates, $r);
     354      }
     355
     356      $rates = $newrates;
     357    }
     358
     359    return $rates;
    319360  }
    320361
Note: See TracChangeset for help on using the changeset viewer.