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 March 31, 2016 12:02
WooCommerce: Restrict shipping services for particular zip codes.
// Fedex Block Services for particular Zip Codes.
// Unset the shipping options of shipping method.
// You can find the shipping service codes by doing inspect element using
// developer tools of chrome. Code for each shipping service can be obtained by
// checking 'value' of shipping option.
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)
{
@WooForce
WooForce / functions.php
Created April 4, 2016 09:47
FedEx - Add custom customs description / customer reference
add_filter('wf_fedex_request','wf_fedex_label_request_callback', 10, 2 );
function wf_fedex_label_request_callback($request,$order){
$custom_product_description = 'My Product Description';
$request['CustomerReferences'][] = array('CustomerReferenceType' => 'CUSTOMER_REFERENCE', 'Value' => $custom_product_description );
$RequestedPackageLineItems = !empty( $request['RequestedShipment']['RequestedPackageLineItems'] ) ? $request['RequestedShipment']['RequestedPackageLineItems'] : array() ;
$flag = 0;
@WooForce
WooForce / functions.php
Last active April 8, 2016 00:24
FedEx - Change Commodities details description.
add_filter('wf_fedex_request','wf_fedex_label_request_callback', 10, 2 );
function wf_fedex_label_request_callback($request,$order){
$custom_product_description = 'My Product Description';
$commodities = !empty( $request['RequestedShipment']['CustomsClearanceDetail']['Commodities'] ) ? $request['RequestedShipment']['CustomsClearanceDetail']['Commodities']: array() ;
foreach ($commodities as $product_id => $value) {
$request['RequestedShipment']['CustomsClearanceDetail']['Commodities'][$product_id]['Description'] = $custom_product_description;
}
return $request;
}
@WooForce
WooForce / functions.php
Created April 13, 2016 13:49
FedEx - Change product name and description while label printing.
add_filter('wf_fedex_request','wf_fedex_label_custom_product_name_request_callback', 10, 2 );
function wf_fedex_label_custom_product_name_request_callback($request, $order){
$product_name = 'Book'; //Type here a product name it'll come as name of all products, Don't leave this blank or white space
$product_description = 'Book'; //Description, This will come for all products, Don't leave this blank or white space
$commodities = isset( $request['RequestedShipment']['CustomsClearanceDetail']['Commodities'] ) ? $request['RequestedShipment']['CustomsClearanceDetail']['Commodities'] : '';
if($commodities != '' ){
foreach ($commodities as $key => $commodity) {
$request['RequestedShipment']['CustomsClearanceDetail']['Commodities'][$key]['Name'] = sanitize_title( $product_name );
$request['RequestedShipment']['CustomsClearanceDetail']['Commodities'][$key]['Description'] = sanitize_title( $product_description );
@WooForce
WooForce / functions.php
Last active April 13, 2016 13:50
FedEx - Add P_O_NUMBER, INVOICE_NUMBER, CUSTOMER_REFERENCE & DEPARTMENT_NUMBER while label printing
add_filter('wf_fedex_request','wf_fedex_label_request_reff_callback', 10, 2 );
function wf_fedex_label_request_reff_callback($request,$order){
$po_number = 'x';
$inv_number = 'x';
$customs_duties_payer = 'SENDER';
$reff = array();
$reff['CustomerReferences'][] = array( 'CustomerReferenceType' => 'P_O_NUMBER', 'Value' => $po_number );
$reff['CustomerReferences'][] = array( 'CustomerReferenceType' => 'INVOICE_NUMBER', 'Value' => $inv_number );
$reff['CustomerReferences'][] = array( 'CustomerReferenceType' => 'CUSTOMER_REFERENCE', 'Value' => $order->id );
@WooForce
WooForce / functions.php
Last active April 19, 2016 12:13
FedEx - Change credentials for international shipping.
// Snippet for Rate request:
add_filter('wf_fedex_calculate_shipping_request','wf_fedex_dual_account_rate_request_callback', 10, 2 );
function wf_fedex_dual_account_rate_request_callback($request,$packages){
$ship_from_country_code = ''; // Origin Countrycode
$ship_from_postal_code = ''; // Postal code associated with alternative account
$fedex_international_acc_no = ''; // Alternative account number
$fedex_international_meter_number = ''; // Alternative account meter number
$fedex_international_key = ''; // Authentication Key of alternative account
$fedex_international_pwd = ''; // Password of alternative account
@WooForce
WooForce / functions.php
Last active April 22, 2016 07:45
Canada Post - Add a custom description while printing label.
add_filter('wf_canadapost_request','wf_canadapost_label_request_callback', 10, 2 );
function wf_canadapost_label_request_callback($request,$order){
$custom_product_description = 'My product description'; // This text might not exceed 35 characters
$xml = new SimpleXMLElement($request);
if($xml->{'delivery-spec'}->{'destination'}->{'address-details'}->{'country-code'} != 'CA'){ //if international
$ref_no = $xml->{'delivery-spec'}->{'references'}->{'customer-ref-1'};
$custom_text = $ref_no.", ".$custom_product_description;
$xml->{'delivery-spec'}->{'references'}->{'customer-ref-1'} = substr($custom_text,0,35);
@WooForce
WooForce / functions.php
Created May 13, 2016 14:47
Australia Post - Price adjustment based on shipping class
add_filter( 'wf_australia_post_rate_services', 'alter_service_rate_adjustments', 10, 2 );
function alter_service_rate_adjustments( $rate_service, $rate_code){
// Array of service codes with combination of shipping class and price adjustment
$adjustments = array(
'AUS_PARCEL_REGULAR' => array(
'16' => 4, // Shipping Class ID => Adjustment Price
'17' => 3,
),
'AUS_PARCEL_EXPRESS' => array(
@WooForce
WooForce / functions.php
Created May 25, 2016 16:49
UPS - Alter create shipment request
add_filter('wf_ups_shipment_confirm_request','alter_shipment_request',10,3);
function alter_shipment_request($request,$order){
$customer_first_name = $order->shipping_first_name;
$customer_last_name = $order->shipping_last_name;
$customer_full_name = $customer_first_name.' '.$customer_last_name;
$request = str_replace('<CompanyName>-</CompanyName>','<CompanyName>'.$customer_full_name.'</CompanyName>',$request);
return $request;
}
@WooForce
WooForce / functions.php
Last active May 31, 2016 03:50
FedEx and USPS - Skip product from shipping package
add_filter('wf_shipping_skip_product','skip_my_products',10,3);
function skip_my_products($skip = false, $product, $package){
$shipping_free_classes = array(16); // array of shipping class ids to exclude from cart
$shipping_class = $product['data']->get_shipping_class_id();
if(in_array($shipping_class, $shipping_free_classes)){
$skip = true;
}