Support » Plugin: Default featured image » importing images into WooCommerce using cron

  • Resolved Vadim

    (@vadikcoma)


    We are using WP All Import to import around 4800 products from XML into WooCommerce.

    The process is following:

    Customer loads images + xml file containing product data and image name to FTP server and we run WPAI in this folder to import everything. This process happens daily.

    In case there are no images for certain products we are using predefined placeholder. We are using Default featured image for that

    In the beginning we used manual run, when we launched import from WP All Import settings everything seems to be ok. Last week we introduced cron and we tried that couple of times. And with cron we faced following issue:

    Default image was added to every product even already containing an image. If I open any product, I can see the placeholder being used as the main image, and the real image was used as an additional image.

    Here how our shop looks after cron (blue logo is a placeholder that we set in Default featured image):

    https://www.dropbox.com/s/gwpb435p3vckbtz/products.jpg

    https://www.dropbox.com/s/0lsl8tkkw8hgx9q/single.png

    Here are cron commands, just for your reference:

    =====

    We spoke with WP All Import team and here is their response:

    ======

    I performed some tests with your plugin Default featured image, and I’m seeing that images are not assigned as a featured image when this plugin is active and the import is run via a Cron Job. If you disable that plugin, then images are imported correctly.

    This issue seems to happen because Cron Jobs are run under an anonymous user.

    To test, I modified line 68 from that plugin (default-featured-image/set-default-featured-image.php) and removed this admin validation: https://d.pr/i/Y1A2bW. Now it seems that featured images are being imported correctly via Cron Jobs.

    That said, I’d recommend getting in touch with that plugin’s developer to see if this modification is valid or if there’s a better way to avoid this behaviour.

    Hopefully, that helps, but let us know if you have any other questions.

    The page I need help with: [log in to see the link]

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author janw.oostendorp

    (@janwoostendorp)

    Hello Vadim,

    You did the research. And I can agree to pretty much all of this.
    But this admin check is needed. I think it would be better to make an exception for the cronjob

    Can you try to add the following if statement?

    
    		// Only affect thumbnails on the frontend, do allow ajax calls.
    		if ( ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) ) {
    			return $null;
    		}
    
    		// Exclude Cronjobs.
    		if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
    			return $null;
    		}
    

    So this is the existing if statement + an extra exclusion for cronjobs.
    I did not test this now, I’ll do my own test on Friday.

    Let me know.
    Jan-Willem

    Thread Starter Vadim

    (@vadikcoma)

    Thank you Jan-Willem!

    Can you please confirm the best way of adding that to plugin code and still keeping updates possible? So update didn’t erase this part about cron jobs

    Plugin Author janw.oostendorp

    (@janwoostendorp)

    Don’t worry about updating, if this works I will put this code in the next update on friday.
    If it doesn’t work I’ll still make a wp-all-import fix and you can still update.

    Thread Starter Vadim

    (@vadikcoma)

    Great, thank you so much!

    I will update code and run cron in next few hours

    Thread Starter Vadim

    (@vadikcoma)

    Unfortunatelly its not working. Its adding Default Image again:

    https://ilumino.lv/product/kabelis-audio-2×0-50-cu-melns-sarkans-a30011/

    https://ilumino.lv/product/kabelis-audio-2×1-5-cu-melns-sarkans/

    Here is plugin code snippet:

     * @return string|array Single metadata value, or array of values
    	 */
    	public function set_dfi_meta_key( $null, $object_id, $meta_key, $single ) {
    		// Only affect thumbnails on the frontend, do allow ajax calls.
    		if ( ( is_admin() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) ) {
    			return $null;
    		}
    
    		// Exclude Cronjobs.
    		if ( defined( 'DOING_CRON' ) && DOING_CRON ) {
    			return $null;
    		}
    
    		// Check only empty meta_key and '_thumbnail_id'.
    		if ( ! empty( $meta_key ) && '_thumbnail_id' !== $meta_key ) {
    			return $null;
    		}
    
    		// Check if this post type supports featured images.
    		if ( ! post_type_supports( get_post_type( $object_id ), 'thumbnail' ) ) {
    			return $null; // post type does not support featured images.
    		}
    
    Thread Starter Vadim

    (@vadikcoma)

    We are running LiteSpeed server. Maybe crons are called differently there?

    Plugin Author janw.oostendorp

    (@janwoostendorp)

    No Litespeed will not have any impact on this. I will test this on friday, and probably have a fix ready for it.

    Do you have the feed url I can use for testing? or is it private?

    Thread Starter Vadim

    (@vadikcoma)

    Sure, can we get in touch via email? vadim@coma.lv

    Plugin Author janw.oostendorp

    (@janwoostendorp)

    Yes I got it. I’ll let you know when I got something.

    Plugin Author janw.oostendorp

    (@janwoostendorp)

    Hello Vadim

    Below is a dirty fix that should help. I tested it with my own xml import (yours was to big, took to much time).

    I recommend placeing the code below in a new file:
    wp-content/mu-plugins/wp-all-import-dirty-fix.php

    
    <?php
    /**
     * @see https://wordpress.org/support/topic/importing-images-into-woocommerce-using-cron/
     */
    function wp_all_import_dirty_fix( $dfi_id ) {
    
    	// get the full callstack, this is very dirty.
    	$stack = debug_backtrace();
    
    	foreach ( $stack as $trace ) {
    		// only check for \PMXI_Import_Record::process
    		if ( empty( $trace['class'] ) || $trace['class'] !== 'PMXI_Import_Record' || empty( $trace['function'] ) || $trace['function'] !== 'process' ) {
    			continue; // this line in the stacktrace is not \PMXI_Import_Record::process
    		}
    
    		return null; // this is \PMXI_Import_Record::process, set DFI id to null.
    	}
    
    	return $dfi_id;
    }
    
    add_filter( 'dfi_thumbnail_id', 'wp_all_import_dirty_fix' );
    

    I’ve tested this by deleting all posts between every test. I think this might work with an update, but you should inspect the settings of your import.
    I’m not that familiar with wp-all-import.

    I’ll contact wp-all-import later.

    If we do find a better solution I’ll let you know.
    You don’t have to worry about updates, this code will never interfere with an update on DFI. At worst a check will be done twice.

    Let me know if you have any questions.
    I’ll respond quicker tomorrow. If you have any problems.

    Jan-Willem

    Thread Starter Vadim

    (@vadikcoma)

    Thank you very much Jan!

    So far it seems to be working (cron is still in progress)

    Plugin Author janw.oostendorp

    (@janwoostendorp)

    Hello Vadim,

    I’ve worked on a cleaner solution and have it ready in this 1.7.0-beta.zip.

    From what I’ve tested this beta works, but I’d love your feedback.

    Could you test this before I release this into the wild?
    Please disable/delete the previous solution before testing.

    Jan-Willem

    Thread Starter Vadim

    (@vadikcoma)

    Thanks Jan,

    Due to client request some time ago we had to remove Default Image plugin, as client added all images for their products and doesn’t need Default Placeholder anymore.

    I will use your solution with another client who will require default image.

    Thank you!

Viewing 13 replies - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.