Skip to content

Instantly share code, notes, and snippets.

Avatar

Yan Knudtskov yanknudtskov

View GitHub Profile
View open-multiple-urls.js
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 Feb 18, 2021
Get WooCommerce Completed Orders by Product Name
View woo-orders-by-product-name.sql
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 Feb 18, 2021
Get WooCommerce Completed Orders by Order IDs
View woo-orders-by-ids.sql
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 Jan 28, 2021
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)
View functions.php
<?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 Nov 25, 2020
Example code to migrate from ACF label based index of choice fields to a value : label based index
View functions.php
<?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 Nov 20, 2020
Add a WooCommerce Checkbox at Checkout to accept privacy policy.
View functions.php
<?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 Nov 5, 2020
A relatively simple way to replace URL for media files in WordPress when running a Staging environment
View functions.php
<?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 Nov 4, 2020
WooCommerce Subscriptions: Also send Subscription Cancelled email to customer
View functions.php
<?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 Nov 4, 2020
Modyfying the WooCommerce Thank You Page Texts
View functions.php
<?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 Sep 13, 2020
Creating a custom WP CLI command
View cli-complete-orders.php
<?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/
*/