ūüĎč I'm Mike, a software developer at Automattic, building things with PHP & React, for WordPress & WooCommerce. I write about my hobbies and the projects I work on.

WooCommerce – Hide price suffix when product is not taxable.


<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_filter( 'woocommerce_get_price_suffix', 'custom_woocommerce_get_price_suffix', 10, 2 );
function custom_woocommerce_get_price_suffix( $price_display_suffix, $product ) {
if ( ! $product->is_taxable() ) {
return '';
}
return $price_display_suffix;
}

view raw

functions.php

hosted with ❤ by GitHub

WooCommerce – Redirect external products offsite (disable single listings)


<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_action( 'template_redirect', 'redirect_external_products' );
function redirect_external_products() {
global $post;
if ( is_singular( 'product' ) && ! empty( $post ) && ( $product = wc_get_product( $post ) ) && $product->is_type( 'external' ) ) {
wp_redirect( $product->get_product_url() );
exit;
}
}

view raw

functions.php

hosted with ❤ by GitHub

WooCommerce – remove payment method from emails


<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
*/
add_filter( 'woocommerce_get_order_item_totals', 'custom_woocommerce_get_order_item_totals' );
function custom_woocommerce_get_order_item_totals( $totals ) {
unset( $totals['payment_method'] );
return $totals;
}

view raw

gistfile1.txt

hosted with ❤ by GitHub

WooCommerce – Hide shipping rates when free shipping is available.


<?php
/**
* Hide shipping rates when free shipping is available.
* Updated to support WooCommerce 2.6 Shipping Zones.
*
* @param array $rates Array of rates found for the package.
* @return array
*/
function my_hide_shipping_when_free_is_available( $rates ) {
$free = array();
foreach ( $rates as $rate_id => $rate ) {
if ( 'free_shipping' === $rate->method_id ) {
$free[ $rate_id ] = $rate;
break;
}
}
return ! empty( $free ) ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'my_hide_shipping_when_free_is_available', 100 );

view raw

functions.php

hosted with ❤ by GitHub

WooCommerce – Add all upsells of a product to the cart via custom link


<?php // Do not include this if already open!
/**
* Code goes in theme functions.php.
* Product must have upsells, and this works with simple products only.
* Example link: yoursite.com?add-upsells-to-cart=X, X being your product ID.
*/
add_action( 'wp_loaded', 'bulk_upsell_add_to_cart_action', 20 );
function bulk_upsell_add_to_cart_action() {
if ( ! empty( $_GET['add-upsells-to-cart'] ) ) {
$product_id = absint( $_GET['add-upsells-to-cart'] );
$product = wc_get_product( $product_id );
if ( $product ) {
$upsell_ids = $product->get_upsells();
if ( $upsell_ids ) {
$count = 0;
foreach ( $upsell_ids as $upsell_id ) {
if ( WC()->cart->add_to_cart( $upsell_id ) ) {
$count ++;
}
}
wc_add_notice( sprintf( _n( 'Added %d item to the cart', 'Added %d items to the cart', $count ), $count ) );
}
}
}
}

view raw

gistfile1.txt

hosted with ❤ by GitHub

WooCommerce – enable free shipping only if product class is found in cart.


<?php // Do not include this if already open!
/**
* Code goes in theme functions.php. Free Shipping Method must be enabled.
*/
add_filter( 'woocommerce_shipping_free_shipping_is_available', 'free_shipping_based_on_cart_shipping_class' );
function free_shipping_based_on_cart_shipping_class( $is_available ) {
/**
* This example enables free shipping only when an item is found in the cart with a class named 'free_shipping'
*/
$cart_items = WC()->cart->get_cart();
$found = false;
foreach ( $cart_items as $cart_item ) {
$product = $cart_item['data'];
$class = $product->get_shipping_class();
if ( 'free_shipping' === $class ) {
$found = true;
break;
}
}
return $is_available && $found;
}

view raw

functions.php

hosted with ❤ by GitHub

WooCommerce – Remove product data tabs and hook content in sequence instead


<?php // Do not include this if already open!
/**
* Remove existing tabs from single product pages.
*/
function remove_woocommerce_product_tabs( $tabs ) {
unset( $tabs['description'] );
unset( $tabs['reviews'] );
unset( $tabs['additional_information'] );
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'remove_woocommerce_product_tabs', 98 );
/**
* Hook in each tabs callback function after single content.
*/
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_product_description_tab' );
add_action( 'woocommerce_after_single_product_summary', 'woocommerce_product_additional_information_tab' );
add_action( 'woocommerce_after_single_product_summary', 'comments_template' );

view raw

functions.php

hosted with ❤ by GitHub

Showing quantity inputs in WooCommerce loops

Here is a quick snippet showing how you can add quantity inputs to WooCommerce loops for simple products. This used to be only possible through template edits, but is now doable through filters because they are magic.


<?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 );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
}

view raw

functions.php

hosted with ❤ by GitHub

The end result:

2016-04-21 at 18.21.png

Github to WordPress.org deploy script

Today I polished, documented, and open sourced a bash script I’ve been using to do plugin deployments from Github to¬†WordPress.org (plus I wanted an excuse to use the new WordPress.com desktop app¬†which is¬†awesome by the way).¬†It’s based on code Barry Kooij¬†sent¬†me a while back. You can find it here:

https://github.com/mikejolley/github-to-wordpress-deploy-script

It handles Github tagging, removing junk files, and SVN tagging with minimal effort. It also has a quick checklist to stop you forgetting to set things in the readme.txt like the stable version¬†(I’ve been guilty of forgetting this in the past).

Once downloaded to your machine, the script requires a Github access token, and a few edits to tell it which plugin you’re deploying.

After you’ve¬†added the details specific to your project, you simply need to open it’s parent¬†directory in terminal, and run the command:

sh release.sh

It then prompts you for a version, branch, and handles the deployment process.

demo

The above demo is from a release of WP Job Manager I did today¬†showing it working nicely.¬†Hope it’s useful!

Loading...