Created
April 15, 2016 19:23
-
-
Save albertojm/55a9319dadc36c936c84a3904d114fbd to your computer and use it in GitHub Desktop.
Chilexpress shipping method, calculated per city.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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') ); |
Estimados, quien lo pudo integrar
saludos
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quisiera saber como integrarlo o quien tiene algo asi gracias.