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 / 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);
@shameemreza
shameemreza / automatically-set-the-focus-keyword-with-product-title-in-rank-math-seo.php
Created July 7, 2024 14:49
Automatically set the focus keyword with product title in Rank Math SEO
function update_focus_keywords() {
$posts = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'product' // Replace post with the name of your post type
));
foreach($posts as $p){
// Checks if Rank Math keyword already exists and only updates if it doesn't have it
$rank_math_keyword = get_post_meta( $p->ID, 'rank_math_focus_keyword', true );
if ( ! $rank_math_keyword ){
update_post_meta($p->ID,'rank_math_focus_keyword',strtolower(get_the_title($p->ID)));
@shameemreza
shameemreza / disable-the-stripe-api-requests-from-loading-on-product-pages.php
Created July 7, 2024 14:49
Disable the Stripe API requests from loading on product pages in WooCommerce Stripe Payment Gateway
add_filter( ‘wc_stripe_load_scripts_on_product_page_when_prbs_disabled’, ‘__return_false’ );
add_filter( ‘wc_stripe_load_scripts_on_cart_page_when_prbs_disabled’, ‘__return_false’ );