Update concat authors for all products

function update_concatenated_authors_for_all_products() { // Get all products $args = array( ‘post_type’ => ‘product’, ‘posts_per_page’ => -1, // Get all products ‘post_status’ => ‘publish’, ); $products = get_posts($args); foreach ($products as $product) { // Reuse the function to update the concatenated…Continue reading

Add discount for non zero store credit

add_action(‘woocommerce_cart_calculate_fees’, ‘apply_subscriber_discount’, 10, 1); function apply_subscriber_discount($cart) { if (is_admin() && !defined(‘DOING_AJAX’)) return; // Ensure it only runs on the front-end $user = wp_get_current_user(); // Apply 10% discount only if the user has the role ‘subscriber’ if (in_array(‘subscriber’, (array) $user->roles)) {…Continue reading

Shipping in Backend

add_action(‘woocommerce_admin_order_data_after_order_details’, ‘add_custom_shipping_method_backend’); function add_custom_shipping_method_backend($order_id) { // Manually get the order object $order = wc_get_order($order_id); if (!$order || !is_a($order, ‘WC_Order’)) { echo ‘ Error: Order is invalid. Please reload the page or check your setup. ‘; return; } ?> Royal Mail…Continue reading

Automatically Add Store Credit on BACS Subscription Renewal

function add_store_credit_on_subscription_renewal($order_id) { $logger = wc_get_logger(); $order = wc_get_order($order_id); if (!$order) { $logger->error(“⚠️ Store Credit Error: Order {$order_id} not found on first attempt.”, array( ‘source’ => ‘store-credit-renewal’ )); sleep(2); // Wait 2 seconds and try again $order = wc_get_order($order_id); if…Continue reading

Discounts for Branch users FE

add_action(‘woocommerce_cart_calculate_fees’, ‘apply_branch_discount_by_category’, 20, 1); function apply_branch_discount_by_category($cart) { if (is_admin() && !defined(‘DOING_AJAX’)) return; // Ensure it only runs on the front-end $user = wp_get_current_user(); // Only apply discount for users with the “branch” role if (!in_array(‘branch’, (array) $user->roles)) { return; }…Continue reading

Set Stripe as default gateway in checkout

add_action(‘template_redirect’, ‘define_default_payment_gateway’); function define_default_payment_gateway() { // Prevent execution during admin, AJAX, or scheduled tasks if (is_admin() || wp_doing_ajax() || wp_doing_cron()) { return; } if (is_checkout() && !is_wc_endpoint_url()) { WC()->session->set(‘chosen_payment_method’, ‘stripe’); } }Continue reading

Free shipping for Branch

add_filter(‘woocommerce_package_rates’, ‘force_free_shipping_for_branch_users’, 20, 2); function force_free_shipping_for_branch_users($rates, $package) { $user = wp_get_current_user(); // Only apply for users with the “branch” role if (!empty($user->roles) && in_array(‘branch’, (array) $user->roles)) { // Create a free shipping method $free_shipping_rate = new WC_Shipping_Rate( ‘free_shipping_branch’, // Rate…Continue reading

Display debug at checkout for test user

function display_store_credit_debug_info() { $test_user_id = 11; if (!is_checkout()) return; if (get_current_user_id() != $test_user_id) return; // Only show for the test user $user_id = get_current_user_id(); $store_credit_balance = get_user_meta($user_id, ‘acfw_store_credit_balance’, true); $session_store_credit = WC()->session->get(‘acfw_store_credit_amount’); $cart_total = WC()->cart->total; echo ‘ ‘; echo ‘🛠…Continue reading

Set default shipping method at checkout

function set_default_shipping_method() { if (is_admin() || !is_checkout()) { return; } $packages = WC()->shipping->get_packages(); foreach ($packages as $key => $package) { if (!empty($package[‘rates’])) { $shipping_methods = array_keys($package[‘rates’]); $chosen_method = WC()->session->get(‘chosen_shipping_methods’); if (empty($chosen_method)) { WC()->session->set(‘chosen_shipping_methods’, array($shipping_methods[0])); } } } } add_action(‘woocommerce_before_cart’, ‘set_default_shipping_method’);…Continue reading