'retrieve simple products and product_variation with corcel

In larvel I'm using corcel and with the help of this comment I'm trying to retrieve only simple products and variations of variable products.

$type = array('product_variation','product');
$status = 'publish';
$posts =
    \Corcel\Model\Post::
            type($type)->
            whereHas('taxonomies', function($q) {
                $q->where('taxonomy', 'product_type')
                ->whereHas('term', function($q) {
                    $q->where('slug', 'simple');
                })
                // since variations have no product_type taxonomy, then
                ->orwhere('taxonomy','<>', 'product_type');
            })->
            status($status)->
            latest()->
            limit(500)->
            get();
return $posts;

but it only returns product_variation(s), and no simple product. can some one please explain my wrong doing?



Solution 1:[1]

i had the same problem, i realized that i can use doesntHave

$posts = \Corcel\Model\Post::status('publish')

       ->whereIn('post_type', ['product_variation','product']  )
       ->doesntHave('taxonomies')
       ->orWhereHas('taxonomies', function ($query) {
           $query->whereIn('taxonomy',['product_type'])
           ->whereHas('term', function($q) {
                                $q->where('slug', 'simple');
                            });
       })
    ->latest()
    ->limit(50)
    ->get();
   return $posts;

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 ali mousavi