Skip to content

Instantly share code, notes, and snippets.

View jackgregory's full-sized avatar

Jack Gregory jackgregory

View GitHub Profile
@jackgregory
jackgregory / functions.php
Last active December 15, 2015 22:58
WooCommerce - Disable and add 'sold out' to a single WooCommerce product variation dropdown
add_action( 'woocommerce_before_add_to_cart_form', 'woocommerce_variations_sold_out_filter' );
function woocommerce_variations_sold_out_filter() {
// disable and add "sold out" to product variations
wc_enqueue_js( '
var product_variations = $( "form.variations_form" ).data( "product_variations" );
// dont disable with multiple drop-downs
if ( product_variations && $( "form.variations_form" ).find( "select" ).length === 1 ) {
var attribute_name = $( "form.variations_form" ).find( "select" ).attr( "name" );
$.each( product_variations, function( key, value ) {
@jackgregory
jackgregory / functions.php
Last active August 29, 2015 14:01
WooCommerce - Add additional taxonomies to product related posts
add_filter( 'woocommerce_product_related_posts_query', 'product_related_posts_query' );
function product_related_posts_query( $query ) {
global $product, $wpdb;
if ( is_object( $product ) ) {
$terms_array = array(0);
// get brands
$terms = wp_get_post_terms( $product->id, 'product_brand' );
@jackgregory
jackgregory / gist:c9912143cc4a8a13f4e5
Last active August 29, 2015 14:04
WooCommerce - Add a low stock note to cart items
add_filter( 'woocommerce_cart_item_name', 'cart_low_stock_note', 10, 3 );
function cart_low_stock_note( $title, $cart_item, $cart_item_key ) {
if( $cart_item ) {
$_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
$output = '';
$output.= $title;
// threshold
if( $_product->get_stock_quantity() <= 4 ) {
@jackgregory
jackgregory / sitelink-search.liquid
Created November 1, 2014 16:27
Search in Sitelinks Shopify Snippet
@jackgregory
jackgregory / gist:a9f30668c022e8530185
Last active February 16, 2017 11:45
WooCommerce Search by SKU
add_filter( 'posts_where', 'search_by_sku' );
function search_by_sku( $where ) {
if( ! is_search() ) {
return $where;
}
global $wpdb;
$query = get_search_query();
@jackgregory
jackgregory / gist:2ef8e0ddc3af61af0a6b
Last active October 26, 2016 15:03
WordPress Mysql Search and Replace
UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');
@jackgregory
jackgregory / functions.php
Created April 14, 2015 08:27
Exclude pages from Jetpack top posts widget
/**
* Exclude pages from Jetpack top posts widget
*
* @param array $posts
* @param array $post_ids
* @param int $count
* @return array
*/
add_filter( 'jetpack_widget_get_top_posts', 'exclude_widget_get_top_posts', 10, 3 );
@jackgregory
jackgregory / functions.php
Last active September 27, 2015 16:42
Add pagination to WooCommerce product shortcodes
function c4a8a13f4e5_woocommerce_shortcode_products_query( $query_args, $atts ) {
$query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
return $query_args;
}
add_filter( 'woocommerce_shortcode_products_query', 'c4a8a13f4e5_woocommerce_shortcode_products_query', 10, 2 );
function c4a8a13f4e5_shortcodes_pagination() {
@jackgregory
jackgregory / htaccess
Created October 12, 2015 14:08
Fonts Allow Control Allow Origin
<ifModule mod_headers.c>
Header set Access-Control-Allow-Origin: *
</ifModule>
@jackgregory
jackgregory / functions.php
Created September 25, 2017 10:46
Display user last login time/date
/**
* Capture user login and add it as timestamp in user meta data
*
*/
function 12kdksd212_user_last_login( $user_login, $user ) {
update_user_meta( $user->ID, 'last_login', time() );
}
add_action( 'wp_login', '12kdksd212_user_last_login', 10, 2 );