How to run PHP Data Import Code for WordPress site?

1

I have a sample code to create a Post of a specific type (product of WooCommerce ) and would like to use it to do a routine importing 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 for importing the data. When I put this routine in a plugin and Enabled by the visual interface Wordpress runs the code countless times creating repeated records. When I run the PHP file in isolation, it reports error Fatal error: Call to undefined function .

Any good practice tips to use for this type of problem?

    
asked by anonymous 16.09.2014 / 16:36

2 answers

2

You can use register_activation_hook , which runs only on the plugin's 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 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;
    }
}

How to 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 a few% with% * until you are sure that the data is all ok, and only then let the database update happen. p>

* I prefer var_dump($valores); die();

    
16.09.2014 / 20:15
1

For this issue of importing products from WooCommerce there is a very good plugin with a free link which has an Add-on for WooCommerce link .

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

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

    
22.09.2014 / 15:32