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 / woocommerce-802-kill-top-seller-query.php
Created August 23, 2023 17:02
"Nullify" the top seller query, within the WooCommerce Status dashboard widget (might be helpful, until such time as the query results are cached).
<?php
/**
* The top-seller query (used for the WooCommerce Status dashboard widget)
* runs on every dashboard request.
*
* It can effectively be nullified using this snippet, if the performance
* impact is too great.
*
* (Tested with WC 8.0.2)
@barryhughes
barryhughes / woo-store-api.full-variation-data.php
Last active May 12, 2023 13:44
Alter the wp-json/wc/store/v1/products/<ID> response so that it includes 'more complete' information about variations.
<?php
/**
* Modify /wp-json/wc/store/v1/products/<ID> so that it returns more complete data about each variation.
*
* Not extensively tested, just captures an idea about how we might do this.
*/
add_filter( 'rest_request_after_callbacks', function ( $response, $handler, $request ) {
// If this is not a Store API product request, do not interfere.
if ( ! str_starts_with( $request->get_route(), '/wc/store/v1/products/' ) || $request->get_method() !== 'GET' ) {
@barryhughes
barryhughes / pr37847-alt__shipping-flat-rate-class-handling.diff
Created May 10, 2023 23:48
A possible alternative to the initial solution in WooCommerce PR#37847 (relates to this comment: https://github.com/woocommerce/woocommerce/pull/37847#issuecomment-1542940612)
diff --git a/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php b/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php
index 4b140a814f..5719a8a12b 100644
--- a/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php
+++ b/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php
@@ -177,8 +177,16 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
// Also handles BW compatibility when slugs were used instead of ids.
$shipping_class_term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' );
$class_cost_string = $shipping_class_term && $shipping_class_term->term_id ? $this->get_option( 'class_cost_' . $shipping_class_term->term_id, $this->get_option( 'class_cost_' . $shipping_class, '' ) ) : $this->get_option( 'no_class_cost', '' );
-
- if ( '' === $class_cost_string ) {
@barryhughes
barryhughes / as-update-callback-cleanup.sql
Last active December 21, 2022 20:27
Cleanup `woocommerce_run_update_callback` actions.
-- Cleans up any non-pending WooCommerce Update Callback scheduled actions
-- and the corresponding log table entries.
--
-- Note that in many cases you may need to adjust the table prefix from
-- 'wp_' to whatever is in effect within your site.
DELETE actions,
action_logs
FROM wp_actionscheduler_actions AS actions
JOIN wp_actionscheduler_logs AS action_logs
@barryhughes
barryhughes / as-cleanup.sql
Created December 21, 2022 20:18
Delete non-pending actions and their logs (Action Scheduler)
DELETE actions,
action_logs
FROM wp_actionscheduler_actions AS actions
JOIN wp_actionscheduler_logs AS action_logs
ON actions.action_id = action_logs.action_id
WHERE actions.hook = 'woocommerce_run_update_callback'
AND actions.status <> 'pending'
@barryhughes
barryhughes / jokes-on-a-timer.sh
Created April 22, 2021 19:11
Print a fresh joke every 2 mins (CLI\Bash\Zsh)
# Every 2 mins (`-n 120` where 120 is num seconds), display a new joke.
#
# - On MacOS you may need to `brew install watch` first of all.
# - Fun to place this in a small Tmux pane
watch -n 120 "curl -s -H \"Accept: text/plain\" https://icanhazdadjoke.com/"
@barryhughes
barryhughes / wp-cli-as-generate-actions.php
Last active April 20, 2021 18:12
Handy dandy testing tool; used to generate fake scheduled actions [WP CLI | Action Scheduler]
<?php
/**
* Generates a set of scheduled actions for testing purposes.
*
* Additionally, it provides test callbacks (which do very little except burn time).
* How much fun is that?
*
* An easy way to add this command is to place the file in the wp-content/mu-plugins
* directory, or else incldue it from a file in that directory.
*
@barryhughes
barryhughes / query-monitor-for-logged-out-users.php
Created April 7, 2021 20:30
Make Query Monitor available for logged out users. Useful alternative when Query Monitor's own authentication cookie-based support for the same thing is not enough.
<?php
/**
* Makes Query Monitor output available to all users.
*
* This can, for example, be added to mu-plugins/query-monitor.php (and can
* easily be disabled by changing 'init' to 'xinit', or commenting the whole
* thing out, or adding a return statement, etc).
*
* Reasons you might use this instead of simply setting Query Monitor's own
-- Delete WooCommerce session data for a specific user.
DELETE session_data,
session_meta
FROM wp_woocommerce_sessions AS session_data
JOIN wp_usermeta AS session_meta ON
session_meta.user_id = session_data.session_key
-- Set the `session_key` value to the target `user_id`.
@barryhughes
barryhughes / random-fact-string.php
Created March 2, 2021 18:16
Get a random fact.
<?php
/**
* Get a string containing a random fact.
*
* If a random fact cannot be fetched from the external service, a default is
* supplied instead.
*
* @return string
*/