Skip to content

Instantly share code, notes, and snippets.

View mikejolley's full-sized avatar

Mike Jolley mikejolley

View GitHub Profile
@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 / gist:9e4f64ebb63266b79a1e
Created March 8, 2016 15:44
One off category to job_listing_category conversion
if ( taxonomy_exists( 'job_listing_category' ) ) {
$categories = get_terms( 'category' );
foreach ( $categories as $category ) {
if ( ! term_exists( $category->name, 'job_listing_category' ) ) {
wp_insert_term( $category->name, 'job_listing_category' );
}
}
}
@mikejolley
mikejolley / functions.php
Last active May 17, 2021 13:51
WooCommerce - Split shipping class items into a new package and limit shipping methods
/**
* This function loops over cart items, and moves any item with shipping class 'special-class' into a new package.
* The new package in this example only takes flat rate shipping.
*/
function split_special_shipping_class_items( $packages ) {
$found_item = false;
$special_class = 'special-class'; // edit this with the slug of your shippig class
$new_package = current( $packages );
$new_package['contents'] = array();
$new_package['contents_cost'] = 0;
@mikejolley
mikejolley / functions.php
Last active July 31, 2017 12:59 — forked from jameskoster/functions.php
WooCommerce - Adjust the quantity incrementer
// Simple products
add_filter( 'woocommerce_quantity_input_args', 'jk_woocommerce_quantity_input_args', 10, 2 );
function jk_woocommerce_quantity_input_args( $args, $product ) {
if ( is_singular( 'product' ) ) {
$args['input_value'] = 2; // Starting value (we only want to affect product pages, not cart)
}
$args['max_value'] = 80; // Maximum value
$args['min_value'] = 2; // Minimum value
$args['step'] = 2; // Quantity steps
add_shortcode( 'custom_product_price', 'custom_product_price_shortcode' );
function custom_product_price_shortcode( $atts ) {
if ( empty( $atts ) ) {
return '';
}
if ( isset( $atts['id'] ) ) {
$product = wc_get_product( $atts['id'] );
return $product->get_price_html();
@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:e73f9d061aaebd25ccdc
Created February 22, 2016 13:07
WooCommerce - Remove subtotal row.
add_filter( 'woocommerce_get_order_item_totals', 'adjust_woocommerce_get_order_item_totals' );
function adjust_woocommerce_get_order_item_totals( $totals ) {
unset($totals['cart_subtotal'] );
return $totals;
}
@mikejolley
mikejolley / gist:5511eb1fddace7606193
Created February 22, 2016 10:34
Simple Global Attribute Size Switcher for WooCommerce Variable Products
<?php
/**
* Show the size switcher.
*/
function size_switcher_display() {
global $product;
echo '<div class="wc-size-switcher">';
echo __( 'Display sizes as:', 'woocommerce-size-switcher' ) . ' ';
@mikejolley
mikejolley / gist:57034501e74ff552dce9
Created February 15, 2016 11:56
Show category name in shop loops
// code added to theme functions.php file
add_action( 'woocommerce_after_shop_loop_item', 'show_product_category_name' );
function show_product_category_name() {
global $post;
echo get_the_term_list( $post->ID, 'product_cat' );
}
@mikejolley
mikejolley / gist:79f12043c18866f46fc6
Last active February 18, 2016 12:04
Restrict resume file allowed mime types
add_filter( 'submit_resume_form_fields', 'alter_allowed_mime_types' );
function alter_allowed_mime_types( $fields ) {
if ( ! empty( $fields['resume_fields']['resume_file'] ) ) {
$fields['resume_fields']['resume_file']['allowed_file_types'] = array(
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'pdf' => 'application/pdf',
);
}
return $fields;