Created
December 7, 2020 14:21
-
-
Save xlawok/6acc170b3148e864468270bc759b0899 to your computer and use it in GitHub Desktop.
Woo shipment modyfication - based on weight and country, with on Carbon Fields on board
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
add_filter( 'woocommerce_package_rates', 'pk_woocommerce_shipping_by_weight', 9999, 2 ); | |
function pk_woocommerce_shipping_by_weight( $rates, $package ) { | |
$destination = $package['destination']; | |
$country = $destination['country']; | |
//cost sets in carbonfields for each reagions | |
$pl_shipping_costs = carbon_get_theme_option( 'pk_shipping_costs_pl' ); | |
$inter_shipping_costs = carbon_get_theme_option( 'pk_shipping_costs_inter' ); | |
$cart_weight=WC()->cart->get_cart_contents_weight(); | |
if($country=="PL"){ | |
foreach($rates as $key => $rate ) { | |
foreach($pl_shipping_costs as $pl_shipping_cost){ | |
$weight_above=$pl_shipping_cost['pk_shipping_method_above_weight_pl']; | |
if($cart_weight>$weight_above&&$key=="flat_rate:".$pl_shipping_cost['pk_shipping_methods']){ | |
$rates[$key]->cost =apply_filters('woocs_convert_price', $pl_shipping_cost['pk_shipping_method_cost_pl'] ,false); | |
} | |
} | |
} | |
} | |
else{ | |
foreach($rates as $key => $rate ) { | |
foreach($inter_shipping_costs as $inter_shipping_cost){ | |
$weight_above=$inter_shipping_cost['pk_shipping_method_above_weight_inter']; | |
if($cart_weight>$weight_above&&$key=="flat_rate:".$inter_shipping_cost['pk_shipping_methods_inter']){ | |
$rates[$key]->cost =apply_filters('woocs_convert_price', $inter_shipping_cost['pk_shipping_method_cost_inter'] ,false); | |
} | |
} | |
} | |
} | |
return $rates; | |
} | |
/// CARBONFIEDLS settings | |
// function agregating shipping method for each zones | |
function pk_shipping_method_array_0() { | |
$worldwide = new WC_Shipping_Zone( 0 ); | |
$shipping_methods = $worldwide->get_shipping_methods(); | |
$listing_methods=[]; | |
foreach ( $shipping_methods as $id => $shipping_method ) { | |
$listing_methods[ $id ] =$shipping_method->title; | |
} | |
return $listing_methods; | |
} | |
function pk_shipping_method_array_1() { | |
$worldwide = new WC_Shipping_Zone( 1 ); | |
$shipping_methods = $worldwide->get_shipping_methods(); | |
$listing_methods=[]; | |
foreach ( $shipping_methods as $id => $shipping_method ) { | |
$listing_methods[ $id ] =$shipping_method->title; | |
} | |
return $listing_methods; | |
} | |
// DEFINITION OF FIELDS IN CARBON FIELDS | |
Field::make( 'separator', 'theme_shipping_separator', 'USTAWIENIA WYSYŁKI' ), | |
Field::make( 'separator', 'theme_shipping_separator_pl', 'POLSKA' ), | |
Field::make( 'complex', 'pk_shipping_costs_pl', 'Dodaj koszty wysyłki' ) | |
->set_layout( 'grid' ) | |
->add_fields( array( | |
Field::make( 'select', 'pk_shipping_methods','Wybierz metodę wysyłki' ) | |
->add_options( 'pk_shipping_method_array_1' ), | |
Field::make( 'text', 'pk_shipping_method_above_weight_pl', 'Powyżej wagi (KG)' )->set_attribute( 'type', 'number' ), | |
Field::make( 'text', 'pk_shipping_method_cost_pl', 'Koszt wysyłki' )->set_attribute( 'type', 'number' ), | |
) ), | |
Field::make( 'separator', 'theme_shipping_separator_inter', 'Międzynarodowa wysyłka' ), | |
Field::make( 'complex', 'pk_shipping_costs_inter', 'Dodaj koszty wysyłki' ) | |
->set_layout( 'grid' ) | |
->add_fields( array( | |
Field::make( 'select', 'pk_shipping_methods_inter','Wybierz metodę wysyłki' ) | |
->add_options( 'pk_shipping_method_array_0' ), | |
Field::make( 'text', 'pk_shipping_method_above_weight_inter', 'Powyżej wagi (KG)' )->set_attribute( 'type', 'number' ), | |
Field::make( 'text', 'pk_shipping_method_cost_inter', 'Koszt wysyłki' )->set_attribute( 'type', 'number' ), | |
) ), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment