How to run PHP Data Import code for Wordpress site?

Asked

Viewed 470 times

1

I have an example code to create a Post of a specific type (product from WooCommerce) and would like to use it to make a routine import of many products.

<?php 

    function createNewProduct() {
        $new_post = array(
            'post_title' => "Custom Variable Test",
            'post_content' => 'Lorem ipsum dolor sit amet...',
            'post_status' => 'publish',
            'post_type' => 'product'
        );

        $skuu = 'custom-prod-1';
        $post_id = wp_insert_post($new_post);

        echo 'post_id = ' . $post_id;
        update_post_meta($post_id, '_sku', $skuu );
        update_post_meta( $post_id, '_price', "25" );

        wp_set_object_terms( $post_id, 'Gasolina', 'pa_fuel' );
        wp_set_object_terms( $post_id, 'Ford', 'pa_brand' );

        update_post_meta( $post_id, '_visibility', 'search' );
        update_post_meta( $post_id, '_stock_status', 'instock');

        // Lendo o Post criado
        $content_post = get_post($post_id);
        $content = $content_post->post_content;

        // $content = apply_filters('the_content', $content);
        // echo '<pre>' . print_r($content, true) . '</pre>';
        // echo '<pre>' . print_r($content_post, true) . '</pre>';
    }
?>

I’d like to know the best approach to importing the data. When I put this routine in a plugin and enable by the visual interface Wordpress executes the code numerous times creating repeated records. When I run the PHP file alone it accuses error Fatal error: Call to undefined function.

Any good practice tips to be used for this type of problem ?

2 answers

2

You can use the register_activation_hook, that runs only on plugin activation:

<?php
/**
 * Plugin Name: (SOPT) Teste Activation Hook
 */

register_activation_hook( __FILE__, 'activate_sopt_32701' );

function activate_sopt_32701()
{
    // fazer_algo_somente_uma_vez();    
}

Or else use this implementation of run_once:

/* https://wordpress.stackexchange.com/a/25667/12615 */
function run_once($key){
    $test_case = get_option('run_once');
    if (isset($test_case[$key]) && $test_case[$key]){
        return false;
    }else{
        $test_case[$key] = true;
        update_option('run_once',$test_case);
        return true;
    }
}

Mode of use:

if ( run_once('meu_custom_import') ){
    // fazer_algo_somente_uma_vez();
}

if ( run_once('meu_outro_custom_import') ){
    // fazer_outro_algo_somente_uma_vez();
}

In the case of import procedures, I recommend doing several var_dump($valores); die(); * until you are sure that the data is all ok, and only then let the database update happen.

* I prefer the printf( '<pre><code>%s</code></pre>', print_r( $variavel, true ) );

1

For this problem of importing products from WooCommerce there is a very good plugin with a free version https://wordpress.org/plugins/wp-all-import/ that has an Add-on to WooCommerce https://wordpress.org/plugins/woocommerce-xml-csv-product-import.

This plugin reads your XML and allows you to use XPATH to define the mapping between your XML and the WooCommerce.

There are videos showing how easy it is to import products including with Attributes (which is the case of Fuel and Brand in their example).

  • Thanks. I’ll try it, thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.