Skip to content

Instantly share code, notes, and snippets.

@justinstern
justinstern / wc-admin-custom-order-fields-dynamic-options.php
Created February 7, 2014 04:09
An example of using the WooCommerce Admin Custom Order Fields 'wc_admin_custom_order_field_options' filter to return a dynamic set of options. In this sample we're pulling all WordPress terms, for the field which we have named "From Database". Options could be pulled from any other database/table, remote service, or anywhere! To use this code, c…
<?php
/*
* Returns a set of admin custom order field options from the database, rather
* than as configured by the admin
*/
add_filter( 'wc_admin_custom_order_field_options', function( $options, $field ) {
global $wpdb;
@justinstern
justinstern / wc-flat-rate-custom-label.php
Created March 11, 2014 05:43
Put this code snippet at the bottom of your theme's functions.php to modify the flat rate shipping label from (Free) to (will be calculated)
<?php
add_filter( 'woocommerce_cart_shipping_method_full_label', 'woocommerce_flat_rate_custom_label', 10, 2 );
function woocommerce_flat_rate_custom_label( $label, $method ) {
if ( 'flat_rate' == $method->id && 0 == $method->cost ) {
$label = $method->label . ' (will be calculated)';
}
return $label;
}
@justinstern
justinstern / wc-tab-manager-global-tab-product-specific-content.php
Last active August 29, 2015 13:59
Sample code to demonstrate the display of product-specific content for a global tab with the WooCommerce Tab Manager plugin http://www.woothemes.com/products/woocommerce-tab-manager/ The content is taken from a Custom Field named 'product_specifications_tab_content' added to the product. This assumes that the product is using the Global Tab Layo…
<?php
// Add everything below to your current theme's functions.php file
add_filter( 'woocommerce_tab_manager_tab_panel_content', 'wc_tab_manager_global_tab_product_specific_content', 10, 3 );
function wc_tab_manager_global_tab_product_specific_content( $content, $tab, $product ) {
// tab by title slug
if ( 'product-specifications' == $tab['name'] ) {
@justinstern
justinstern / functions.php
Created June 10, 2014 04:04
Snippet to automatically assign a invoice number to an order when payment is successfully completed http://www.woothemes.com/products/print-invoices-packing-lists/
<?php
// Add the following to the bottom of your theme's functions.php
add_action( 'woocommerce_payment_complete', 'set_pip_invoice_number' );
function set_pip_invoice_number( $order_id ) {
$order = new WC_Order( $order_id );
if ( in_array( $order->status, array( 'processing', 'completed' ) ) && function_exists( 'woocommerce_pip_invoice_number' ) ) {
woocommerce_pip_invoice_number( $order_id );
@justinstern
justinstern / functions.php
Last active August 29, 2015 14:05
WooCommerce PDF Product Vouchers: Display any voucher numbers on the order emails
<?php
// Add the following to the end of your theme's functions.php
// Add the following to the end of your theme's functions.php
add_action( 'woocommerce_email_order_meta', 'wc_pdf_product_vouchers_email_voucher_numbers', 10, 3 );
function wc_pdf_product_vouchers_email_voucher_numbers( $order, $sent_to_admin, $plain_text ) {
if ( class_exists( 'WC_PDF_Product_Vouchers_Order' ) ) {
$vouchers = WC_PDF_Product_Vouchers_Order::get_vouchers( $order );
@justinstern
justinstern / wc-subcategory-thumbnail.php
Last active December 13, 2015 20:39
Pluggable function to only display images for parent categories
<?php
function woocommerce_subcategory_thumbnail( $category ) {
global $woocommerce;
// Modification: don't show the image for subcategories
if ( $category->parent ) return;
$small_thumbnail_size = apply_filters( 'single_product_small_thumbnail_size', 'shop_catalog' );
$image_width = $woocommerce->get_image_size( 'shop_catalog_image_width' );
$image_height = $woocommerce->get_image_size( 'shop_catalog_image_height' );
@justinstern
justinstern / wc-all-symlink-plugins-url.php
Created April 8, 2013 07:50
Allow symlinked plugins to function with WooCommerce plugin_url() call: issue #2859: https://github.com/woothemes/woocommerce/issues/2859
@justinstern
justinstern / wc-measurement-price-calculator-custom-unit.php
Last active December 17, 2015 15:29
This demonstrates how you would setup a new hypothetical weight unit named XX which is equal to 5000 lbs. The new unit is added to the woocommerce admin, and then conversions are provided to the weight standard unit of 'lbs', as well as the conversion from 'XX' to lbs. This approach could be adapted to create new units for the other measurement …
<?php
add_filter( 'woocommerce_catalog_settings', 'add_woocommerce_weight_unit_xx' );
/**
* This adds the new unit to the WooCommerce admin
*/
function add_woocommerce_weight_unit_xx( $settings ) {
foreach ( $settings as &$setting ) {
@justinstern
justinstern / gist:5700683
Last active December 18, 2015 00:59
Hides the product page price per unit for woocommerce measurement price calculator products
<?php
add_filter( 'woocommerce_get_price_html', 'wc_hide_product_page_price_per_unit', 11, 2 );
function wc_hide_product_page_price_per_unit( $price_html, $product ) {
// if this is a product variation, get the parent product which holds the calculator settings
$_product = $product;
if ( isset( $product->variation_id ) && $product->variation_id ) { $_product = WC_Price_Calculator_Product::get_product( $product->id ); }
@justinstern
justinstern / gist:5706113
Created June 4, 2013 13:56
Filter to mark an order as not free if any of the items have a regular price (ie the order is only "free" because of coupons or discounts) for the WooCommerce Sequential Order Numbers plugin
add_filter( 'wc_sequential_order_numbers_is_free_order', 'wc_sequential_order_numbers_is_free_order', 10, 2 );
function wc_sequential_order_numbers_is_free_order( $is_free, $order_id ) {
if ( $is_free ) {
$order = new WC_Order( $order_id );
// lets make sure the items truly are free, and not just on sale, or discounted with a coupon
foreach ( $order->get_items() as $item ) {
$product = $order->get_product_from_item( $item );