Skip to content

Instantly share code, notes, and snippets.

Avatar

Stuart Duff stuartduff

View GitHub Profile
@stuartduff
stuartduff / storefront-header-category-image.php
Created Jun 21, 2022
Add WooCommerce product category image full width under the header of the Storefront theme.
View storefront-header-category-image.php
/**
* Add WooCommerce product category image full width under the header of the Storefront theme.
*/
function woocommerce_category_image() {
if ( is_product_category() ){
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
@stuartduff
stuartduff / wc-add-webp-mime-type-import-support.php
Last active May 5, 2022
Add webp MIME to be supported with the default WooCommerce Product Importer
View wc-add-webp-mime-type-import-support.php
function add_additonal_wc_mime_types($mime_types) {
$mime_types['webp'] = 'image/webp'; // Add support for webp images in the WooCommerce Product importer.
return $mime_types;
}
add_filter('woocommerce_rest_allowed_image_mime_types', 'add_additonal_wc_mime_types', 1, 1);
@stuartduff
stuartduff / wc-composite-products-change-select-options-text.php
Created Feb 17, 2022
Change the text for the Select Options button when using Composite Products
View wc-composite-products-change-select-options-text.php
add_filter( 'woocommerce_product_add_to_cart_text', function( $text ) {
global $product;
if ( $product->is_type( 'composite' ) ) {
$text = $product->is_purchasable() ? __( 'Custom options text', 'woocommerce' ) : __( 'Read more', 'woocommerce' );
}
return $text;
}, 10 );
@stuartduff
stuartduff / wc-customer-cancel-payment-url.php
Created Aug 20, 2021
Set a custom return URL for canceled orders on WooCommerce
View wc-customer-cancel-payment-url.php
/* Paypal cancel order redirect */
add_filter( 'woocommerce_get_cancel_order_url_raw', 'paypal_canceled_redirect' );
function paypal_canceled_redirect(){
// Replace the URL below with the one you'd like to return to after a canceled order.
return "https://woocommerce.com";
}
@stuartduff
stuartduff / wc-restrict-email-domain-registration.php
Created Aug 18, 2021
WooCommerce Restrict registration to certain email domains
View wc-restrict-email-domain-registration.php
function is_valid_registration_email_domain( $username, $email, $validation_errors ){
$valid_email_domains = array( 'gmail.com', 'yahoo.com' ); // Add allowed domains here
$valid = false; // sets default validation to false
foreach( $valid_email_domains as $d ){
$d_length = strlen( $d );
$current_email_domain = strtolower( substr( $email, -($d_length), $d_length));
if( $current_email_domain == strtolower($d) ){
$valid = true;
break;
}
@stuartduff
stuartduff / wc-stripe-add-product-metadata.php
Last active Aug 13, 2021
WooCommerce Stripe Add "Products" to Stripe Payment metadata
View wc-stripe-add-product-metadata.php
/*
* Add "Products" to Stripe metadata
*/
function filter_wc_stripe_payment_metadata( $metadata, $order, $source ) {
$count = 1;
foreach( $order->get_items() as $item_id => $line_item ){
$item_data = $line_item->get_data();
$product = $line_item->get_product();
$product_name = $product->get_name();
@stuartduff
stuartduff / sf-increase-recent-products-homepage.pgp
Last active Jul 9, 2021
Increase Storefront recent products homepage to 14
View sf-increase-recent-products-homepage.pgp
function increase_sf_recent_products( $args ) {
// Sets the maximum products to 14
$args['limit'] = 14;
// Output
return $args;
}
function remove_powerpack_filter() {
@stuartduff
stuartduff / wc-change-pakistani-rupee-symbol.php
Last active Jun 11, 2021
Change the pakistani rupe symbol in WooCommerce only on checkout page
View wc-change-pakistani-rupee-symbol.php
add_filter('woocommerce_currency_symbol', 'change_existing_currency_symbol', 10, 2);
function change_existing_currency_symbol( $currency_symbol, $currency ) {
if ( ! is_checkout() ) {
return $currency_symbol;
}
switch( $currency ) {
case 'PKR': $currency_symbol = 'PKR'; break;
@stuartduff
stuartduff / wc-store-onwer-pending-payment-email.php
Created May 26, 2021
This code will send an email to the WooCommerce store owner for Pending Payment orders
View wc-store-onwer-pending-payment-email.php
// New order notification only for "Pending" Order status
add_action( 'woocommerce_checkout_order_processed', 'pending_new_order_notification', 20, 1 );
function pending_new_order_notification( $order_id ) {
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// Only for "pending" order status
if( ! $order->has_status( 'pending' ) ) return;
// Get an instance of the WC_Email_New_Order object
@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() ) {