Skip to content

Instantly share code, notes, and snippets.

View webdados's full-sized avatar

Marco Almeida webdados

View GitHub Profile
@webdados
webdados / sanitize_file_name.php
Created September 10, 2020 11:00
Sanitize WordPress uploaded file names
<?php
add_filter( 'sanitize_file_name', 'my_sanitize_file_name' );
function my_sanitize_file_name( $filename ) {
$original_chars = array(
'/А/','/Б/','/В/','/Г/', // cyrillic alphabet
'/Д/','/Е/','/Ж/','/З/','/И/',
'/Й/','/К/','/Л/','/М/','/Н/',
'/О/','/П/','/Р/','/С/','/Т/',
@webdados
webdados / custom_prevent_barcode.php
Created September 1, 2020 08:33
Only show the WooCommerce Barcode on processing (and completed) orders
<?php
//Add this to your (child-)theme functions.php file
//Before woocommerce_email_after_order_table 1 and woocommerce_order_details_after_order_table 1, because on the woocommerce_order_barcodes_display_barcode filter we don't have access to the $order object
add_action( 'woocommerce_email_after_order_table', 'custom_prevent_barcode', -1, 1 );
add_action( 'woocommerce_order_details_after_order_table', 'custom_prevent_barcode', -1, 1 );
function custom_prevent_barcode( $order ) {
$allowed_statuses = array(
'processing',
@webdados
webdados / remove_unnecessary_scripts_and_styles.css.php
Created July 31, 2020 12:17
Remove WordPress unnecessary scripts and styles that some shortcodes load on every page
<?php
add_action( 'wp_enqueue_scripts', 'my_remove_unnecessary_scripts_and_styles', PHP_INT_MAX );
function my_remove_unnecessary_scripts_and_styles() {
//Remove Mapbox styles and scripts
global $post;
if ( ! ( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'wp_mapbox_gl_js' ) ) ) {
wp_dequeue_style( 'mapbox_gl_js_css' );
wp_dequeue_style( 'mapbox_gl_js_geocoder_css' );
wp_dequeue_style( 'mapbox_gl_js_directions_css' );
wp_dequeue_style( 'wp-mapbox-gl-js' );
@webdados
webdados / woocommerce_shop_closed.php
Created July 24, 2020 07:10
Close the WooCommerce shop on certain days of the week at a certain time in the day
<?php
/* Hire me at webdados.pt for more WooCommerce magic */
//The function that determines if the shop is closed or not
function custom_shop_closed() {
//Week days closed - ISO-8601 numeric representation of the day of the week - 1 (for Monday) through 7 (for Sunday)
$week_days_closed = array( 5, 6 ); //Fridays and Saturdays
//Hours closed
$min_hour_close = '07:00';
<?php
//You need this plugin: https://www.webdados.pt/wordpress/plugins/dpd-portugal-para-woocommerce-wordpress/
//Monitor the result (success) and do whatver you want with it - Needs to be set before running it
add_action( 'woo_dpd_portugal_requested_pickup', function( $pickup_request ) {
var_dump( $pickup_request ); //You should email it, or log it, or... whatever
} );
//Monitor the result (error) and do whatver you want with it - Needs to be set before running it
@webdados
webdados / invoicexpress_woocommerce_automatic_invoice_possible_status.php
Created June 29, 2020 11:40
Add possible statuses to InvoiceXpress for WooCommerce automatic invoicing
<?php
add_filter( 'invoicexpress_woocommerce_automatic_invoice_possible_status', function( $statuses ) {
$statuses[] = 'wc-picking';
$statuses[] = 'wc-another-one-bites-the-dust';
return $statuses;
} );
@webdados
webdados / woocommerce_product_add_to_cart_text.php
Created June 24, 2020 10:08
Change WooCommerce add to cart text for products on backorder
<?php
add_filter( 'woocommerce_product_add_to_cart_text', 'my_woocommerce_product_add_to_cart_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'my_woocommerce_product_add_to_cart_text', 10, 2 );
function my_woocommerce_product_add_to_cart_text( $text, $product ) {
if ( $product->is_on_backorder() ) {
return 'Reservar';
}
return $text;
}
@webdados
webdados / woocommerce_checkout_update_order_meta.php
Created June 24, 2020 09:47
Add WooCommerce order meta if there's a product on backorder
<?php
add_action( 'woocommerce_checkout_update_order_meta', function( $order_id, $data ) {
foreach ( WC()->cart->cart_contents as $item ) {
if ( $item['data']->is_on_backorder() ) {
$order = wc_get_order( $order_id );
$order->update_meta_data( '_has_backorder_product', 1 );
$order->save();
break;
}
}
@webdados
webdados / invoicexpress_woocommerce_exccat_exclude_types.php
Last active June 8, 2020 17:11
Exclude document types from invoicing exclusions on "Invoicing with InvoiceXpress for WooCommerce - Exclude categories/tags extension"
<?php
add_filter( 'invoicexpress_woocommerce_exccat_exclude_types', function( $exclude_types ) {
//Return an array of document types where all items should be invoiced and not excluded by category or tag
return array(
'transport_guide'
);
} );
@webdados
webdados / woocommerce_get_european_union_countries_after_wc4.php
Last active April 7, 2020 20:56
Workaround for WooCommerce get_european_union_countries( 'eu_vat' ) after WooCommerce 4.0.0
<?php
//See here why this is needed: https://github.com/woocommerce/woocommerce/issues/26105
function woocommerce_get_european_union_countries_after_wc4( $type = '' ) {
$countries = WC()->countries->get_european_union_countries();
if ( 'eu_vat' === $type ) {
$countries[] = 'MC';
$countries[] = 'IM';
$countries[] = 'GB'; //Great Britain is still part of the EU VAT zone
}
return $countries;