• Hi!

    I hope someone here can help me, this is driving me nuts… I’m trying to create a custom txt feed for all my woocommerce products. I got arround 6k products in my shop…

    This is the code I’m using so far:

    function create_product_feed() {
        try {
            $count_single = wp_count_posts('product');
            $published_products = $count_single->publish;
            $chunks = ceil($published_products / 500);
            $file = ABSPATH . 'product-feed.txt';
            // generate xml content
    
            if ((file_exists( $file ))) {
                @unlink ( $file );
            }
    
            $feed = fopen($file, 'a+');
            $j = 0;
    
            fwrite($feed, chr(239) . chr(187) . chr(191) . 'Bezeichnung|Beschreibung|Preis|Deeplink|Produktbild|Herst_Nr|Hersteller|Lager_Baden|Lager_Moedling|EAN' . "\n");
    
            for($i = 0; $i < $chunks; $i++) {
                // Construct WP query
                $wp_query = array(
                    'post_type'          => array('product'),
                    'category__not_in'   => array(46),
                    'post_status'        => 'publish',
                    'fields'             => 'ids',
                    'offset'             => $i * 500,
                    'posts_per_page'     => '500',
                    'no_found_rows'      => true,
                    'suppress_filters' => false
                );
    
                $postchunk = new WP_Query($wp_query);
                $id_array = $postchunk->posts;
    
                foreach ($id_array as $post) {
                    $product_data = '';
    
                    //$thumb = wp_get_attachment_image_src(get_post_thumbnail_id($post), 'single-post-thumbnail');
                    // sanitize product short description
                    $desc_r = strip_tags(get_the_excerpt($post));
                    $desc_t = htmlspecialchars(trim($desc_r));
                    $desc_p = preg_replace("/\r|\n/", "", $desc_t);
                    $desc = strip_tags($desc_p, '<p><a>');
                    //setup_postdata($post);
                    $product_data .= get_the_title($post) . '|';
                    $product_data .= $desc . '|';
                    $product_data .= get_post_meta($post, '_price', true) . '|';
                    $product_data .= get_permalink($post) . '|';
                    //$product_data .= $thumb . '|';
    
                    $post_id = $post;
                    $product_data .= get_the_post_thumbnail_url($post_id, 'thumbnail') . '|';
    
                    $product_data .= get_post_meta($post, '_sku', true) . '|';
                    $product_data .= get_post_meta($post, 'ean', true) . PHP_EOL;
    
                    fwrite($feed, chr(239) . chr(187) . chr(191) . $product_data);
    
                    $j++;
                    unset($product_data);
                }
    
                unset($postchunk);
                unset($id_array);
            }
    
            // close the file
            fclose($feed);
    
            $response = "Feed erstellt! => " . $published_products . '/' . $chunks . '/' . $j;
    
        } catch (exception $ex) {
            $response = "Fehler: " . $ex;
        }
    
        echo $response;
        wp_die();
    }

    The feed gets created to a certain point, BUT I’m getting an internal server error inside google chrome dev console as soon as I use get_the_post_thumbnail_url()…

    I set wp_debug and saw that I’m getting a memory error:

    [04-Jul-2020 12:35:00 UTC] PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 65536 bytes) in /wp-includes/functions.php on line 4609

    and also

    [04-Jul-2020 12:35:00 UTC] PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 20480 bytes) in /wp-includes/meta.php on line 964

    When I comment out that piece of code it works fine… I also tried get_post_thumbnail_id() and wp_get_attachment_image_src() but same issue…

    It can’t be a memory thing though… before I was trying to create my own feed I was using the plugin “product feed pro” which uses exactly the same functions as I do to get the thumbnail image, without running into any memory issues.

    I just don’t know what to do anymore, I’m working on this small function for hours now, without being able to find out what the problem is…

    Please help! Thank you for your time!

    • This topic was modified 4 years, 8 months ago by webdesignsteyrer. Reason: wrong code
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Does your hosting plan allocate more than 256MB memory? You could try increasing the memory used by PHP & WP. For example in wp-config.php:

    ini_set('memory_limit','512MB');
    define('WP_MEMORY_LIMIT', '512M');
    define('WP_MAX_MEMORY_LIMIT', '512M');

    The error thrown is from adding data to the WP cache. You might try getting the data you need directly from the DB with $wpdb->get_var(). I believe this would bypass WP caching (I could be wrong on this). A post’s featured image attachment ID is in postmeta under key “_thumbbail_id”. Attachment image data is also in postmeta under key “_wp_attachment_metadata”. Construct the URL from the data returned.

    Thread Starter webdesignsteyrer

    (@webdesignsteyrer)

    @bcworkz I could… but why pay more? it should work with 256MB as well, because the plugin I was using, is doing it the same way as I’m trying to…

    Thread Starter webdesignsteyrer

    (@webdesignsteyrer)

    It just doesn’t work… no matter what I do or what function I use, as soon as I call wp_get_attachment_image_src or wp_get_attachment_image_src => internal server error ??

    Moderator bcworkz

    (@bcworkz)

    I was only suggesting you ensure you maximize usage of the memory already allocated to your current hosting account in case you’ve not already done so. I was not asking you to upgrade to more memory.

    My thinking isget_the_post_thumbnail_url() adds to the internal cache, taking up memory. If using $wpdb->get_var() instead bypasses caching, you’ll not use up a lot of memory for caching. The various wp_get_*() functions all utilize caching, so are not any more useful in reducing memory used. If $wpdb->get_var() turns out to use caching as well, using PHP mysqli_*() functions certainly would not.

    I think your only other option would be to reduce the number of products processed in one go.

    Thread Starter webdesignsteyrer

    (@webdesignsteyrer)

    Yes I know, thank you very much ?? I couldn’t figure it out, it just didn’t work, so I ended up increasing the memory to 512MB, works like a charm ??

    Thx for your answers and help!

    Have a nice day, guys!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Memory Issues with get_the_post_thumbnail_url’ is closed to new replies.