Skip to content

Instantly share code, notes, and snippets.

View shameemreza's full-sized avatar
🇧🇩
Problem-Solver | WooCommerce Expert | Customer-First Mindset

Shameem Reza shameemreza

🇧🇩
Problem-Solver | WooCommerce Expert | Customer-First Mindset
View GitHub Profile
@shameemreza
shameemreza / automatically-reset-inventory-when-subscription-cancele.php
Created December 20, 2024 05:27
Automatically Restock on Subscription Cancellation
add_action('woocommerce_subscription_status_cancelled', 'restock_inventory_on_subscripadd_action('woocommerce_subscription_status_cancelled', 'adjust_inventory_on_subscription_cancel', 10, 1);
function adjust_inventory_on_subscription_cancel($subscription) {
if (!$subscription) return;
foreach ($subscription->get_items() as $item) {
$product = $item->get_product();
if ($product && $product->managing_stock()) {
$current_stock = $product->get_stock_quantity();
@shameemreza
shameemreza / make-mandatory-coupons-for-all-products-in-woocommerce.php
Created December 10, 2024 04:54
Make mandatory coupons for all products in WooCommerce
add_action( 'woocommerce_check_cart_items', 'mandatory_global_coupon_code' );
function mandatory_global_coupon_code() {
$coupon_code = 'summer2'; // The required coupon code
// Check if the required coupon is applied
$coupon_applied = in_array( strtolower( $coupon_code ), WC()->cart->get_applied_coupons() );
// If the coupon is not applied, prevent checkout
if ( ! $coupon_applied ) {
@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'