Skip to content

Instantly share code, notes, and snippets.

View mikejolley's full-sized avatar

Mike Jolley mikejolley

View GitHub Profile
@mikejolley
mikejolley / test-plugin.php
Last active November 4, 2024 01:41
WooCommerce Block Checkout Example to listen for payment method change and add a fee server side
<?php
// Reset payment method in session
add_action('woocommerce_checkout_init', function(){
if( ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || ( defined( 'DOING_AJAX') && DOING_AJAX ) || !WC() || !WC()->session || is_admin() ) {
return;
}
// Get first payment method and store it
// Since payment method selection is not persistent, on page load always the first one will be selected
@mikejolley
mikejolley / functions.php
Last active November 8, 2022 00:25
WooCommerce 3.3 - Hide uncategorized category from the shop page on the frontend
<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_filter( 'woocommerce_product_subcategories_args', 'custom_woocommerce_product_subcategories_args' );
function custom_woocommerce_product_subcategories_args( $args ) {
$args['exclude'] = get_option( 'default_product_cat' );
return $args;
@mikejolley
mikejolley / double-serialized-attributes-fixer.php
Created May 16, 2017 10:34
Small drop-in plugin to fix double-serialized product attributes (they are no longer double-unserialized in WC Core)
<?php
/**
* Plugin Name: Double serialized attributes fixer
* Description: Adds a tool to fix double serialized attributes in WooCommerce.
* Version: 0.0.9
* Author: Mike J
* Requires at least: 4.4
* Tested up to: 4.7
*/
if ( ! defined( 'ABSPATH' ) ) {
@mikejolley
mikejolley / functions.php
Created April 7, 2017 11:34
WooCommerce 3.0 - Disable deferred email sending
<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_filter( 'woocommerce_defer_transactional_emails', '__return_false' );
@mikejolley
mikejolley / functions.php
Created February 10, 2017 14:49
WooCommerce - Notify admin when a new customer account is created
<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_action( 'woocommerce_created_customer', 'woocommerce_created_customer_admin_notification' );
function woocommerce_created_customer_admin_notification( $customer_id ) {
wp_send_new_user_notifications( $customer_id, 'admin' );
}
@mikejolley
mikejolley / functions.php
Created July 21, 2016 08:57
WooCommerce - Hide price suffix when product is not taxable.
<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_filter( 'woocommerce_get_price_suffix', 'custom_woocommerce_get_price_suffix', 10, 2 );
function custom_woocommerce_get_price_suffix( $price_display_suffix, $product ) {
if ( ! $product->is_taxable() ) {
return '';
@mikejolley
mikejolley / functions.php
Created July 2, 2016 13:21
WooCommerce - Redirect external products offsite (disable single listings)
<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_action( 'template_redirect', 'redirect_external_products' );
function redirect_external_products() {
global $post;
@mikejolley
mikejolley / gist:c4606aa52754b334f0f1bbe7e5b5ca6b
Created June 30, 2016 08:13
WooCommerce - remove payment method from emails
<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_filter( 'woocommerce_get_order_item_totals', 'custom_woocommerce_get_order_item_totals' );
function custom_woocommerce_get_order_item_totals( $totals ) {
unset( $totals['payment_method'] );
return $totals;
@mikejolley
mikejolley / functions.php
Last active February 5, 2024 15:33 — forked from claudiosanches/functions.php
WooCommerce - Hide shipping rates when free shipping is available.
<?php
/**
* Hide shipping rates when free shipping is available.
* Updated to support WooCommerce 2.6 Shipping Zones.
*
* @param array $rates Array of rates found for the package.
* @return array
*/
function my_hide_shipping_when_free_is_available( $rates ) {
$free = array();
@mikejolley
mikejolley / gist:cd67b0baf1c3767bb29f76471fea120b
Last active September 13, 2021 20:45
WooCommerce - Add all upsells of a product to the cart via custom link
<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
* Product must have upsells, and this works with simple products only.
* Example link: yoursite.com?add-upsells-to-cart=X, X being your product ID.
*/
add_action( 'wp_loaded', 'bulk_upsell_add_to_cart_action', 20 );
function bulk_upsell_add_to_cart_action() {