Skip to content

Instantly share code, notes, and snippets.

View brycehamrick's full-sized avatar

Bryce Hamrick brycehamrick

View GitHub Profile
@brycehamrick
brycehamrick / woocommerce-subscription-export.sql
Last active April 12, 2024 13:07
woocommerce-subscription-export
/***********************************
* This query is intended to retrieve all necessary
* columns to migrate to Recharge as described here:
* https://support.rechargepayments.com/hc/en-us/articles/360008683974-Migration-template-explained
************************************/
SELECT
p.ID AS subscription_id,
oi.order_item_name AS external_product_name,
5e94c20e0c7675cd693236944cb5a67e344ba9b6d6c0b456324ee4fed3f94af75ea532f98dcc9d5ddfbbd38a3abc997759fad8c57b4e5f993212b51866246ffe
@brycehamrick
brycehamrick / orders-with-tracking.sql
Created February 19, 2019 18:13
WooCommerce Orders with Tracking Number
SELECT o.ID, fn.meta_value as first_name, ln.meta_value as last_name,
SUBSTRING_INDEX(SUBSTRING_INDEX(t.meta_value,';',9),':',-1) AS tracking
FROM `wp_posts` o
JOIN `wp_postmeta` t ON t.post_id = o.ID
JOIN `wp_postmeta` fn ON fn.post_id = o.ID
JOIN `wp_postmeta` ln ON ln.post_id = o.ID
WHERE
o.`post_type` = 'shop_order'
AND
t.`meta_key` = '_wc_shipment_tracking_items'
@brycehamrick
brycehamrick / orders-with-product.sql
Created January 25, 2019 16:27
WooCommerce Select Orders + Meta that contain product
SELECT
order_items.order_id,
posts.post_status,
max(
CASE
WHEN
pm.meta_key = '_shipping_first_name'
and order_items.order_id = pm.post_id
THEN
pm.meta_value
@brycehamrick
brycehamrick / get-discontinued-subscriptions.sql
Created July 25, 2018 16:19
Query to get a list of WooCommerce subscriptions that include discontinued products.
SELECT o.ID AS order_id, oim.meta_value AS product_id, p.post_title AS product_name
FROM wp_woocommerce_order_itemmeta oim
JOIN wp_woocommerce_order_items oi ON oim.order_item_id = oi.order_item_id
JOIN wp_posts o ON oi.order_id = o.ID
JOIN wp_posts p ON oim.meta_value = p.ID
JOIN wp_postmeta pm ON p.ID = pm.post_id
WHERE oim.meta_key = '_product_id'
AND o.post_type = 'shop_subscription'
AND pm.meta_key = '_is_discontinued'
AND pm.meta_value = 'yes'
@brycehamrick
brycehamrick / get-products.sql
Last active January 1, 2019 11:43
Products in category including stock count and _is_discontinued
SELECT
/** Post ID **/
p.id,
/** Product Code **/
mt.meta_value AS sku,
/** Product Stock **/
m.meta_value AS stock,
/** Item Title **/
p.post_title AS title,
/** Item Price **/
Verifying my Blockstack ID is secured with the address 142jtxEBrxXdDFKVYCZa949RoggN5sf82z https://explorer.blockstack.org/address/142jtxEBrxXdDFKVYCZa949RoggN5sf82z
@brycehamrick
brycehamrick / get-orders.sql
Last active July 24, 2018 02:42 — forked from lukecav/Command
Sales by SKU using a MySQL query if using WooCommerce
SELECT
sku, count(1)
FROM
wp_woocommerce_order_items oi, -- orders table
wp_posts p, -- woocommerce use posts to keep metadata on the order
wp_postmeta pm, -- we use this table to filter the completed orders
wp_postmeta pm1, -- we use this table again for getting the shipping country info
wp_woocommerce_order_itemmeta oim, -- use this table to get product ids from orders
(SELECT p.id as product_id, meta_value as sku FROM wp_posts p, wp_postmeta pm where
post_type='product' and post_status='publish'
@brycehamrick
brycehamrick / woocommerce-disable-guest-for-virtual.php
Created February 19, 2018 06:26
Disable guest checkout if virtual products are in the cart.
<?php
add_filter( 'pre_option_woocommerce_enable_guest_checkout', 'conditional_guest_checkout_based_on_product' );
function conditional_guest_checkout_based_on_product( $value ) {
if ( WC()->cart ) {
$cart = WC()->cart->get_cart();
foreach ( $cart as $item ) {
$product = $item['data'];
if(!empty($product) && $product->is_virtual()){
$value = "no";
@brycehamrick
brycehamrick / gist:e0b3255a8a15e6ce9ba3e8407f4a5497
Created February 2, 2018 06:26
Adds redirect_to support for the woocommerce my account login form
function filter_woocommerce_login_redirect( $redirect, $user ) {
$new_redirect = (isset($_GET['redirect_to'])) ? $_GET['redirect_to'] : $redirect;
return $new_redirect;
};
add_filter( 'woocommerce_login_redirect', 'filter_woocommerce_login_redirect', 10, 2 );