Shipping method (Shipping) in custom Woocommerce

Asked

Viewed 95 times

0

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

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 showing the freight types to the customer and how to use the function "get_freight()"?! Or some example as a base! From now on, thank you very much.

1 answer

0


Follow the solution I found and this working 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' );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.