retrieve simple products and product_variation with corcel

201 Views Asked by At

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?

1

There are 1 best solutions below

0
ali mousavi On BEST ANSWER

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;