Skip to content

Instantly share code, notes, and snippets.

View stuartduff's full-sized avatar

Stuart Duff stuartduff

View GitHub Profile
@stuartduff
stuartduff / wc-restrict-email-domain-registration.php
Created August 18, 2021 10:36
WooCommerce Restrict registration to certain email domains
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 February 19, 2024 22:49
WooCommerce Stripe Add "Products" to Stripe Payment metadata
/*
* 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 July 9, 2021 13:13
Increase Storefront recent products homepage to 14
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 June 11, 2021 13:23
Change the pakistani rupe symbol in WooCommerce only on checkout page
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 15:01
This code will send an email to the WooCommerce store owner for Pending Payment orders
// 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 15:09
This code snippet for WooCommerce Subscriptions will only allow a single purchased subscription across all subscription products for customers.
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-homepage-all-product-categories.php
Created April 26, 2021 11:03
Displays all product categoris on the homepage of Storefront
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 April 23, 2021 15:12
This code snippet will remove the word From: in the WooCommerce Bookings price display
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 / remove-billing-email-field..php
Created April 8, 2021 14:38
Remove billing email field from WooCommerce
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 April 5, 2021 10:53
Remove Add To Cart Buttons From WooCommerce Product Archives
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');