0

I've created a custom function that read an XML file and import it in woocommerce products. The XML files are exported by an external platform every day in the website ftp.

I replaced the wordpress cronjob for a real cronjob, the cronjob actually calls the function but the function is never executed (see screenshot).

XML cronjob

My function:

<?php 
/**
 * DynamiX Child Theme Functions
 * Load languages directory for translation
*/ 

function XML_import() {
    $dir = $_SERVER['DOCUMENT_ROOT'] . 'import_pw/';
    $files1 = scandir($dir);
    $files = array_diff($files1, array('.', '..'));



    foreach($files as $file) {
        $filename = $dir . $file;

        if (file_exists($filename))
        {
            $time = strtotime('2010-04-28 17:25:43');
            $time = filemtime($filename);

            $elapsedTime = time()-filemtime($filename);

            if( $elapsedTime < (24*3600) ) {
                $xml = simplexml_load_file($filename) or die ("Error, cannot create object");

                    foreach($xml->Machine as $Machine) {

                        if (!get_page_by_title($Machine->OMSCHR1, OBJECT, 'product')) :

                            // Empty array
                            $attributes = array();

                            if($Machine->BOUWJAAR != 0) {
                                // Bouwjaar Attribute
                                $attribute = new WC_Product_Attribute();
                                $attribute->set_id(0);
                                $attribute->set_name('Bouwjaar');
                                $attribute->set_options(array($Machine->BOUWJAAR));
                                $attribute->set_position(1);
                                $attribute->set_visible(true);
                                $attribute->set_variation(false);
                                $attributes['bouwjaar'] = $attribute;
                            }

                            if($Machine->MERK != '') {
                                // Merk Attribute
                                $attribute = new WC_Product_Attribute();
                                $attribute->set_id(0);
                                $attribute->set_name('Merk');
                                $attribute->set_options(array($Machine->MERK));
                                $attribute->set_position(0);
                                $attribute->set_visible(true);
                                $attribute->set_variation(false);
                                $attributes['merk'] = $attribute;
                            }

                            if($Machine->configuratie->NRR_1034 != '') {
                                // Brandstof Attribute
                                $attribute = new WC_Product_Attribute();
                                $attribute->set_id(0);
                                $attribute->set_name('Brandstof');
                                $attribute->set_options(array($Machine->configuratie->NRR_1034));
                                $attribute->set_position(2);
                                $attribute->set_visible(true);
                                $attribute->set_variation(false);
                                $attributes['brandstof'] = $attribute;
                            }

                            if($Machine->URENST1 != 0) {
                                // Draaiuren Attribute
                                $attribute = new WC_Product_Attribute();
                                $attribute->set_id(0);
                                $attribute->set_name('Draaiuren');
                                $attribute->set_options(array($Machine->URENST1));
                                $attribute->set_position(3);
                                $attribute->set_visible(true);
                                $attribute->set_variation(false);
                                $attributes['draaiuren'] = $attribute;
                            }

                            $description = '';

                            if($Machine->TYPE != '') {
                                $description .= 'Type: ' . $Machine->TYPE . '<br>';
                            }
                            if($Machine->MERKCODE != '') {
                                $description .= 'NO: ' . $Machine->MERKCODE . '<br>';
                            }
                            if($Machine->KENTEKEN != '') {
                                $description .= 'Kenteken: ' . $Machine->KENTEKEN . '<br>';
                            }
                            if($Machine->BOUWJAAR != 0) {
                                $description .= 'Bouwjaar: ' . $Machine->BOUWJAAR . '<br>';
                            }
                            if($Machine->URENST1 != 0) {
                                $description .= 'Urenstaat: ' . $Machine->URENST1 . '<br>';
                            }
                            if($Machine->configuratie->NRR_1034 != '') {
                                $description .= 'Brandstof: ' . $Machine->configuratie->NRR_1034 . '<br>';
                            }
                            if($Machine->configuratie->NRR_1093 != '') {
                                $description .= 'Hefvermogen: ' . $Machine->configuratie->NRR_1093 . '<br>';
                            }
                            if($Machine->configuratie->NRR_1035 != '') {
                                $description .= 'Hefhoogte: ' . $Machine->configuratie->NRR_1035 . '<br>';
                            }
                            if($Machine->configuratie->NRR_1026 != '') {
                                $description .= 'Voorzetapparatuur: ' . $Machine->configuratie->NRR_1026 . '<br>';
                            }
                            if($Machine->configuratie->NRR_1020 != '') {
                                $description .= 'Cabine: ' . $Machine->configuratie->NRR_1020 . '<br>';
                            }
                            if($Machine->configuratie->NRR_1008 != '') {
                                $description .= 'Extra hydrauliek ventielen: ' . $Machine->configuratie->NRR_1008 . '<br>';
                            }

                            $base64_img = $Machine->IMAGE;

                            // Upload dir.
                            $upload_dir  = wp_upload_dir();
                            $upload_path = str_replace( '/', DIRECTORY_SEPARATOR, $upload_dir['path'] ) . DIRECTORY_SEPARATOR;

                            $img             = str_replace( 'data:image/jpeg;base64,', '', $base64_img );
                            $img             = str_replace( ' ', '+', $img );
                            $decoded         = base64_decode( $img );
                            $filename        = $title . '.jpeg';
                            $file_type       = 'image/jpeg';
                            $hashed_filename = md5( $filename . microtime() ) . '_' . $filename;

                            // Save the image in the uploads directory.
                            $upload_file = file_put_contents( $upload_path . $hashed_filename, $decoded );

                            $attachment = array(
                                'post_mime_type' => $file_type,
                                'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $hashed_filename ) ),
                                'post_content'   => '',
                                'post_status'    => 'inherit',
                                'guid'           => $upload_dir['url'] . '/' . basename( $hashed_filename )
                            );

                            $attach_id = wp_insert_attachment( $attachment, $upload_dir['path'] . '/' . $hashed_filename );

                            var_dump($attach_id);

                            // Set woocommerce product
                            $getters_and_setters = array('name' => $Machine->OMSCHR1, 'slug' => $Machine->OMSCHR1, 'category_ids' => array(70),'description' => $description, 'image_id' => $attach_id, 'attributes' => $attributes);
                            $product = new WC_Product();
                            foreach ($getters_and_setters as $function => $value) {
                                $product->{"set_{$function}"}($value);
                            }

                            // Save product
                            $product->save();
                    endif;
                }
            }
        }
    }
}

