Skip to content

Instantly share code, notes, and snippets.

View barryhughes's full-sized avatar
🇨🇦

Barry Hughes barryhughes

🇨🇦
  • Automattic
  • Vancouver Island, Canada
View GitHub Profile
@barryhughes
barryhughes / simple-woocommerce-product-generator.php
Created September 11, 2025 18:01
Simple example of a generator-based approach to iterating across the (WooCommerce) product catalog.
<?php
/**
* @return Generator<WC_Product>
*/
function fetch_all_products(): Generator {
$page = 1;
while ( true ) {
$products = wc_get_products( [
<?php
/**
* For subscription products, overrides the default product price string with
* something custom.
*/
add_filter(
'woocommerce_subscriptions_product_price_string',
function ( string $product_price_string, WC_Product $product ): string {
// Update the criteria as needed to target the correct product(s).
@barryhughes
barryhughes / debug-console-blueprint-conflict.php
Created August 26, 2025 15:20
Solves a conflict between WooCommerce (and its Blueprints package) and WP Console, when invoking `wp shell`.
<?php
/**
* Avoid conflicts between WP Console and WooCommerce.
*
* In the context of WP CLI and specifically the `wp shell` command, we may wish to unhook WooCommerce's CLI integration
* for the Blueprint package to prevent a conflict between its dependencies and those of WP Console.
*
* Relevant versions when this snippet was written:
*
* - WooCommerce 10.1.0
@barryhughes
barryhughes / simple-subscriptions-ui.php
Created June 30, 2025 18:56
Consolidate the subscriptions admin list into the orders admin list (prototype/early POC)
<?php
/**
* Plugin Name: Simple Subscriptions UI
* Description: Simplifies the WooCommerce Subscriptions admin UI. Prototype/experimental.
* Version: 1.0.0
* Text Domain: simple-subscriptions-ui
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*
* @package simple-subscriptions-ui
@barryhughes
barryhughes / subscriptions-script-generate-renewal-orders-for-subscription.php
Last active April 2, 2025 00:15
Generate x renewal orders for an existing subscription.
<?php
/**
* This script can be used to quickly add a stack of renewals
* to an existing subscription. Can be useful when testing
* scalability concerns for WooCommerce Subscriptions.
*/
function generate_renewal_order_for_subscription( $subscription_id ) {
$subscription = new WC_Subscription( $subscription_id );
<?php
/**
* Temporary fix for https://github.com/woocommerce/woocommerce/issues/53057.
*/
add_action(
'after_plugin_row',
function () {
global $wp_list_table;
@barryhughes
barryhughes / prs-merged-since
Created November 14, 2024 23:30
Bash > List all PRs merged since a given commit was tagged (since a given version)
#!/bin/bash
# Attempts to list PRs merged since a given tag was created.
#
# Run from within the repo you are interested in (of course fetch/pull
# down all the latest changes from remote).
#
# Usage: prs-merged-since <tag_name>
# Example: prs-merged-since v1.0.0
@barryhughes
barryhughes / single-product-canonical-redirect.php
Created September 18, 2024 23:40
WooCommerce (9.3.x and earlier): redirect to the canonical single product URL if the category slug is invalid.
<?php
/**
* When the product permalink structure is `product/%product_cat%`, WooCommerce (9.3.x or earlier)
* accepts any value as the `%product_cat%`, which is not always desirable.
*
* This snippet attempts to detect this situation, and will redirect to the canonical URL as needed.
* You can add it as a mu-plugin. For example:
*
* wp-content/mu-plugins/canonical-product-redirect.php
@barryhughes
barryhughes / site-visibility-remove.php
Created September 13, 2024 15:59
WooCommerce > 9.3.1 > Remove "live" (site visiblity) from admin bar
<?php
/**
* Removes the 'site visibility badge' from the WordPress admin bar.
*
* Intended for use with WooCommerce 9.3.1, can be used via a code
* snippet manager or else you may wish to add as a mu-plugin, ie:
*
* wp-content/mu-plugin/disable-site-visibility-badge.php
*/
@barryhughes
barryhughes / wc-rest-api-orders-ids-only.php
Last active September 9, 2024 21:46
WooCommerce > REST API > Orders > IDs Only
<?php
add_filter(
'rest_post_dispatch',
function ( $response, $server, $request ) {
if (
$request->get_route() === '/wc/v3/orders'
&& $request->get_param( '_fields' ) === [ 'id' ]
&& $request->get_param( '_vendor' ) === 'flag'
) {