Home > front end >  Get all products matching an attribute through WooCommerce API
Get all products matching an attribute through WooCommerce API

Time:04-13

I would like to find a way to get all the products that match an attribute brand = 226Ers. For example. I am using the automattic/woocommerce package. I have tried a thousand ways but none of them work. I have looked at the documentation and checked the internet but I can't find the way to do it. Here is an example:

$data = [
            'attributes' => [
                [
                    'id' => 3,// id of attribute Marca
                    'options' => '226Ers'
                ]
            ]
        ];
$this->productos = $woocommerce->get('products', $data);

CodePudding user response:

It seems like you should pass the attribute and the attribute term to retrieve the product filtered by attribute. From ( woocommerce/includes/rest-api/Controllers/Version3/class-wc-rest-products-controller.php Line 117 )

    // Filter by attribute and term.
    if ( ! empty( $request['attribute'] ) && ! empty( $request['attribute_term'] ) ) {
        if ( in_array( $request['attribute'], wc_get_attribute_taxonomy_names(), true ) ) {
            $tax_query[] = array(
                'taxonomy' => $request['attribute'],
                'field'    => 'term_id',
                'terms'    => $request['attribute_term'],
            );
        }
    }

So the code may like

$data = [
    'attribute_term' => 3',
    'attribute' => 'pa_marca'
];
$this->productos = $woocommerce->get('products', $data);
  • Related