• Resolved nobby666

    (@nobby666)


    Hey guys

    I have managed to export my 25 subscribers from an old install, into a csv. I want to import it into a new build version of the site. I can only see a range of premium tools to carry out this task, whilst I am sure they are well priced for a big business needing to move thousands of items, I am tiny and dont really want pay for a premium tool for such a small task.

    Anyone know how I can get these imported?

    thanks

    Nobby

Viewing 4 replies - 1 through 4 (of 4 total)

  • Hi Nobby,

    For importing subscribers into a WooCommerce site specifically, you can leverage WordPress and WooCommerce functionalities with a bit of custom PHP code. However, since subscribers are essentially WordPress users with a specific role, you’ll be working with WordPress user functions primarily.

    Here’s a simple approach using a custom PHP script. This script reads your CSV file and imports each subscriber as a WordPress user with the role of ‘customer’ (commonly used by WooCommerce for customers/subscribers). Before proceeding, ensure your CSV is formatted with the necessary columns (e.g., email, first name, last name). Adjust the column names in the script as needed to match your CSV.

    Make sure your CSV file has headers that at least include email, first_name, and last_name. Adjust your CSV or the script according to the data you have.

    You can then add the following code to your theme’s functions.php file and run your import. This code will:

    1. Add a new page to the Tools menu in the WordPress admin for importing subscribers from a CSV file.
    2. Provide an upload form on this new page.
    3. Handle the file upload securely, with basic validation and sanitization.
    4. Process the CSV file to create or update users with the role of ‘customer’.
    function csv_importer_admin_menu() {
        add_management_page('Import CSV', 'Import CSV', 'manage_options', 'import-csv', 'csv_importer_upload_form');
    }
    add_action('admin_menu', 'csv_importer_admin_menu');
    
    function csv_importer_upload_form() {
        ?>
        <div class="wrap">
            <h2>Import Subscribers from CSV</h2>
            <form method="post" enctype="multipart/form-data">
                <input type="file" name="csv_import" id="csv_import" accept=".csv">
                <?php submit_button('Import CSV'); ?>
            </form>
        </div>
        <?php
    }
    function csv_importer_handle_upload() {
        if (isset($_FILES['csv_import']) && current_user_can('manage_options')) {
            if ($_FILES['csv_import']['error'] === UPLOAD_ERR_OK && $_FILES['csv_import']['type'] === 'text/csv') {
                $csv_path = $_FILES['csv_import']['tmp_name'];
                $handle = fopen($csv_path, 'r');
                if ($handle !== FALSE) {
                    fgetcsv($handle); // Assuming the first row contains headers
                    while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
                        $email = sanitize_email($data[0]);
                        $first_name = sanitize_text_field($data[1]);
                        $last_name = sanitize_text_field($data[2]);
                        if (!email_exists($email)) {
                            $user_id = wp_create_user($email, wp_generate_password(), $email);
                            wp_update_user([
                                'ID' => $user_id,
                                'first_name' => $first_name,
                                'last_name' => $last_name,
                                'role' => 'customer'
                            ]);
                        }
                    }
                    fclose($handle);
                    add_action('admin_notices', function() { echo '<div class="updated"><p>Subscribers imported successfully.</p></div>'; });
                }
            } else {
                add_action('admin_notices', function() { echo '<div class="error"><p>There was an error with the upload. Please ensure you are uploading a CSV file.</p></div>'; });
            }
        }
    }
    add_action('admin_init', 'csv_importer_handle_upload');
    
    Thread Starter nobby666

    (@nobby666)

    Hey

    Thanks for the detailed reply, not quite what I was expecting tbh, i figured there would be simply plugin. What you are showing me is probably a touch beyond my understanding. I have inserted this code into the functions.php and it looks fine, however, it errrors, i suspect as my data fields dont match up. Perhaps you could just show me an example of how to match the datafields up, so I can hopefully get this done.

    These are the fields used in my exported file:

    _billing_period
    _billing_interval
    _suspension_count
    _requires_manual_renewal
    _schedule_trial_end
    _schedule_next_payment
    _schedule_cancelled
    _schedule_end
    _schedule_payment_retry
    _schedule_start
    _subscription_switch_data
    _order_key
    _customer_user
    _created_via
    _download_permissions_granted
    _recorded_sales
    _recorded_coupon_usage_counts
    _new_order_email_sent
    _order_stock_reduced
    _billing_email
    _order_currency
    _order_total
    _prices_include_tax
    _billing_address_index
    _subscription_renewal_order_ids_cache
    _billing_first_name
    _billing_last_name
    _billing_address_1
    _billing_city
    _billing_postcode
    _billing_country
    _billing_phone
    _subscription_resubscribe_order_ids_cache
    _subscription_switch_order_ids_cache
    mailchimp_woocommerce_landing_site
    is_vat_exempt
    mepr_date_of_birth
    mepr_preferred_region
    mepr_favourite_match_venue
    mailchimp_woocommerce_is_subscribed
    _payment_method
    _payment_method_title

    Thanks for your help so far.

    regards

    Nobby

    Plugin Support ckadenge (woo-hc)

    (@ckadenge)

    Hi @nobby666,

    I understand that the process can seem a bit complex, especially if you’re not familiar with coding. However, please keep in mind that most plugins are premium plugins.

    In that case, I suggest the following alternatives:

    1. Running the exact question you’re asking, through an AI platform like ChatGPT, for recommendations.
    2. Reaching out to a developer to assist you out with the task
    3. Finding YouTube videos and tutorials that demonstrate the process using a step by step method.
    4. Joining our WooCommerce Slack community (it does have a developer channel where you can ask coding questions): https://woo.com/community-slack/

    Hope it helps!

    Thread Starter nobby666

    (@nobby666)

    Many thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Importing 25 subscriptions’ is closed to new replies.