Product::options()
Summary
No summary available.Source
File: src/BigCommerce/Post_Types/Product/Product.php
public function options() { $data = json_decode( get_post_meta( $this->post_id(), self::OPTIONS_DATA_META_KEY, true ), true ); if ( empty( $data ) || ! is_array( $data ) ) { return []; } // ensure we have all the fields we expect for each option $data = array_map( function ( $option ) { return wp_parse_args( $option, [ 'id' => 0, 'display_name' => '', 'type' => '', 'sort_order' => 0, 'option_values' => [], 'required' => true, 'config' => [], ] ); }, $data ); // filter out option values not present on any of the variants $source = $this->get_source_data(); $variant_options = []; foreach ( $source->variants as $variant ) { foreach ( $variant->option_values as $value ) { $variant_options[ $value->option_id ][] = $value->id; } } $variant_options = array_map( 'array_unique', $variant_options ); $data = array_map( function ( $option ) use ( $variant_options ) { $valid_values = isset( $variant_options[ $option['id'] ] ) ? $variant_options[ $option['id'] ] : []; $option['option_values'] = array_filter( $option['option_values'], function ( $value ) use ( $valid_values ) { return in_array( $value['id'], $valid_values ); } ); return $option; }, $data ); // respect the sorting set by the user usort( $data, function ( $a, $b ) { if ( $a['sort_order'] == $b['sort_order'] ) { return ( $a['display_name'] < $b['display_name'] ) ? - 1 : 1; } return ( $a['sort_order'] < $b['sort_order'] ) ? - 1 : 1; } ); return $data; }