// Schedule Cron Job Event
function custom_cron_job() {
    if ( ! wp_next_scheduled( 'XML_import' ) ) {
        wp_schedule_event( time(), 'hourly', 'XML_import' );
    }
}
add_action( 'wp', 'custom_cron_job' );

I added in my wp-config.php:

define('DISABLE_WP_CRON', true);

Anyone that could help me? I'm stuck on this one.

Thanks. Rens

2
  • If you disable WP Cron, you have to manually call it yourself, and do so for every site in a network, can you confirm the method you're using to do that? As well as the cron job itself, and identify the file in your question ( is it a standalone file? functions.php? A plugin? It's not clear ) – Tom J Nowell Sep 26 '18 at 21:24
  • Thanks for commenting! I've set an real cronjob @ my hosting. the code i'm posting is the functions.php from a child theme. – RensM Sep 27 '18 at 16:07
0

I've managed to fix it!

The problem was calling the wrong add_action.

I had:

// Schedule Cron Job Event
function custom_cron_job() {
    if ( ! wp_next_scheduled( 'XML_import' ) ) {
        wp_schedule_event( time(), 'hourly', 'XML_import' );
    }
}
add_action( 'wp', 'custom_cron_job' );

But it must be:

// Schedule Cron Job Event
if (! wp_next_scheduled( 'xml' ) ) {
    wp_schedule_event( time(), 'hourly', 'xml' );
}

add_action( 'xml', 'XML_import' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.