Skip to content

Instantly share code, notes, and snippets.

View WooForce's full-sized avatar

WooForce WooForce

View GitHub Profile
@WooForce
WooForce / functions.php
Created August 23, 2016 13:52
UPS - convert charset while creating label
add_filter('wf_ups_shipment_confirm_request', 'wf_ups_convert_charset');
function wf_ups_convert_charset($request){
return iconv("UTF-8", "ISO-8859-1//TRANSLIT", $request);
}
@WooForce
WooForce / functions.php
Created August 19, 2016 10:52
WooCommerce - Customize Cart / Checkout Page No Shipping Methods Available message based on Zip Code.
// For Cart Page.
add_filter( 'woocommerce_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
// For Checkout page
add_filter( 'woocommerce_cart_no_shipping_available_html', 'wf_customize_default_message', 10, 1 );
function wf_customize_default_message( $default_msg ) {
$zip_array = array(
'30031',
);
if ( in_array( WC()->customer->get_shipping_postcode() , $zip_array) ) {
@WooForce
WooForce / functions.php
Created August 17, 2016 06:27
Hide Shipping methods when free shipping is available
add_filter('woocommerce_package_rates', 'wf_hide_shipping_methods_when_free_shipping_is_applicable', 10, 2);
function wf_hide_shipping_methods_when_free_shipping_is_applicable($rates, $package)
{
foreach($rates as $key => $value) {
if (strpos($key , 'free_shipping') !== false) {
foreach($rates as $ratekey => $ratevalue) {
@WooForce
WooForce / functions.php
Created August 16, 2016 14:15
Alter messages for incomplete address in shipping caluculator
add_filter( 'woocommerce_cart_no_shipping_available_html', 'wf_custom_message_for_incomplete_address', 10, 1 ); // Alters message on Cart page
add_filter( 'woocommerce_no_shipping_available_html', 'wf_custom_message_for_incomplete_address', 10, 1 ); // Alters message on Checkout page
function wf_custom_message_for_incomplete_address( $default ) {
$wf_cart_country = WC()->customer->get_shipping_country();
$wf_cart_state = WC()->customer->get_shipping_state();
$wf_cart_postcode = WC()->customer->get_shipping_postcode();
$shipping_msg_array = array();
@WooForce
WooForce / functions.php
Created August 16, 2016 05:03
Adjusting puerto rico as state
function wf_remove_puerto_rico_country( $country ) {
if(array_key_exists('PR', $country))
{
unset($country['PR']);
}
return $country;
}
add_filter( 'woocommerce_countries', 'wf_remove_puerto_rico_country', 10, 1 );
function wf_add_puerto_to_us_states( $states ) {
@WooForce
WooForce / functions.php
Created August 11, 2016 10:56
UPS - Alter UPS account based on order address
// Altering rate request
add_filter('wf_ups_rate_request_data', 'wf_ups_modify_settings_fields',10, 3);
function wf_ups_modify_settings_fields($rate_req_data, $package){
$destination_country = $package['destination']['country'];
$destination_state = $package['destination']['state'];
$alternate_accounts = array();
@WooForce
WooForce / functions.php
Created August 11, 2016 08:12
Adjusting shipping rates based on product shipping class
add_filter( 'woocommerce_package_rates', 'adjustment_in_rates_of_product_with_shipping_class', 10, 2 );
function adjustment_in_rates_of_product_with_shipping_class( $available_shipping_methods, $package ) {
// Shipping class IDs with extra costs.
$shipping_class_and_price = array(
13 => 10,
);
$shipping_services = array(
@WooForce
WooForce / functions.php
Created July 14, 2016 17:30
Set minimum shipping rate for a shipping method
add_filter('woocommerce_package_rates', 'wf_ups_min_rate', 10, 2);
function wf_ups_min_rate($available_rates, $package)
{
$min_shipping_rate = 10;
foreach($available_rates as $rate_key => $rate){
if($rate->method_id == 'wf_shipping_ups' && $rate->cost<$min_shipping_rate){
$available_rates[$rate_key]->cost = $min_shipping_rate;
}
}
@WooForce
WooForce / functions.php
Created June 28, 2016 19:12
Price adjustment for a certain state and service.
add_filter('wf_ups_rate', 'wf_rate_adjustment', 10, 1);
function wf_rate_adjustment($rate_xml_object){
$states_requires_price_adjustment = array('CA'); //add state code here
$service_code_requires_price_adjustment = array('01'=>0.1, '02'=>0.5, '03'=>0.12); //add service code with adjustment value.
$shipping_state = WC()->customer->shipping_state;
if(!empty($shipping_state) && in_array($shipping_state, $states_requires_price_adjustment) ){
$code = (string)$rate_xml_object->RatedShipment->Service->Code;
if( array_key_exists($code, $service_code_requires_price_adjustment) ){
@WooForce
WooForce / functions.php
Created June 28, 2016 17:30
Rearrange shipping methods
add_filter('woocommerce_package_rates', 'wf_sort_shipping_methods', 10, 2);
function wf_sort_shipping_methods($available_shipping_methods, $package)
{
// Arrange shipping methods as per your requirement
$sort_order = array(
'wf_shipping_ups' => array(),
'wf_shipping_usps' => array(),
'free_shipping' => array(),
'local_pickup' => array(),