Skip to content

Instantly share code, notes, and snippets.

View WooForce's full-sized avatar

WooForce WooForce

View GitHub Profile
@WooForce
WooForce / functions.php
Last active September 6, 2018 13:11
DHL Express - Change currency conversion rate with live data
add_filter( 'wf_dhl_conversion_rate', 'alter_conversion_rate',10,2);
function alter_conversion_rate($rate, $dhl_currency){
if( !function_exists('get_woocommerce_currency')){
return;
}
$from_currency = urlencode($dhl_currency);
$to_currency = urlencode(get_woocommerce_currency());
try {
@WooForce
WooForce / functions.php
Created June 22, 2016 17:21
Shipping Pro - discard the base cost of one of the shipping classes if both of them come up in the cart based on states
add_filter( 'wf_woocommerce_shipping_pro_shipping_costs', 'wf_hide_base_cost_for_non_westcost');
function wf_hide_base_cost_for_non_westcost( $costs ) {
$shipping_state = WC()->customer->shipping_state;
$states_requires_price_adjustment = array('CA');
$shippingclass_requires_price_adjustment = array('shipping-class-a');
if(!empty($shipping_state) && in_array($shipping_state,$states_requires_price_adjustment)){
foreach ($costs as $method_group => $method_cost) {
if(isset($method_cost['shipping_name']) && isset($method_cost['cost'])){
@WooForce
WooForce / functions.php
Created June 22, 2016 17:15
Stamps - - Alter create shipment request
add_filter('wf_stamps_request','filter_stamps_request',10,2);
function filter_stamps_request($request, $order){
if(isset($request['To'])){
if(isset($request['To']['ZIPCode'])){
// ignore values after '-'
$request['To']['ZIPCode'] = current(explode('-',$request['To']['ZIPCode']));
}
}
@WooForce
WooForce / functions.php
Last active August 23, 2016 11:33
Ignore invalid returned rates for stamps
add_filter('wf_rate_for_package','ignore_invalid_box_rates',10,2);
function ignore_invalid_box_rates($response, $package_request){
// stamps package types and sizes
$stamps_box_sizes = array(
// Domestic
'US-FC' => array(
// Name of the service shown to the user
'name' => 'First-Class Mail®',
'package_types' => array(
'Package' => array(
@WooForce
WooForce / functions.php
Created June 21, 2016 09:12
To use a particular shipping method based on order total weight
add_filter( 'wf_rate_for_service', 'wf_force_rate_on_weight', 10, 2 );
function wf_force_rate_on_weight($rates, $packages){
$weight_less_than[] = array('weight'=>1, 'service_code'=>'US-EMI', 'package_type'=>'USPS flat rate envelope'); //Enter proper values or remove this line
$weight_greater_than[] = array('weight'=>1, 'service_code'=>'US-EMI', 'package_type'=>'Package'); //Enter proper values or remove this line
$weight_equal_to[] = array('weight'=>1, 'service_code'=>'US-EMI', 'package_type'=>'USPS flat rate envelope'); //Enter proper values or remove this line
$weight_less_than[] = array('weight'=>1, 'service_code'=>'US-XM', 'package_type'=>'USPS Legal Flat Rate Envelope'); //Enter proper values or remove this line
$weight_greater_than[] = array('weight'=>1, 'service_code'=>'US-XM', 'package_type'=>'Package'); //Enter proper values or remove this line
$weight_equal_to[] = array('weight'=>1, 'service_code'=>'US-XM', 'package_type'=>'USPS Legal Flat Rate Envelope'); //Enter proper values or remove th
@WooForce
WooForce / functions.php
Last active December 1, 2017 05:10
Hide shipping method based on shipping class
add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 10, 2);
function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package)
{
$hide_when_shipping_class_exist = array(
42 => array(
'free_shipping'
)
);
@WooForce
WooForce / my-custom-shipping-cost.php
Created June 8, 2016 05:08
Plugin to calculate My Custom Shipping Cost
<?php
/*
Plugin Name: My Custom Shipping
Description: Plugin to calculate My Custom Shipping Cost
Version: 1.0.0
Author: WooForce
Author URI: http://www.wooforce.com
*/
function my_custom_shipping_init() {
@WooForce
WooForce / functions.php
Last active June 7, 2016 12:22
Price adjustment if the cart total exceed given value.
add_filter( 'woocommerce_package_rates', 'wf_rate_adjustment_if_cart_total_exceed', 10, 2 );
function wf_rate_adjustment_if_cart_total_exceed( $available_shipping_methods, $package ) {
$minimum_cart_price_to_adjust = 30; //give value here
$shipping_services_with_price = array(
'wf_shipping_ups:01' => 0, //give shipping service with adjustment price in percent.
'wf_shipping_ups:02' => 0
);
$cart_total = WC()->cart->cart_contents_total;
@WooForce
WooForce / functions.php
Created June 1, 2016 16:12
Hide shipping methods when a shipping class is not available in the cart
add_filter('woocommerce_package_rates', 'hide_shipping_method_when_shipping_class_product_is_not_in_cart', 10, 2);
function hide_shipping_method_when_shipping_class_product_is_not_in_cart($available_shipping_methods, $package)
{
// Shipping class IDs that need the method removed
$shipping_class_ids = array(
27,
);
@WooForce
WooForce / functions.php
Last active August 2, 2016 12:23
Hide shipping methods based on destination zipcode
add_filter('woocommerce_package_rates', 'wf_remove_shipping_options_for_particular_zip_codes', 10, 2);
function wf_remove_shipping_options_for_particular_zip_codes($rates, $package)
{
global $woocommerce;
$zip_array = array(
'30031'
);
if ( in_array( $woocommerce->customer->get_shipping_postcode() , $zip_array) ) {
unset($rates['wf_fedex_woocommerce_shipping:FEDEX_GROUND']);