Shipping method on custom woocommerce

0

Hello. I am creating a method of sending (plugin) where I send the zip and cart items and the system (a third party site) returns me the values of the freight (PAC and SEDEX). I have already implemented a part of the plugin that follows.

function get_fretes($cep, $itens_carrinho){
   // RETURNA OS VALORES DOS FRETES: PAC, SEDEX e ETCs
   return $fretes;
}

function metodo_envio_teste_init() {
   if ( ! class_exists( 'WC_TESTE_PAC' ) ) {
      class WC_TESTE_PAC extends WC_Shipping_Method {
         public function __construct() {
            $this->id = 'teste_pac';
            $this->title = __( 'PAC' );
            $this->method_description = __( 'Método usado pelo fornecedor, calculado diretamente!' ); //
            $this->enabled = 'yes';
         }
         public function is_available( $package ){
           // Verifica se o cliente digitou o cep
           if(empty(WC()->customer->get_shipping_postcode()))
              return false;
           else
              return true;
         }
         public function calculate_shipping( $package = Array() ) {
            $rate = array(
              'id' => $this->id,
              'label'    => "PAC",
              'cost'     => '50'
            );
            $this->add_rate( $rate );
         }
      }
   }
}
add_action( 'woocommerce_shipping_init', 'metodo_envio_teste_init' );

function add_metodo_envio_teste( $methods ) {
   $methods[] = 'WC_TESTE_PAC';
   return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_metodo_envio_teste' );

I need help on how to show customer freight rates and how to use the "get_fretes ()" function ?! Or some example as a basis! Thanks in advance.

    
asked by anonymous 09.07.2016 / 03:42

1 answer

0

Follow the solution I found and it works perfectly!

function get_fretes($cep, $itens_carrinho){
   // RETURNA OS VALORES DOS FRETES: PAC, SEDEX e ETCs
   //      Array(
   //         [PAC] => 12.32
   //         [SEDEX] => 15.66
   //      )
   return $fretes;
}

function metodo_envio_teste_init() {
   if ( ! class_exists( 'WC_TESTE_PAC' ) ) {
      class WC_TESTE_PAC extends WC_Shipping_Method {
         public function __construct() {
            $this->id = 'teste_pac';
            $this->title = __( 'PAC' );
            $this->method_description = __( 'Método usado pelo fornecedor, calculado diretamente!' ); //
            $this->enabled = 'yes';
         }
         public function is_available( $package ){
           // Verifica se o cliente digitou o cep
           if(empty(WC()->customer->get_shipping_postcode()))
              return false;
           else{
              global $fretes;   
              $carrinho = WC()->cart->get_cart();
              $itens = array();
              foreach($carrinho as $item){
                 $produto = wc_get_product($item['product_id']);
                 $itens[] = array('sku' => $produto->get_sku(), 'quantity' => $item['quantity']);
              }
              $fretes = get_fretes(WC()->customer->get_shipping_postcode(), $itens);
              if($fretes != null)
                 return true;
              else
                 return false;
           }
         }
         public function calculate_shipping( $package = Array() ) {
            global $fretes;
            $rate = array(
              'id' => $this->id,
              'label'    => "PAC",
              'cost'     => $fretes['PAC']
            );
            $this->add_rate( $rate );
         }
      }
   }
}
add_action( 'woocommerce_shipping_init', 'metodo_envio_teste_init' );

function add_metodo_envio_teste( $methods ) {
   $methods[] = 'WC_TESTE_PAC';
   return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'add_metodo_envio_teste' );
    
10.04.2017 / 06:20