Skip to content

Instantly share code, notes, and snippets.

Avatar

Stuart Duff stuartduff

View GitHub Profile
@stuartduff
stuartduff / wc-subscriptions-only-allow-one-subscription.php
Created May 6, 2021
This code snippet for WooCommerce Subscriptions will only allow a single purchased subscription across all subscription products for customers.
View wc-subscriptions-only-allow-one-subscription.php
add_filter('woocommerce_add_to_cart_validation', 'check_num_of_subscriptions', 10, 2);
function check_num_of_subscriptions( $valid, $product_id )
{
$product_to_add = wc_get_product( $product_id );
if ( $product_to_add instanceof WC_Product_Subscription || $product_to_add instanceof WC_Product_Variable_Subscription) {
// alternative use: $has_sub = wcs_user_has_subscription( '', '', 'active' );
if ( has_active_subscription() ) {
@stuartduff
stuartduff / storefront-display-all-homepage-categories
Last active May 2, 2021
Display All Product Categories on Storefront homepage
View storefront-display-all-homepage-categories
/**
* Alters the output of the homepage product categories on the Storefront theme
* Affects the storefront_product_categories_args filter in /inc/storefront-template-functions.php
*/
function sd_display_all_home_product_categories( $args ) {
// Sets the maximum product categories to 50, you can increase this to display more if need be.
$args['limit'] = 50;
@stuartduff
stuartduff / storefront-homepage-all-product-categories.php
Created Apr 26, 2021
Displays all product categoris on the homepage of Storefront
View storefront-homepage-all-product-categories.php
function sd_display_four_home_product_categories( $args ) {
// Displays all product categoris on the homepage of Storefront
$args['limit'] = -1;
// Output
return $args;
}
add_filter( 'storefront_product_categories_args', 'sd_display_four_home_product_categories' );
@stuartduff
stuartduff / wc-remove-bookings-from-price.php
Created Apr 23, 2021
This code snippet will remove the word From: in the WooCommerce Bookings price display
View wc-remove-bookings-from-price.php
function custom_booking_product_price( $price, $product ) {
$target_product_types = array(
'booking'
);
if ( in_array ( $product->product_type, $target_product_types ) ) {
$price = str_replace("From:", "", $price);
return $price;
}
// return normal price
return $price;
@stuartduff
stuartduff / translate.php
Last active Apr 20, 2021
WordPress Translate Text testing Snippet - this snippet when added to a themes functions.php file will let you test if the Localization of a word or text string is taking effect and changing within a theme or plugin without using the translation .MO and .PO files.
View translate.php
function translate_text( $translated ) {
// The first parameter is the original text, the second parameter is the changed text.
$translated = str_ireplace( 'Choose and option', 'Select', $translated );
// Returns the translated text
return $translated;
}
add_filter( 'gettext', 'translate_text' );
@stuartduff
stuartduff / remove-billing-email-field..php
Created Apr 8, 2021
Remove billing email field from WooCommerce
View remove-billing-email-field..php
function custom_override_billing_fields( $fields ) {
unset($fields['billing_email']);
return $fields;
}
add_filter( 'woocommerce_billing_fields' , 'custom_override_billing_fields' );
@stuartduff
stuartduff / remove-add-to-cart-archives.php
Created Apr 5, 2021
Remove Add To Cart Buttons From WooCommerce Product Archives
View remove-add-to-cart-archives.php
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
@stuartduff
stuartduff / wcs-remove-resubscribe.php
Created Feb 24, 2021
Remove resubscribe button for WC Subscriptions
View wcs-remove-resubscribe.php
/**
* Remove the "Change Payment Method" button from the My Subscriptions table.
*
* This isn't actually necessary because @see eg_subscription_payment_method_cannot_be_changed()
* will prevent the button being displayed, however, it is included here as an example of how to
* remove just the button but allow the change payment method process.
*/
function eg_remove_my_subscriptions_button( $actions, $subscription ) {
foreach ( $actions as $action_key => $action ) {
@stuartduff
stuartduff / wc-billing-fields-uk-brexit-regions.php
Last active Feb 18, 2021
Changes the county field to state/region to separate regions in the UK for brexit.
View wc-billing-fields-uk-brexit-regions.php
/**
* Changes the county field to state/region to separate regions in the UK for brexit.
* This can be used to specify specific shipping zones for UK regions, like exluding Northern Ireland from UK shipping.
*/
function custom_woocommerce_uk_states_brexit( $states ) {
$states['GB'] = array(
'ENG' => 'England',
'SCO' => 'Scotland',
@stuartduff
stuartduff / storefront-powerpack-custom-header-element.php
Created Nov 1, 2017
Add a custom text element to the header configurator of Storefront Powerpack
View storefront-powerpack-custom-header-element.php
// Adds the text element to the powerpack header components
function sp_header_components_custom_text( $components ) {
$components['text'] = array(
'title' => __( 'Custom Text', 'storefront-powerpack' ),
'hook' => 'sp_header_custom_text_output'
);
return $components;
}
add_filter( 'sp_header_components', 'sp_header_components_custom_text' );