Skip to content

Instantly share code, notes, and snippets.

@xlawok
Last active December 19, 2020 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xlawok/05ce3b03011b37ba29784639add09084 to your computer and use it in GitHub Desktop.
Save xlawok/05ce3b03011b37ba29784639add09084 to your computer and use it in GitHub Desktop.
Woo + carbonfields - shipment modyfication - weight, zones and country, and class
//carbonfields settings:
function pk_get_shipping_countries(){
//get shipping countries for defining countries for bulky products
$woo_countries = new WC_Countries();
$countries = $woo_countries->get_allowed_countries();
return $countries;
}
function pk_get_shipping_zones(){
//get shipping methods for default zone (not included in other) ZONE 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 ] ="Strefa domyślna: ".$shipping_method->title;
}
$delivery_zones = WC_Shipping_Zones::get_zones();
foreach ((array) $delivery_zones as $key => $the_zone ) {
$pk_zone = new WC_Shipping_Zone($key );
$zone_shipping_methods = $pk_zone->get_shipping_methods();
foreach ( $zone_shipping_methods as $id => $shipping_method ) {
$listing_methods[ $id ] =$the_zone['zone_name'].": ".$shipping_method->title;
}
// $listing_methods[ $key ] =$the_zone['zone_name'];
}
return $listing_methods;
}
//defining carbon fields
Field::make( 'separator', 'theme_shipping_separator_all', 'Wprowadzanie kosztów wysyłki' ),
Field::make( 'complex', 'pk_shipping_costs_all', 'Dodaj koszty wysyłki' )
->set_layout( 'grid' )
->add_fields( array(
Field::make( 'select', 'pk_shipping_methods_all','Wybierz metodę wysyłki i region' )
->add_options( 'pk_get_shipping_zones' ),
Field::make( 'text', 'pk_shipping_method_above_weight_all', 'Powyżej wagi (KG)' )->set_attribute( 'type', 'number' ),
Field::make( 'text', 'pk_shipping_method_cost_all', 'Koszt wysyłki' )->set_attribute( 'type', 'number' ),
) ),
Field::make( 'separator', 'theme_bulky_shipping_separator', 'Ograniczenie wysyłki dużych gabarytów do krajów' ),
Field::make( 'multiselect', 'pk_bulky_countries','Wybierz kraj z dostawą dużych gabarytów' )
->add_options( 'pk_get_shipping_countries' ),
//functions.php
add_filter( 'woocommerce_package_rates', 'pk_woocommerce_shipping_by_weight_and_class', 9999, 2 );
function pk_woocommerce_shipping_by_weight_and_class( $rates, $package ) {
$destination = $package['destination'];
$country = $destination['country'];
$shipping_class_target = 35; // shipping class ID (to find it, see screenshot below)
$bulky_in_cart = false;
foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
//checking if any product in cart is in BULKY CLASS
if ( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
$bulky_in_cart = true;
break;
}
}
$all_bulky_countries = carbon_get_theme_option( 'pk_bulky_countries' );
if($bulky_in_cart&&!in_array($country,$all_bulky_countries)){
// checking if current country is not in array_of countries where you can shipp bulky products
// if could not shipp cleare shipping rates
$rates=false;
wc_add_notice( __("There are no delivery methods available for large products for the selected country."), 'error' );
}
else{
$shipping_zone = WC_Shipping_Zones::get_zone_matching_package( $package );
$zone_id=$shipping_zone->get_zone_id();
$all_shipping_costs = carbon_get_theme_option( 'pk_shipping_costs_all' );
$cart_weight=WC()->cart->get_cart_contents_weight();
foreach($rates as $key => $rate ) {
foreach($all_shipping_costs as $all_shipping_cost){
$weight_above=$all_shipping_cost['pk_shipping_method_above_weight_all'];
if($cart_weight>$weight_above&&$key=="flat_rate:".$all_shipping_cost['pk_shipping_methods_all']){
$rates[$key]->cost =apply_filters('woocs_convert_price', $all_shipping_cost['pk_shipping_method_cost_all'] ,false);
}
}
}
}
return $rates;
}
//PREVENT proceed to checkout if no shipping method
function disable_checkout_button_no_shipping() {
$package_counts = array();
// get shipping packages and their rate counts
$packages = WC()->shipping->get_packages();
foreach( $packages as $key => $pkg )
$package_counts[ $key ] = count( $pkg[ 'rates' ] );
// remove button if any packages are missing shipping options
if( in_array( 0, $package_counts ) )
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
}
add_action( 'woocommerce_proceed_to_checkout', 'disable_checkout_button_no_shipping', 1 );
function prevent_checkout_access_no_shipping() {
// Check that WC is enabled and loaded
if( function_exists( 'is_checkout' ) && is_checkout() ) {
// get shipping packages and their rate counts
$packages = WC()->cart->get_shipping_packages();
foreach( $packages as $key => $pkg ) {
$calculate_shipping = WC()->shipping->calculate_shipping_for_package( $pkg );
if( empty( $calculate_shipping['rates'] ) ) {
wp_redirect( esc_url( WC()->cart->get_cart_url() ) );
exit;
}
}
}
}
add_action( 'wp', 'prevent_checkout_access_no_shipping' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment