Skip to content

Instantly share code, notes, and snippets.

Avatar

Rynaldo rynaldos

View GitHub Profile
@rynaldos
rynaldos / nocoupon-checkout.php
Created May 27, 2020
WooCommerce remove checkout page coupon field for guests
View nocoupon-checkout.php
function no_coupon_checkout_function() {
if ( !is_user_logged_in() ) {
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
}
}
add_action('init', 'no_coupon_checkout_function');
@rynaldos
rynaldos / wc_pk_country_states.php
Last active May 5, 2020
Pakistan state/city extension for checkout page and shipping zones. Replaces states for cities
View wc_pk_country_states.php
add_filter( 'woocommerce_countries', 'rs_edit_pk_country' );
function rs_edit_pk_country ( $countries ) {
$new_countries = array(
'PK' => __( 'Pakistan', 'woocommerce' ),
);
return array_merge( $countries, $new_countries );
}
add_filter( 'woocommerce_continents', 'rs_add_new_pk_country_to_continents' );
@rynaldos
rynaldos / wc-mmq-browser-message.php
Created Apr 17, 2020
WooCommerce min/max quantities translate browser message
View wc-mmq-browser-message.php
function wc_max_qty_scripts() {
wp_add_inline_script( 'woocommerce', 'window.onload = function(){
function onBlurHandler( event ) {
var max = this.getAttribute( "max" );
if ( this.validity.rangeOverflow ) {
message = this.value + " is too much! Please contact us to order more than " + max + ".";
this.setCustomValidity( message );
View ie-req-code-wc.php
add_filter( 'woocommerce_checkout_fields', 'custom_override_default_address_fields' );
function custom_override_default_address_fields($fields){
global $woocommerce;
$country = $woocommerce->customer->get_billing_country();
if($country == 'IE'){
$fields['billing']['billing_postcode']['required'] = true;
$fields['shipping']['shipping_postcode']['required'] = true;
}
return $fields;
}
@rynaldos
rynaldos / wc-mmg-dbp.php
Last active Jul 2, 2020
Change Maxmind geolocation database path for WooCommerce
View wc-mmg-dbp.php
add_filter( 'woocommerce_maxmind_geolocation_database_path', 'woocommerce_new_maxmind_geolocation_database_path' );
function woocommerce_new_maxmind_geolocation_database_path( $database_path ) {
$upload_dir = wp_upload_dir();
$database_path = trailingslashit( $upload_dir['basedir'] ) . 'my-new-folder/dbfile.ext';
return $database_path;
}
// Did this help? Donate me some BTC: 1BEsm8VMkYhSFJ92cvUYwxCtsfsB2rBfiG
@rynaldos
rynaldos / wc-zero-tax-line.php
Created Jan 17, 2019
Show zero rate tax line in checkout when exempt
View wc-zero-tax-line.php
add_filter( 'woocommerce_cart_tax_totals', 'show_zero_rate_taxes' );
function show_zero_rate_taxes( $tax_totals ) {
if ( empty( $tax_totals ) && WC()->customer->is_vat_exempt() ) {
$tax_totals['zero-rated'] = (object) array(
'label' => 'Zero Rated Tax',
'amount' => 0,
'formatted_amount' => wc_price( 0 ),
'name' => 'Zero Rated Tax',
@rynaldos
rynaldos / wc_pimages_checkout.php
Last active Mar 23, 2021
Add product images to WooCommerce checkout page order review
View wc_pimages_checkout.php
function rs_woo_cart_attributes($cart_item, $cart_item_key) {
global $product;
if (is_cart()){
echo "<style>#checkout_thumbnail{visibility:hidden;}</style>";
}
$item_data = $cart_item_key['data'];
$post = get_post($item_data->get_id());
$thumb = get_the_post_thumbnail($item_data->get_id(), array( 32, 50));
echo '<div id="checkout_thumbnail" style="float: left; padding-right: 8px">' . $thumb . '</div> ' . $post->post_title;
}
@rynaldos
rynaldos / hide-free-when-pp-available.php
Last active Jul 2, 2020
WooCommerce (hide free shipping when per-product is available)
View hide-free-when-pp-available.php
function hide_free_when_pp_method_available( $rates ) {
$pp = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'per_product' === $rate->method_id ) {
$pp[ $rate_id ] = $rate;
break;
}
}
return ! empty( $pp ) ? $pp: $rates;
}
@rynaldos
rynaldos / wc-eu-vat-countries.php
Last active Feb 15, 2021
filter the countries where the VAT number field will show up for
View wc-eu-vat-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 ) {
// only show field for users in BE
return array( 'BE' );
}
@rynaldos
rynaldos / wc-sort-checkout-countries-custom.php
Last active Jul 2, 2020
[WooCommerce 3.0+] Custom sorting for checkout page country dropdown
View wc-sort-checkout-countries-custom.php
// disable woocommerce sorting
add_filter( 'woocommerce_sort_countries', '__return_false' );
add_filter( 'woocommerce_countries', 'wc_custom_countries_order', 10, 1 );
function wc_custom_countries_order( $countries ) {
// replace with iso code of the country (example: US or GB)
unset($countries['country_1_iso_code']);
unset($countries['country_2_iso_code']);
unset($countries['country_3_iso_code']);
// replace with iso code of country AND country name (example: US | United States or GB | United Kingdom (UK)