Skip to content

Instantly share code, notes, and snippets.

View yanknudtskov's full-sized avatar

Yan Knudtskov yanknudtskov

View GitHub Profile
var urls = [];
var selector = "span.run > a";
var base_url = "https://baseurl.com";
jQuery( selector ).each( function() {
urls.push(base_url + jQuery(this).attr('href'));
});
for (let i = 0; i < urls.length; i++) {
@yanknudtskov
yanknudtskov / woo-orders-by-product-name.sql
Created February 18, 2021 21:42
Get WooCommerce Completed Orders by Product Name
select
p.ID as order_id,
p.post_date,
max( CASE WHEN pm.meta_key = '_billing_email' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_email,
max( CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_first_name,
max( CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_last_name,
max( CASE WHEN pm.meta_key = '_billing_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_1,
max( CASE WHEN pm.meta_key = '_billing_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_2,
max( CASE WHEN pm.meta_key = '_billing_city' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_city,
max( CASE WHEN pm.meta_key = '_billing_state' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_state,
@yanknudtskov
yanknudtskov / woo-orders-by-ids.sql
Created February 18, 2021 21:40
Get WooCommerce Completed Orders by Order IDs
select
p.ID as order_id,
p.post_date,
max( CASE WHEN pm.meta_key = '_billing_email' and p.ID = pm.post_id THEN pm.meta_value END ) as billing_email,
max( CASE WHEN pm.meta_key = '_billing_first_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_first_name,
max( CASE WHEN pm.meta_key = '_billing_last_name' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_last_name,
max( CASE WHEN pm.meta_key = '_billing_address_1' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_1,
max( CASE WHEN pm.meta_key = '_billing_address_2' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_address_2,
max( CASE WHEN pm.meta_key = '_billing_city' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_city,
max( CASE WHEN pm.meta_key = '_billing_state' and p.ID = pm.post_id THEN pm.meta_value END ) as _billing_state,
@yanknudtskov
yanknudtskov / functions.php
Created January 28, 2021 10:00
HTML cutter when you want to preserver formatting and avoid strip_tags and still be able to grab a substr (which can cut off HTML-tags)
<?php
function yanco_html_cut($text, $max_length)
{
$tags = array();
$result = "";
$is_open = false;
$grab_open = false;
$is_close = false;
@yanknudtskov
yanknudtskov / functions.php
Created November 25, 2020 13:12
Example code to migrate from ACF label based index of choice fields to a value : label based index
<?php
add_shortcode( 'migrate_acf_choice_data', 'migrate_acf_choice_data' );
function migrate_acf_choice_data() {
global $wpdb;
$meta_key_to_migrate = ''; // Set the name of the acf_field to migrate the data for
$dry_run = true; // Set to false to enable live run
$html = '';
@yanknudtskov
yanknudtskov / functions.php
Created November 20, 2020 11:37
Add a WooCommerce Checkbox at Checkout to accept privacy policy.
<?php
/**
* Add privacy policy tick box at checkout
*/
add_action( 'woocommerce_review_order_before_submit', 'yanco_add_checkout_privacy_policy', 9 );
function yanco_add_checkout_privacy_policy() {
woocommerce_form_field( 'privacy_policy', array(
'type' => 'checkbox',
'class' => array('form-row privacy'),
@yanknudtskov
yanknudtskov / functions.php
Created November 5, 2020 20:37
A relatively simple way to replace URL for media files in WordPress when running a Staging environment
<?php
define( 'STAGING_URL', 'https://staging.mysiteurl.com');
define( 'PRODUCTION_URL', 'https://www.mysiteurl.com');
add_filter( 'wp_get_attachment_url', 'yanco_staging_wp_get_attachment_url', 1000000, 2 );
function yanco_staging_wp_get_attachment_url( $url, $attachment )
{
if( strpos( $url, STAGING_URL ) !== false ) {
$url = str_replace( STAGING_URL, PRODUCTION_URL, $url );
@yanknudtskov
yanknudtskov / functions.php
Created November 4, 2020 12:00
WooCommerce Subscriptions: Also send Subscription Cancelled email to customer
<?php
add_action('woocommerce_subscription_status_pending-cancel', 'yanco_woocommerce_subscription_status_pending_cancel', 10, 3 );
function yanco_woocommerce_subscription_status_pending_cancel( $subscription ) {
$customer_email = $subscription->get_billing_email();
$wc_emails = WC()->mailer()->get_emails();
$admin_email = $wc_emails['WCS_Email_Cancelled_Subscription']->recipient;
$wc_emails['WCS_Email_Cancelled_Subscription']->trigger( $subscription );
@yanknudtskov
yanknudtskov / functions.php
Created November 4, 2020 09:14
Modyfying the WooCommerce Thank You Page Texts
<?php
add_filter( 'the_title', 'woo_personalize_order_received_title', 10, 2 );
function woo_personalize_order_received_title( $title, $id ) {
if ( is_order_received_page() && get_the_ID() === $id ) {
global $wp;
// Get the order. Line 9 to 17 are present in order_received() in includes/shortcodes/class-wc-shortcode-checkout.php file
$order_id = apply_filters( 'woocommerce_thankyou_order_id', absint( $wp->query_vars['order-received'] ) );
$order_key = apply_filters( 'woocommerce_thankyou_order_key', empty( $_GET['key'] ) ? '' : wc_clean( $_GET['key'] ) );
@yanknudtskov
yanknudtskov / cli-complete-orders.php
Created September 13, 2020 15:40
Creating a custom WP CLI command
<?php
/*
Plugin Name: Yan&Co complete orders CLI command
Plugin URI: https://www.yanco.dk/
Description: Complete virtual orders CLI command
Author: Yan&Co
Version: 1.0.0
Author URI: https://www.yanco.dk/
*/