79507656

Date: 2025-03-13 21:24:08
Score: 1.5
Natty:
Report link

thanks to @Rishard i tried to custom the endpoint by add this code in WP snippet

add_filter('woocommerce_rest_prepare_product_object', function ($response, $product, $request) {
    if (!empty($response->data['attributes'])) {
        foreach ($response->data['attributes'] as &$attribute) {
            if (!empty($attribute['options'])) {
                $options_with_ids = [];
                foreach ($attribute['options'] as $option) {
                    $term = get_term_by('name', $option, $attribute['slug']);
                    if ($term) {
                        $options_with_ids[] = [
                            'id' => $term->term_id,
                            'name' => $term->name
                        ];
                    } else {
                        $options_with_ids[] = [
                            'id' => null,
                            'name' => $option
                        ];
                    }
                }
                $attribute['options'] = $options_with_ids;
            }
        }
    }
    return $response;
}, 10, 3);

now to response is

{
                "id": 7,
                "name": "Size",
                "slug": "pa_size",
                "position": 4,
                "visible": true,
                "variation": true,
                "options": [
                    {
                        "id": 123,
                        "name": "L"
                    },
                    {
                        "id": 138,
                        "name": "XL"
                    }
                ]
            }
Reasons:
  • Blacklisted phrase (0.5): thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Rishard
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Mohamed Sabry