This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This script is using Laravel Dusk to take screenshots of a URL, but not in the context of a regular Dusk test. | |
* My Laravel application is taking screenshots of *other* sites, but you could adapt this to a Dusk test taking screenshots of your Laravel site. | |
* 1. Install Dusk https://laravel.com/docs/5.8/dusk#installation | |
* 2. Make sure it works with regular Dusk tests first. | |
* 3. Use the code below for a stand-alone Chrome webdriver instance to do automation things. | |
*/ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var regions = [ | |
"northland" => "Northland", | |
"auckland" => "Auckland", | |
"waikato" => "Waikato", | |
"bay_of_plenty" => "Bay of Plenty", | |
"gisborne" => "Gisborne", | |
"hawkes_bay" => "Hawke's Bay", | |
"taranaki" => "Taranaki", | |
"manawatu_wanganui" => "Manawatu-Wanganui", | |
"wellington" => "Wellington", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//easy way to cover multiple post types, including custom ones | |
foreach(['page','post','custom_post_type'] as $content_type){ | |
add_filter( 'manage_' . $content_type . '_posts_columns', 'mytheme_custom_column' ); | |
add_action( 'manage_' . $content_type . '_posts_custom_column', 'mytheme_custom_column_values', 10, 2); | |
add_filter( 'manage_edit-' . $content_type . '_sortable_columns', 'mytheme_custom_column_sorting' ); | |
} | |
//column label |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//We will use this function to modify prices whenever WooCommerce retrieves them | |
function mytheme_global_sale_price( $price, $product ) { | |
$product_id = $product->get_id(); | |
//do any checks you need to exclude or include products here | |
$excluded_products = [123,678]; | |
if(in_array($product_id, $excluded_products)) return $price; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//When registering styles and scripts in WordPress, you can specify a 'version' parameter which is appended to the file's URL. | |
//This is fine, but it's inconvenient having to bump the version number whenever you make a change. So why not do it automatically? | |
//Note we use get_stylesheet_directory() instead of get_stylesheet_directory_uri(), so we get an absolute file path for filemtime() | |
$mtime = filemtime(get_stylesheet_directory() .'/js/script.js'); | |
wp_register_script( 'theme-js', get_stylesheet_directory_uri() .'/js/script.js', array( 'jquery' ), $mtime, true ); | |
wp_enqueue_script( 'theme-js' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
WooCommerce has a handy feature in the admin UI to "Create variations from all attributes". | |
To do this programmatically (as of WooCommerce v3.7.0) use a modified version of link_all_variations() found in woocommerce/includes/class-wc-ajax.php | |
*/ | |
function myplugin_create_variations($product_id){ | |
wc_maybe_define_constant( 'WC_MAX_LINKED_VARIATIONS', 50 ); | |
wc_set_time_limit( 0 ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Set a default layout to appear in a Flexible Content Field for certain post types (eg products) | |
//We just set a default value for our desired field. This is an array of arrays. | |
//Each inner array requires at minimum an element called 'acf_fc_layout'. You may also set other field default values here (use field keys, not names). | |
function mytheme_set_default_layout($value, $post_id, $field){ | |
if(get_post_type($post_id) == 'product' && empty($value)){ | |
$value = array( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Since 3.x WooCommerce has had an excellent native product import/export. | |
//You can do a huge amount just by tweaking your CSV to load meta values into the correct meta fields for your destination site. | |
//It'll even import images from the source site by URL if possible, and upload them to the destination site. | |
//If you need to do something a little fancier for every product you import, check out the woocommerce_product_import_* hooks here https://woocommerce.github.io/code-reference/hooks/hooks.html | |
//You can modify the CSV data before it's used, and modify the resulting WC_Product during and after creation | |
//eg do something to every product after it's created |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//I found a few tutorials for making custom WooCommerce shipping methods but most were confusing or didn't work any more. | |
//This is a simple custom shipping method. | |
//It originally calculated a fee per 'delivery day' based on some date meta on the cart items, but for this example it's a hard-coded flat fee. | |
//See calculate_shipping() | |
//Step 1: | |
//Save this code (minus these instructions) in a file in your plugins directory, preferably in its own subdirectory eg myplugin_shipping/myplugin_shipping.php | |
//Step 2: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Grab a remote image and import it into WordPress with PHP | |
*/ | |
function mytheme_import_image($image_url){ | |
//$image_url is something like https://www.mysite.com/images/myimage.jpg |