Skip to content

Instantly share code, notes, and snippets.

@NickGreen
NickGreen / cart_total_notice.php
Last active November 2, 2021 16:33
Add cart and checkout notices if total less than $25
<?php
add_action( 'woocommerce_before_cart_table', 'free_shipping_notice' );
add_action( 'woocommerce_checkout_before_customer_details', 'free_shipping_notice' );
function free_shipping_notice() {
if ( 25 > WC()->cart->get_total() ) {
echo '<div style="color: #e08e79;">All orders over $25 ship free</div>';
}
}
@NickGreen
NickGreen / prod_cat_email.php
Last active November 2, 2021 16:32
Add Product Category to email subject WooCommerce
<?php
add_filter( 'woocommerce_email_subject_new_order', 'customizing_new_order_subject', 10, 2 );
function customizing_new_order_subject( $formated_subject, $order ) {
$email = WC()->mailer->get_emails()['WC_Email_New_Order'];
$subject = $email->get_option( 'subject', $email->get_default_subject() );
$product_categories = array();
foreach ( $order->get_items() as $item ) {
$product_categories[] = implode( ', ', wp_get_post_terms( $item->get_product_id(), 'product_cat', array( 'fields' => 'names' ) ) );
@NickGreen
NickGreen / md5_hash.js
Created April 27, 2021 16:24
MD5 hashing in JS example
additionalFields.forEach( ( { label, type, required, options = '' } ) => {
if ( '' !== type ) {
var MD5 = function(d){var r = M(V(Y(X(d),8*d.length)));return r.toLowerCase()};function M(d){for(var _,m="0123456789ABCDEF",f="",r=0;r<d.length;r++)_=d.charCodeAt(r),f+=m.charAt(_>>>4&15)+m.charAt(15&_);return f}function X(d){for(var _=Array(d.length>>2),m=0;m<_.length;m++)_[m]=0;for(m=0;m<8*d.length;m+=8)_[m>>5]|=(255&d.charCodeAt(m/8))<<m%32;return _}function V(d){for(var _="",m=0;m<32*d.length;m+=8)_+=String.fromCharCode(d[m>>5]>>>m%32&255);return _}function Y(d,_){d[_>>5]|=128<<_%32,d[14+(_+64>>>9<<4)]=_;for(var m=1732584193,f=-271733879,r=-1732584194,i=271733878,n=0;n<d.length;n+=16){var h=m,t=f,g=r,e=i;f=md5_ii(f=md5_ii(f=md5_ii(f=md5_ii(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_hh(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_gg(f=md5_ff(f=md5_ff(f=md5_ff(f=md5_ff(f,r=md5_ff(r,i=md5_ff(i,m=md5_ff(m,f,r,i,d[n+0],7,-680876936),f,r,d[n+1],12,-389564586),m,f,d[n+2],17,606105819),i,m,d[n+3],22,-1044525330),r=md5_ff(r,i=md5_ff
@NickGreen
NickGreen / return_to_shop.php
Created February 15, 2021 16:24
Return to shop link WooCommerce
<?php
add_filter( 'woocommerce_return_to_shop_redirect', function() { return '/shop'; });
@NickGreen
NickGreen / currency_position.php
Last active February 8, 2021 19:48
Switch currency position to left for USD
<?php
add_filter('woocommerce_price_format', 'woo_custom_format_position', 999, 2);
function woo_custom_format_position( $format, $currency_pos )
{
/*'left':$format = '%1$s%2$s';
'right':$format = '%2$s%1$s';
'left_space':$format = '%1$s&nbsp;%2$s';
'right_space':$format = '%2$s&nbsp;%1$s';
*/
@NickGreen
NickGreen / get_location.php
Created February 3, 2021 20:54
Get customer location
<?php
if( get_current_user_id() > 0 ) { // if customer exists already
$country = WC()->customer->get_billing_country();
} else { // if not-logged-in, need to use geolocation data
// Get an instance of the WC_Geolocation object class
$geo_instance = new WC_Geolocation();
// Get geolocated user geo data.
$user_geodata = $geo_instance->geolocate_ip();
// Get current user GeoIP Country
@NickGreen
NickGreen / custom_price.php
Last active November 22, 2021 02:19
Custom price based on custom field
<?php
// Save custom calculated price as custom cart item data
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {
if ( CUSTOMER_IS_IN_US ) {
// Set the custom data in the cart item
$cart_item_data['custom_price'] = GET_PRICE_FROM_CUSTOM_FIELD;
@NickGreen
NickGreen / business_hours.php
Created December 2, 2020 18:03
Disable all autoupdates during business hours
<?php
// Place this snippet in config.php file
// Suspend all updates when outside of business hours, 9:00 AM to 5:30 PM
$updates_suspended = (date('Hi') < 0900 || date('Hi') > 1730);
define( 'AUTOMATIC_UPDATER_DISABLED', $updates_suspended );
@NickGreen
NickGreen / auto_update_specific_times.php
Last active April 15, 2021 13:16
Allow plugins to be autoupdated only during specific days and times
<?php
/*
Plugin Name: Plugin Autoupdate Filter
Plugin URI: https://gist.github.com/NickGreen/a66d349575cf9e78c6dafd92efa5288a/edit
Description: Plugin which sets plugin autoupdates to always on, but only happen during specific times.
Version: 1.0
Author: Nick Green
Author URI:
License: GPLv3
*/
@NickGreen
NickGreen / primary-keys.sh
Last active October 9, 2020 22:14
Create primary keys if they are missing
#!/bin/bash
# Export a database backup beforehand
wp-cli db export --path=/htdocs/__wp__
# Get a list of tables missing primary kkeys
TABLES=$(wp-cli --path=/htdocs/__wp__ db query 'SELECT TABLES.TABLE_NAME FROM INFORMATION_SCHEMA.TABLES LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c ON ( TABLES.TABLE_NAME = c.TABLE_NAME AND c.CONSTRAINT_SCHEMA = TABLES.TABLE_SCHEMA AND c.constraint_name = "PRIMARY" ) WHERE c.constraint_name IS NULL AND TABLE_TYPE = "BASE TABLE"')