Skip to content

Instantly share code, notes, and snippets.

@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-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() ) {
@justinstern
justinstern / wc-pdf-product-vouchers-voucher-number-on-admin-email.php
Created December 5, 2013 23:06
Display any voucher numbers from voucher items in the order on the New Order admin email. To use this snippet simply paste into the bottom of your theme's functions.php
<?php
// Displays any voucher numbers on the New Order admin email
add_action( 'woocommerce_email_order_meta', function( $order, $is_admin ) {
if ( $is_admin ) {
$voucher_numbers = array();
$order_items = $order->get_items();
@justinstern
justinstern / wc-measurement-price-calculator-hide-prices.php
Created May 23, 2014 16:17
WC Measurement Price Calculator customization to hide the product price per unit http://www.woothemes.com/products/measurement-price-calculator/
<?php
// Add the following to the bottom of your current theme's functions.php
add_filter( 'woocommerce_sale_price_html', 'remove_price_per_unit_html', 15, 2 );
add_filter( 'woocommerce_price_html', 'remove_price_per_unit_html', 15, 2 );
add_filter( 'woocommerce_empty_price_html', 'remove_price_per_unit_html', 15, 2 );
add_filter( 'woocommerce_variable_sale_price_html', 'remove_price_per_unit_html', 15, 2 );
add_filter( 'woocommerce_variable_price_html', 'remove_price_per_unit_html', 15, 2 );
add_filter( 'woocommerce_variable_empty_price_html', 'remove_price_per_unit_html', 15, 2 );
@justinstern
justinstern / wc-tab-manager-category-tab.php
Created December 27, 2013 05:40
Sample code for the WooCommerce Tab Manager plugin to conditionally display a tab based on the product category. To use this code, first go to WooCommerce > Tab Manager > Add Global Tab and create a new global tab, which for this example we'll name "Sample Tab". "Sample Tab" will now show up for every product that uses the global tab layout. Let…
<?php
add_filter( 'wc_tab_manager_product_tabs', 'wc_tab_manager_product_tabs' );
function wc_tab_manager_product_tabs( $tabs ) {
global $product;
foreach ( $tabs as $tab_name => $tab ) {
@justinstern
justinstern / functions.php
Last active July 22, 2016 18:03
Include featured images in WordPress RSS feed post entries
// add everything below this point to the bottom of your current theme's functions.php
function prepend_thumbnail_rss( $content ) {
global $post;
if ( has_post_thumbnail( $post->ID ) ) {
$content = get_the_post_thumbnail( $post->ID, 'full' ) . $content;
}
return $content;
}
add_filter( 'the_excerpt_rss', 'prepend_thumbnail_rss' );
add_filter( 'the_content_feed', 'prepend_thumbnail_rss' );
@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
*
<?php
// add the following to the bottom of your functions.php
// assumes a copy of the core class-wc-shortcodes.php, customized, is available at path/to/custom/class-wc-shortcodes.php
add_action( 'woocommerce_loaded', 'use_custom_wc_shortcodes' );
function use_custom_wc_shortcodes() {
// load our custom shortcodes class