Skip to content

Instantly share code, notes, and snippets.

@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:2767607
Created May 22, 2012 08:32
WooCommerce - Default shipping SUB-method 1.5.7/1.6+
add_filter( 'woocommerce_shipping_chosen_method', 'custom_shipping_chosen_method', 10, 2 );
function custom_shipping_chosen_method( $chosen_method, $_available_methods ) {
switch ( $_available_methods[0] ) {
case 'table_rate:0-USPS':
$chosen_method = 'table_rate:0-USPS';
break;
case 'table_rate:1-USPS':
$chosen_method = 'table_rate:1-USPS';
break;
@mikejolley
mikejolley / gist:2328155
Created April 7, 2012 11:57
WooCommerce - Custom order number (in 1.5.3+)
add_filter( 'woocommerce_order_number', 'custom_woocommerce_order_number', 1, 2 );
function custom_woocommerce_order_number( $oldnumber, $order ) {
return 'CUSTOMPREFIX' . $order->id;
}
@mikejolley
mikejolley / gist:2263203
Last active June 1, 2019 17:50
WooCommerce - Add custom field (in an order) to the order emails
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_woocommerce_email_order_meta_keys');
function my_woocommerce_email_order_meta_keys( $keys ) {
$keys['How did you hear about us?'] = 'hear_about_us';
return $keys;
}
@mikejolley
mikejolley / gist:2193064
Created March 25, 2012 11:38
WooCommerce - Store customer IP in order meta
// Code goes in functions.php
add_action( 'woocommerce_checkout_update_order_meta', 'wc_store_user_ip' );
function wc_store_user_ip( $order_id ) {
update_post_meta( $order_id, 'Customer IP', $_SERVER['REMOTE_ADDR'] );
}
@mikejolley
mikejolley / gist:2176823
Created March 24, 2012 00:44
WooCommerce - Show products from current product category (when viewing a single product)
<?php
if ( is_singular('product') ) {
global $post;
// get categories
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $cats_array[] = $term->term_id;
@mikejolley
mikejolley / gist:2044101
Last active May 18, 2021 17:02
WooCommerce - Show number of items in cart and total
<a class="cart-contents" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get_cart_contents_count() ); ?> - <?php echo WC()->cart->get_cart_total(); ?></a>
@johnpbloch
johnpbloch / protected-roles.php
Created March 13, 2012 14:07
Define 'protected' WordPress roles that can only be deleted by users of at least one protected role. This allows you to enable clients to create, edit, and delete users without deleting your account.
<?php
class JPB_User_Caps {
/**
* An array of all protected roles
* @var array
*/
protected $protectedRoles = array(
'webmaster',
@mikejolley
mikejolley / gist:1965933
Created March 3, 2012 12:50
WooCommerce payment gateway plugin base
<?php
/*
Plugin Name: WooCommerce <enter name> Gateway
Plugin URI: http://woothemes.com/woocommerce
Description: Extends WooCommerce with an <enter name> gateway.
Version: 1.0
Author: WooThemes
Author URI: http://woothemes.com/
Copyright: © 2009-2011 WooThemes.
@mikejolley
mikejolley / gist:1965842
Created March 3, 2012 12:17
version of email_order_items_table for use outside of the class
<?php
foreach($order->get_items() as $item) :
$_product = $order->get_product_from_item( $item );
$file = $sku = $variation = $image = '';
if ($show_image) :
$src = wp_get_attachment_image_src( get_post_thumbnail_id( $_product->id ), 'thumbnail');
$image = apply_filters('woocommerce_order_product_image', '<img src="'.$src[0].'" alt="Product Image" height="'.$image_size[1].'" width="'.$image_size[0].'" style="vertical-align:middle; margin-right: 10px;" />', $_product);