Skip to content

Instantly share code, notes, and snippets.

View shameemreza's full-sized avatar
🇧🇩
Talk is cheap. Check My Code, Blog and Portfolio.

Shameem Reza shameemreza

🇧🇩
Talk is cheap. Check My Code, Blog and Portfolio.
View GitHub Profile
@shameemreza
shameemreza / hide-out-of-stock-variations-in-woocommerce.php
Created September 26, 2024 09:12
Hide out of stock variations from variable product page in WooCommerce
add_filter( 'woocommerce_variation_is_visible', 'hide_out_of_stock_variations', 10, 4 );
function hide_out_of_stock_variations( $is_visible, $variation_id, $product_id, $variation ) {
if ( ! $variation->is_in_stock() ) {
$is_visible = false;
}
return $is_visible;
}
@shameemreza
shameemreza / woocommerce-remove-shopping-button.php
Created August 22, 2024 09:37
Remove the WooCommerce Continue Shopping Button
// Remove "Product added to cart" message
add_filter( 'wc_add_to_cart_message_html', '__return_null' );
// Remove "Continue Shopping" button
remove_action( 'woocommerce_cart_actions', 'woocommerce_button_continue_shopping', 10 );
@shameemreza
shameemreza / automatically-cancel-orders-in-woocommerce.php
Created July 25, 2024 05:03
Automatically cancel orders after one hour in WooCommerce
add_action( 'woocommerce_order_status_pending', 'wcwiz_cancel_failed_pending_order_event' );
function wcwiz_cancel_failed_pending_order_event( $order_id ) {
if ( ! wp_next_scheduled( 'wcwiz_cancel_failed_pending_order_after_one_hour', array( $order_id ) ) ) {
wp_schedule_single_event( time() + 3600, 'wcwiz_cancel_failed_pending_order_after_one_hour', array( $order_id ) );
}
}
add_action( 'wcwiz_cancel_failed_pending_order_after_one_hour', 'wcwiz_cancel_order' );
@shameemreza
shameemreza / show-company-names-in-woocommerce-order-lists.php
Created July 9, 2024 11:08
Show company instead of name in the orders list
// Show company instead of name in the orders list
add_filter( 'woocommerce_admin_order_buyer_name', function( string $buyer, WC_Order $order ): string {
$company = trim( $order->get_billing_company() );
if ( empty( $company ) ) {
return $buyer;
} else {
return $company;
}
}, 10, 2 );
@shameemreza
shameemreza / change-the-user-role-on-purchase-in-woocommerce.php
Created July 7, 2024 15:14
Change the user role on purchase in WooCommerce
add_action( 'woocommerce_order_status_processing', 'change_role_on_purchase', 10, 2 );
add_action( 'woocommerce_order_status_completed', 'change_role_on_purchase', 10, 2 );
function change_role_on_purchase( $order_id, $order ) {
$user = $order->get_user(); // Get the WP_User Object
// Check for "customer" user roles only
if( is_a( $user, 'WP_User' ) && in_array( 'customer', (array) $user->roles ) ) {
// Remove WooCommerce "customer" role
$user->remove_role( 'customer' );
@shameemreza
shameemreza / empty-the-cart-on-logout-in-woocommerce.php
Created July 7, 2024 14:59
Empty the cart on logout in WooCommerce
function wc_empty_cart_logout() {
if( function_exists('WC') ){
WC()->cart->empty_cart();
}
}
add_action('wp_logout', 'wc_empty_cart_logout');
@shameemreza
shameemreza / change-the-expiration-time-for-the-wc-product-loop-transient-from-30-days-to-1-day.php
Created July 7, 2024 14:52
Change the expiration time for the wc_product_loop_ transient from 30 days to 1 day in WooCommerce
add_action( 'setted_transient', 'mmx_wc_product_loop_transient', 50, 3 );
function mmx_wc_product_loop_transient( $transient, $value, $expiration ){
$pos = strpos( $transient, 'wc_product_loop_' );
if ( $pos !== false && $expiration == 2592000 ) {
set_transient( $transient, $value, DAY_IN_SECONDS );
}
}
@shameemreza
shameemreza / disable-square-api-calls-in-wp-admin-woocommerce-square.php
Created July 7, 2024 14:51
Disable Square API calls in wp-admin when using WooCommerce Square
add_filter( 'pre_http_request', 'sport_shooting_depot_mock_square_background_check', 10, 3 );
function sport_shooting_depot_mock_square_background_check( $preemt, $args, $url ) {
if ( $url !== 'YOUR_SITE_URL_HERE/wp-admin/admin-ajax.php?action=wc_square_background_sync_test' ) {
return false;
}
return array(
'body' => '[TEST_LOOPBACK]',
'response' => array(
'code' => '200 OK'
@shameemreza
shameemreza / change-the-currency-symbol-in-woocommerce.php
Created July 7, 2024 14:51
Change the currency symbol in WooCommerce
function filter_woocommerce_currency_symbol( $currency_symbol, $currency ) {
// Compare
switch( $currency ) {
case 'GBP': $currency_symbol = '€';
break;
}
return $currency_symbol;
}
add_filter( 'woocommerce_currency_symbol', 'filter_woocommerce_currency_symbol', 1, 2 );
@shameemreza
shameemreza / disable-default-shipping-method-selected.php
Created July 7, 2024 14:50
Disable default shipping method selected in WooCommerce
add_filter( 'woocommerce_shipping_chosen_method', '__return_false', 99);