Skip to content

Instantly share code, notes, and snippets.

View mikejolley's full-sized avatar

Mike Jolley mikejolley

View GitHub Profile
@mikejolley
mikejolley / gist:9971101
Created April 4, 2014 09:23
WC customising checkout fields using actions and filters - Lesson 3 snippet 1
/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {
echo '<div id="my_custom_checkout_field"><h2>' . __('My Field') . '</h2>';
woocommerce_form_field( 'my_field_name', array(
@mikejolley
mikejolley / functions.php
Created July 2, 2016 13:21
WooCommerce - Redirect external products offsite (disable single listings)
<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_action( 'template_redirect', 'redirect_external_products' );
function redirect_external_products() {
global $post;
@mikejolley
mikejolley / gist:3969579
Created October 28, 2012 19:28
WooCommerce - Create a coupon via PHP
$coupon_code = 'UNIQUECODE'; // Code
$amount = '10'; // Amount
$discount_type = 'fixed_cart'; // Type: fixed_cart, percent, fixed_product, percent_product
$coupon = array(
'post_title' => $coupon_code,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' => 'shop_coupon'
@mikejolley
mikejolley / gist:1622323
Created January 16, 2012 18:54
WooCommerce - Change default catalog sort order
/**
* This code should be added to functions.php of your theme
**/
add_filter('woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby');
function custom_default_catalog_orderby() {
return 'date'; // Can also use title and price
}
@mikejolley
mikejolley / gist:23db488e7cd753c2c066
Created November 5, 2014 02:05
ShipStation Custom Fields
// Add this code to your theme functions.php file or a custom plugin
add_filter( 'woocommerce_shipstation_export_custom_field_2', 'shipstation_custom_field_2' );
function shipstation_custom_field_2() {
return '_meta_key'; // Replace this with the key of your custom field
}
// This is for custom field 3
add_filter( 'woocommerce_shipstation_export_custom_field_3', 'shipstation_custom_field_3' );
@mikejolley
mikejolley / functions.php
Created March 10, 2016 09:39
WooCommerce Disable guest checkout for certain products
<?php
// Code goes in theme functions.php or a custom plugin
add_filter( 'pre_option_woocommerce_enable_guest_checkout', 'conditional_guest_checkout_based_on_product' );
function conditional_guest_checkout_based_on_product( $value ) {
$restrict_ids = array( 1, 2, 3 ); // Replace with product ids which cannot use guest checkout
if ( WC()->cart ) {
$cart = WC()->cart->get_cart();
@mikejolley
mikejolley / functions.php
Last active June 19, 2023 03:10
WooCommerce - Show quantity inputs for simple products within loops.
<?php
/**
* Code should be placed in your theme functions.php file.
*/
add_filter( 'woocommerce_loop_add_to_cart_link', 'quantity_inputs_for_woocommerce_loop_add_to_cart_link', 10, 2 );
function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="cart" method="post" enctype="multipart/form-data">';
$html .= woocommerce_quantity_input( array(), $product, false );
@mikejolley
mikejolley / gist:3b37b9cc19a774665f31
Last active April 21, 2023 12:44
Example instance based shipping method
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Sample instance based method.
*/
class WC_Shipping_Test_Method extends WC_Shipping_Method {
@mikejolley
mikejolley / gist:9f5adb8d194d7681e7b7
Last active April 18, 2023 22:49 — forked from corsonr/gist:6681929
WooCommerce - Create a product categories dropdown list in a shortcode
<?php
/**
* WooCommerce Extra Feature
* --------------------------
*
* Register a shortcode that creates a product categories dropdown list
*
* Use: [product_categories_dropdown orderby="title" count="0" hierarchical="0"]
*/
@mikejolley
mikejolley / gist:d4c46102d69d4560abb6
Created March 21, 2016 11:13
WooCommerce - Allow non-admin access to backend, and enable admin_bar
/* Code goes in theme functions.php */
add_filter( 'woocommerce_prevent_admin_access', '__return_false' );
add_filter( 'woocommerce_disable_admin_bar', '__return_false' );