Codexshaper / laravel-woocommerce

WooCommerce Rest API for Laravel
MIT License
198 stars 57 forks source link

Is there a way to find all product variations for a specific attribute id, and/or a specific option value ? #37

Closed invaders-xx closed 4 years ago

maab16 commented 4 years ago

@invaders-xx

I checked and unfortunately attribute parameter not filter products properly in my case. Maybe it's woocommerce related bug. I attached some codes maybe it helps you

use Codexshaper\WooCommerce\Facades\Product;
use Codexshaper\WooCommerce\Facades\Variation;

$products = Product::whereType('variable')->get();;
$variations = [];
$option = "S";

foreach ($products as $product) {
    $p_variations = Variation::all($product->id);
    foreach ($p_variations as $product_variation) {
        $attributes = $product_variation->attributes;

        foreach ($attributes as $attribute) {

            if($attribute->option != $option) continue;

            $variations[] = $product_variation;
        }
    } 
}

var_dump($variations);

If not work inform me. Thanks

invaders-xx commented 4 years ago

thanks!