Skip to content

Instantly share code, notes, and snippets.

@albertojm
Created April 15, 2016 19:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save albertojm/55a9319dadc36c936c84a3904d114fbd to your computer and use it in GitHub Desktop.
Save albertojm/55a9319dadc36c936c84a3904d114fbd to your computer and use it in GitHub Desktop.
Chilexpress shipping method, calculated per city.
<?php
class WC_Chilexpress_Shipping_Method extends WC_Shipping_Method{
public function __construct(){
$this->id = 'chilexpress_shipping_method';
$this->method_title = __( 'Envíos vía Chilexpress (Chile)', 'woocommerce' );
// Load the settings.
$this->init_form_fields();
$this->init_settings();
// Define user set variables
$this->enabled = $this->get_option( 'enabled' );
$this->title = $this->get_option( 'title' );
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
public function init_form_fields(){
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Habilitar/Deshabilitar', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Habilitar envíos via Chilexpress', 'woocommerce' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Título del método de envío', 'woocommerce' ),
'type' => 'text',
'description' => __( 'El título del método de envío que el usuario verá en la página de checkout', 'woocommerce' ),
'default' => __( 'Envío vía Chilexpress', 'woocommerce' ),
)
);
}
public function is_available( $package ){
foreach ( $package['contents'] as $item_id => $values ) {
$_product = $values['data'];
$weight = $_product->get_weight();
return true;
}
return true;
}
public function calculate_shipping($package){
//get the total weight and dimensions
$weight = 0;
$dimensions = 0;
foreach ( $package['contents'] as $item_id => $values ) {
$_product = $values['data'];
$weight = $weight + $_product->get_weight() * $values['quantity'];
$dimensions = $dimensions + (($_product->length * $values['quantity']) * $_product->width * $_product->height);
}
if(!empty($_POST['state']) or !empty($_POST['s_state'])) :
if(!empty($_POST['s_state'])):
$state = $_POST['s_state'];
else:
$state = $_POST['state'];
endif;
$shipping_args = array(
'post_type' => 'chxp_city',
'orderby' => 'meta_value',
'posts_per_page' => 1,
'relation' => 'AND', // Optional, defaults to "AND"
'meta_query' => array(
array(
'key' => 'chxp_city_code',
'value' => $state,
'compare' => '='
),
)
);
$shipping = get_posts( $shipping_args );
if($shipping) :
$small_price = get_post_meta($shipping[0]->ID, 'chxp_small_price', true);
$medium_price = get_post_meta($shipping[0]->ID, 'chxp_medium_price', true);
if($weight < 6 && $weight >= 3) :
$cost = $medium_price + 1000;
elseif($weight < 10 && $weight >= 6) :
$cost = $medium_price + 1700;
elseif ($weight >= 10) :
$cost = $medium_price + 2200;
elseif($weight < 3) :
$cost = (int)$small_price + 700;
endif;
// send the final rate to the user.
$this->add_rate( array(
'id' => $this->id,
'label' => $this->title,
'cost' => $cost
));
return $cost;
endif;
else:
return false;
endif;
}
static function chilexpress_create_states($states) {
$args = array( 'posts_per_page' => -1, 'post_type' => 'chxp_city', 'orderby' => 'title', 'order' => 'ASC');
$posts = get_posts( $args );
if($posts) :
$states['CL'] = array();
foreach ($posts as $key => $city) {
$code = get_post_meta($city->ID, 'chxp_city_code', true);
$states['CL'][$code] = $city->post_title;
}
endif;
return $states;
}
static function woo_add_cart_fee(WC_Cart $cart) {
//WC()->cart->calculate_shipping();
}
}
add_action('woocommerce_cart_calculate_fees', array('WC_Chilexpress_Shipping_Method', 'woo_add_cart_fee'));
add_filter('woocommerce_states', array('WC_Chilexpress_Shipping_Method', 'chilexpress_create_states') );
@AnglDavd
Copy link

imagino que esto va en la function.php ?

@simoniturra
Copy link

Estás armando un plugin?

@Naginni21
Copy link

Feliz te ayudo con este código.
¿Que te falta para que este funcional?
Saludos

@rishi2414
Copy link

Please let me know how can I use this script in my website.
Thanks in advance

@jhoynerk
Copy link

@albertojm quiero integrar los servicios de tarificación y georeferencia, Tendras conocimientos sobre si estos servicios se pueden integrar? la URL de tarifiación es la siguiente. http://qaws.ssichilexpress.cl/TarificarCourier?wsdl pero no he logrado obtener los datos.

@sundios
Copy link

sundios commented Jan 6, 2017

Hola,

Funciona? donde lo integro functions.php?

@AkiraJinjuriki
Copy link

Quisiera saber como integrarlo o quien tiene algo asi gracias.

@AkiraJinjuriki
Copy link

Estimados, quien lo pudo integrar
saludos

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment