logo

Pablo Guides

Creating a WordPress plugin to import products into WooCommerce involves several steps. Below is a step-by-step guide to help you build a basic product import plugin. This example assumes you want to import products from a CSV file, but you can adapt the code to support other formats or sources.

Step 1: Set Up Plugin Structure

  1. Create a new directory for your plugin, e.g., woocommerce-product-importer.
  2. Inside this directory, create the main PHP file, e.g., woocommerce-product-importer.php.

Step 2: Define Plugin Header and Basic Structure

<?php
/*
Plugin Name: WooCommerce Product Importer
Description: Import products into WooCommerce from a CSV file.
Version: 1.0
Author: Your Name
*/

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

Step 3: Create Admin Menu and Import Form

// Add menu item in the admin dashboard
add_action('admin_menu', 'product_importer_menu');

function product_importer_menu() {
    add_menu_page(
        'Product Importer',
        'Product Importer',
        'manage_options',
        'product-importer',
        'product_importer_page'
    );
}

// Create the admin page
function product_importer_page() {
    ?>
    <div class="wrap">
        <h2>Product Importer</h2>
        <form method="post" action="">
            <input type="submit" name="import_products" class="button button-primary" value="Import Products">
        </form>
    </div>
    <?php
}

Step 4: Handle Product Import Logic

if (isset($_POST['import_products'])) {
    add_action('admin_notices', 'product_importer_notice');

    function product_importer_notice() {
        echo '<div class="notice notice-success is-dismissible"><p>Products have been imported successfully!</p></div>';
    }

    // Perform the import here
    // Example: Read data from a CSV file and create WooCommerce products

    // For demonstration purposes, let's assume a CSV file named products.csv in the plugin directory
    $csv_path = plugin_dir_path(__FILE__) . 'products.csv';

    if (file_exists($csv_path)) {
        $csv_data = array_map('str_getcsv', file($csv_path));

        foreach ($csv_data as $row) {
            $product_data = array(
                'post_title' => $row[0], // Assuming the first column is the product title
                'post_content' => $row[1], // Assuming the second column is the product description
                'post_type' => 'product', // WooCommerce product post type
                'post_status' => 'publish',
            );

            $product_id = wp_insert_post($product_data);

            // Set product data (adjust this based on your CSV file structure)
            update_post_meta($product_id, '_regular_price', $row[2]); // Assuming the third column is the regular price
            update_post_meta($product_id, '_price', $row[2]);
        }
    }
}

Step 5: Upload CSV File for Testing

Create a CSV file (e.g., products.csv) in the plugin directory with the product data. For example:

Product Title,Product Description,Regular Price
Product 1,Description 1,20.00
Product 2,Description 2,30.00

Step 6: Testing and Final Considerations

  1. Upload the plugin to the WordPress plugins directory.
  2. Activate the plugin through the WordPress admin.
  3. Go to the "Product Importer" page in the admin dashboard and click the "Import Products" button.

This is a basic example, and depending on your specific needs, you may need to enhance the plugin to handle variations, categories, images, and other product details. Additionally, consider error handling, validation, and security measures in a real-world scenario. Always thoroughly test the plugin on a staging site before deploying it to a live environment.

Pablo Guides