Skip to content

Instantly share code, notes, and snippets.

Avatar

WooCommerce.com Documentation woogist

View GitHub Profile
@woogist
woogist / wc_shipment_tracking_add_custom_provider.php
Last active Nov 26, 2021
Shipment Tracking: adds custom provider
View wc_shipment_tracking_add_custom_provider.php
/**
* wc_shipment_tracking_add_custom_provider
*
* Adds custom provider to shipment tracking or add existing providers to another country.
*
* Change the country name, the provider name, and the URL (it must include the %1$s)
* Add one provider per line
*/
add_filter( 'wc_shipment_tracking_get_providers' , 'wc_shipment_tracking_add_custom_provider' );
View functions.php
add_action( 'updated_users_subscriptions_for_order', 'wc_subs_suspend_on_cod' );
function wc_subs_suspend_on_cod( $order ) {
if ( ! is_object( $order ) ) {
$order = new WC_Order( $order );
}
foreach ( WC_Subscriptions_Order::get_recurring_items( $order ) as $order_item ) {
$subscription_key = WC_Subscriptions_Manager::get_subscription_key( $order->id, WC_Subscriptions_Order::get_items_product_id( $order_item ) );
@woogist
woogist / functions.php
Last active Nov 16, 2021
Apply different tax rates based on user role
View functions.php
<?php
/**
* Apply a different tax rate based on the user role.
*/
function wc_diff_rate_for_user( $tax_class, $product ) {
if ( is_user_logged_in() && current_user_can( 'administrator' ) ) {
$tax_class = 'Zero Rate';
}
return $tax_class;
@woogist
woogist / wc-eu-vat-remove-some-countries.php
Created Sep 29, 2021
filter to remove select countries where the VAT number field will show for
View wc-eu-vat-remove-some-countries.php
add_filter( 'woocommerce_eu_vat_number_country_codes', 'woo_custom_eu_vat_number_country_codes' );
function woo_custom_eu_vat_number_country_codes( $vat_countries ) {
$display_vat = array_diff($vat_countries, ['SE', 'GB']); // remove countries in second array
return array_values($display_vat); // reindex array
}
@woogist
woogist / wc-eu-vat-one-country.php
Created Sep 29, 2021
filter the countries where the VAT number field will show for
View wc-eu-vat-one-country.php
add_filter( 'woocommerce_eu_vat_number_country_codes', 'woo_custom_eu_vat_number_country_codes' );
function woo_custom_eu_vat_number_country_codes( $vat_countries ) {
// only show field for users in BE
return array( 'BE' );
}
View functions.php
add_filter( 'woocommerce_countries_base_postcode', create_function( '', 'return "80903";' ) );
add_filter( 'woocommerce_countries_base_city', create_function( '', 'return "COLORADO SPRINGS";' ) );
View functions.php
function wc_subscriptions_custom_price_string( $pricestring ) {
$pricestring = str_replace( 'every 3 months', 'per season', $pricestring );
$pricestring = str_replace( 'sign-up fee', 'initial payment', $pricestring );
return $pricestring;
}
add_filter( 'woocommerce_subscriptions_product_price_string', 'wc_subscriptions_custom_price_string' );
add_filter( 'woocommerce_subscription_price_string', 'wc_subscriptions_custom_price_string' );
View functions.php
function wc_edit_bookings_form_fields_order( $fields ) {
$duration = $fields['wc_bookings_field_duration'];
$persons = $fields['wc_bookings_field_persons'];
$resource = $fields['wc_bookings_field_resource'];
$date = $fields['wc_bookings_field_start_date'];
$new_fields = array(
'wc_bookings_field_start_date' => $date,
'wc_bookings_field_resource' => $resource,
'wc_bookings_field_duration' => $duration,
View hide_unused_providers.php
add_filter( 'wc_shipment_tracking_get_providers', 'custom_shipment_tracking' );
function custom_shipment_tracking( $providers ) {
unset($providers['Australia']);
unset($providers['Austria']);
unset($providers['Brazil']);
unset($providers['Belgium']);
unset($providers['Canada']);
unset($providers['Czech Republic']);
@woogist
woogist / functions.php
Last active May 14, 2021
Redirect to a custom page after login based on the user role
View functions.php
<?php
/**
* Redirect users to custom URL based on their role after login
*
* @param string $redirect
* @param object $user
* @return string
*/
function wc_custom_user_redirect( $redirect, $user ) {
// Get the first of all the roles assigned to the user