'The PHP code skips the main image on my product feed from Woocommerce

This function should return an array of Woocommerce product photos links, but it seems that the product feed that is created take the main picture only when there are no photos in the product galery, if it finds it skips the main image.

protected function getPhotosUrlsForWoocommerceProduct($product_id)
{
    if ($product_id == null)
        return null;

    $product_gallery_ids = get_post_meta($product_id, '_product_image_gallery', $return_only_one = true);
    // $product_gallery_ids = 45 sau 45,46,77

    $product_images_array = array();

    if (!empty($product_gallery_ids)) {
        $product_images_array = explode(",", $product_gallery_ids);
    } else {
        $product_thumbnail_id = get_post_meta($product_id, '_thumbnail_id', $return_only_one = true);
        if (!empty($product_thumbnail_id)) {
            $product_images_array = array($product_thumbnail_id);
        }
    }
    $photos_urls = array();
    foreach ($product_images_array as $photo_id) {
        $image_array = wp_get_attachment_image_src($photo_id, $size = 'full');
            
        $photos_urls[$photo_id] = $image_array[$url_key = 0];
    }
    return $photos_urls;
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source