Scripts and snippets that might be useful:

WooCommerce Dynamic Shipping

Updates shipping expenses dynamically when you change country.

Add to cart notification

Receive an email each time someone puts something in the cart.

add_action('woocommerce_add_to_cart', 'send_email_on_add_to_cart');

function send_email_on_add_to_cart($cart_item_key) {
    $cart = WC()->cart->get_cart();
    $cart_item = $cart[$cart_item_key];

    $product_name = $cart_item['data']->get_name();
    $product_price = $cart_item['data']->get_price();
    $quantity = $cart_item['quantity'];

    // Email details
    $to = 'info@sitiware.it';
    $subject = 'New item added to cart';
    $headers = array('Content-Type: text/html; charset=UTF-8');
    $body = '<p>Product Name: ' . $product_name . '</p>';
    $body .= '<p>Product Price: ' . $product_price . '</p>';
    $body .= '<p>Quantity: ' . $quantity . '</p>';

    wp_mail($to, $subject, $body, $headers);
}

Add title to all images

Add title attr to all images in the website.

function add_title_attribute_to_images($content) {
    // Use regex to find all image tags
    $pattern = '/<img(.*?)>/i';

    // Callback function to process each image tag
    $content = preg_replace_callback($pattern, function($matches) {
        $img_tag = $matches[0];
        
        // Check if title attribute is already present
        if (!preg_match('/title="/i', $img_tag)) {
            // Find the `alt` attribute value
            preg_match('/alt="([^"]*)"/i', $img_tag, $alt_matches);
            $alt_text = isset($alt_matches[1]) ? $alt_matches[1] : '';

            // Add `title` attribute only if it's missing
            $img_tag = str_replace('<img', '<img title="' . esc_attr($alt_text) . '"', $img_tag);
        }

        return $img_tag;
    }, $content);

    return $content;
}
add_filter('the_content', 'add_title_attribute_to_images');
function add_title_attribute_to_images($content) {
    // Use regex to find all image tags
    $pattern = '/<img(.*?)>/i';

    // Add title attribute if it doesn't exist
    $replacement = '<img$1 title="$2"';

    // Check for `alt` attribute and duplicate it for `title`
    $content = preg_replace_callback($pattern, function($matches) {
        $img_tag = $matches[0];
        
        // Find the `alt` attribute value
        preg_match('/alt="([^"]*)"/i', $img_tag, $alt_matches);
        $alt_text = isset($alt_matches[1]) ? $alt_matches[1] : '';

        // If title attribute is missing, add it with `alt` attribute value
        if (!preg_match('/title="/i', $img_tag)) {
            $img_tag = str_replace('<img', '<img title="' . esc_attr($alt_text) . '"', $img_tag);
        }

        return $img_tag;
    }, $content);

    return $content;
}
add_filter('the_content', 'add_title_attribute_to_images');

Reactivate an expired cancelled subscription

With this snippet it is possible to reactivate an expired subscription with the “expired” status. Once the snippet has been activated, you must visit the following url by modifying yourdomain.com with the domain of your site and replacing 123 with the subscription id.

https://yourdomain.com/wp-admin/admin-post.php?action=force_subscription_reactivation&subscription_id=123

add_action('admin_post_force_subscription_reactivation', 'force_subscription_reactivation');

function force_subscription_reactivation() {
    if (!current_user_can('manage_woocommerce')) {
        wp_die(__('Non hai i permessi per eseguire questa azione.', 'woocommerce'));
    }

    $subscription_id = isset($_GET['subscription_id']) ? absint($_GET['subscription_id']) : 0;

    if ($subscription_id) {
        $subscription = wcs_get_subscription($subscription_id);

        if (!$subscription) {
            wp_die(__('Subscription non trovata o ID non valido.', 'woocommerce'));
        }

        if ($subscription->has_status('expired')) {
            try {
                // Aggiorna le date obbligatorie per evitare errori
                $new_next_payment_date = gmdate('Y-m-d H:i:s', strtotime('+1 month')); // Esempio: 1 mese da oggi
                $new_end_date = gmdate('Y-m-d H:i:s', strtotime('+6 months')); // Esempio: 6 mesi da oggi

                $subscription->update_dates([
                    'next_payment' => $new_next_payment_date,
                    'end'          => $new_end_date,
                ]);

                // Aggiorna lo stato forzatamente
                wp_update_post([
                    'ID'          => $subscription_id,
                    'post_status' => 'wc-active',
                ]);

                // Salva i cambiamenti nella subscription
                $subscription->save();

                wp_redirect(admin_url('edit.php?post_type=shop_subscription'));
                exit;
            } catch (Exception $e) {
                wp_die(__('Errore durante la riattivazione della subscription: ' . $e->getMessage(), 'woocommerce'));
            }
        } else {
            wp_die(__('La subscription non è nello stato expired.', 'woocommerce'));
        }
    }

    wp_die(__('ID della subscription non valido.', 'woocommerce'));
}