Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View stuartduff's full-sized avatar

Stuart Duff stuartduff

View GitHub Profile
@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 / wc-add-webp-mime-type-import-support.php
Last active February 17, 2024 20:26
Add webp MIME to be supported with the default WooCommerce Product Importer
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-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 / reverse-woocommerce-reviews-order.php
Created December 9, 2015 15:23
This snippet will reverse the display order of the WooCommerce product reviews
// show newest product reviews on top
function sd_newest_reviews_first( $args ) {
$args['reverse_top_level'] = true;
return $args;
}
add_filter( 'woocommerce_product_review_list_args', 'sd_newest_reviews_first' );
@stuartduff
stuartduff / remove-my-account-downloads-menu.php
Created May 2, 2023 16:08
Remove WooCommerce Downloads menu item from My Account page.
function custom_my_account_menu_items( $items ) {
unset($items['downloads']);
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'custom_my_account_menu_items' );
@stuartduff
stuartduff / remove-storefront-theme-handheld-search-icon.php
Created April 12, 2023 12:55
Remove Storefront theme handheld search icon
function remove_handheld_search( $links ) {
unset( $links['search'] );
return $links;
}
add_filter( 'storefront_handheld_footer_bar_links', 'remove_handheld_search' );
@stuartduff
stuartduff / gist:7b5d1f85bd29917fb9f9eca395d306f5
Created March 15, 2023 13:09
Replace Storefront Header Search With Default WordPress Search Widget
function storefront_product_search() {
if ( storefront_is_woocommerce_activated() ) {
?>
<div class="site-search">
<?php the_widget( 'WP_Widget_Search', 'title=' ); ?>
</div>
<?php
}
}
function remove_wc_stripe_from_checkout_page( $available_gateways ) {
unset( $available_gateways['stripe'] );
return $available_gateways;
}
add_filter( 'woocommerce_available_payment_gateways', 'remove_wc_stripe_from_checkout_page' );
@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 / move-stripe-payment-request-buttons.php
Created October 27, 2020 10:50
Move the WooCommerce Stripe payment request butttons.
remove_action( ‘woocommerce_after_add_to_cart_quantity’, array( WC_Stripe_Payment_Request::instance(), ‘display_payment_request_button_html’ ), 1 );
add_action( ‘woocommerce_simple_add_to_cart’, array( WC_Stripe_Payment_Request::instance(), ‘display_payment_request_button_html’ ), 1 );