Skip to content

Instantly share code, notes, and snippets.

View webdados's full-sized avatar

Marco Almeida webdados

View GitHub Profile
@webdados
webdados / woocommerce_variation_has_changed.js
Created October 7, 2020 15:33
Show all options on the first select for a WooCommerce variable products even if not available
(function($) {
$( '.variations_form' ).on( 'woocommerce_variation_has_changed', function( ev ) {
$( this ).find( '.variations select' ).each( function( index, el ) {
//Get the first select current value
var value = $( el ).val();
//Get the original HTML from .data( 'attribute_html' ) and replace it again on the element.
$( el ).html( $( el ).data( 'attribute_html' ) );
//Then set the value again
$( el ).val( value );
@webdados
webdados / woocommerce_nif_field_priority.php
Created September 28, 2020 12:34
Change NIF field priority on the WooCommerce checkout
<?php
//Change NIF field priority
add_filter( 'woocommerce_nif_field_priority', 'woocommerce_nif_field_priority' );
function woocommerce_nif_field_priority( $priority ) {
//Default is 120
return 25;
}
@webdados
webdados / magic_coupon_html_message_replace_tags.php
Created September 22, 2020 12:18
First and last day of the month as "Magic coupon" HTML message
<?php
add_filter( 'magic_coupon_html_message_replace_tags', 'bf_magic_coupon_html_message_replace_tags' );
function bf_magic_coupon_html_message_replace_tags( $tags ) {
$tags['date_first_day_month'] = date_i18n( get_option( 'date_format' ), strtotime( date_i18n( 'Y-m-01' ) ) );
$tags['date_last_day_month'] = date_i18n( get_option( 'date_format' ), strtotime( date_i18n( 'Y-m-t' ) ) );
return $tags;
}
@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;
}