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 / 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'
) {
<?php
$tests = [
true,
false,
-1,
0,
1,
-5.0,
1.0,
@barryhughes
barryhughes / entity-arb-data-maps.php
Created March 6, 2024 21:23
WooCommerce/dynamic properties: associating arbitrary values with individual entity instances.
<?php
/**
* Simple approach showing a structured approach to associating arbitrary, ephemeral
* (non-persisted) data with individual product objects and other entities.
*
* Benefits include avoidance of collisions between two plugins using the same key,
* the range of checks that can be applied to stored values, etc.
*
* @link https://github.com/woocommerce/woocommerce/issues/45286
*/
@barryhughes
barryhughes / scenarios-for-17355.php
Created January 30, 2024 15:25
Mu-plugin code that continuously assigns (currently unassigned) product attribute terms to products, then creates yet more unassigned product attribute terms. This purposefully uses the WP API and avoids the WC API, as a way to replicate conditions that *might* lead to the problem described in https://github.com/woocommerce/woocommerce/issues/17355
<?php
/**
* Assigns currently unassigned product attribute terms to products, to try and
* replicate the problem in the linked issue. Also generates new unassigned terms
* on each request, to further exacerbate things.
*
* We deliberately do not use the WooCommerce API, because in this case we are
* try to circumvent WC's cache invalidation logic.
*
* @see https://github.com/woocommerce/woocommerce/issues/17355
@barryhughes
barryhughes / detect-legacy-wc-rest-api-usage.php
Last active November 27, 2023 18:11
Detect usage of WooCommerce's legacy REST API. Logs any attempts, and displays an admin notice.
<?php
/**
* Plugin name: Detect Legacy REST API Usage (WooCommerce)
* Description: Attempts to detect and log usage of WooCommerce's legacy REST API.
* Version: 2023-11-24.1
*/
function detect_and_log_wc_legacy_api_requests() {
global $wp;