Skip to content

Instantly share code, notes, and snippets.

@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 );
@justinstern
justinstern / wc-cogs-product-page-cost.php
Created September 3, 2013 23:09
Render the product cost on the product page for simple products
<?php
add_action( 'woocommerce_single_product_summary', 'show_product_cost', 15 );
function show_product_cost() {
global $product;
$item_cost = get_post_meta( $product->id, '_wc_cog_cost', true );
@justinstern
justinstern / wc-pdf-product-vouchers-custom-fields.php
Last active May 2, 2017 20:40
Sample code demonstrating how to create custom voucher fields for with the WooCommerce PDF Product Vouchers plugin
<?php
// Put the following in your theme's functions.php for instance:
add_filter( 'wc_pdf_product_vouchers_voucher_fields', 'wc_pdf_product_vouchers_add_custom_fields', 10, 2 );
/**
* Adds some custom fields to the given product voucher
*
@justinstern
justinstern / wc-subs-test-handle.php
Created October 30, 2013 03:25
Subscriptions test handler code. Adds a "Renew Subscription" button to the Admin Edit Order page for orders containing a subscription product, which when clicked causes the subscriptions renewal code to be fired. This can be added to functions.php
<?php
add_action( 'woocommerce_order_actions_start', function() {
global $post_id;
$order = new WC_Order( $post_id );
if ( class_exists( 'WC_Subscriptions_Order' ) && WC_Subscriptions_Order::order_contains_subscription( $order ) ) {
@justinstern
justinstern / wc-elavon-vm-custom-params.php
Created November 12, 2013 22:39
Sample code for adding a custom parameter to the WooCommerce Elavon VM Payment Gateway transaction request. This is most useful when a custom field is required on an account. Simply add the following to the bottom of your theme's functions.php, including whatever custom parameters your account setup requires
<?php
add_filter( 'wc_payment_gateway_elavon_vm_request_xml', 'wc_payment_gateway_elavon_vm_add_custom_fields', 10, 2 );
function wc_payment_gateway_elavon_vm_add_custom_fields( $request_xml, $request ) {
$request_xml->addChild( 'ssl_name_on_card', str_replace( array( '&', '<', '>' ), '', $request->ssl_first_name . ' ' . $request->ssl_last_name ) );
return $request_xml;
}
@justinstern
justinstern / wc-catalog-lightbox.php
Created November 25, 2013 04:02
Display a lightbox of the product catalog image, rather than linking to the product page. Drop this code snippet into the bottom of your theme's functions.php file. Implementation note: at line 25 we're stripping the image dimensions out of the original image source in order to try and display the full sized image. So if the catalog thumbnail is…
<?php
// Code to display catalog images in a lightbox follows:
add_action( 'wp_enqueue_scripts', 'frontend_scripts_include_lightbox' );
function frontend_scripts_include_lightbox() {
global $woocommerce;
if ( is_shop() || is_product_category() ) {