| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
namespace ShopEngine\Modules\Comparison; |
| 5 |
|
| 6 |
|
| 7 |
use ShopEngine\Modules\Swatches\Helper; |
| 8 |
use ShopEngine\Modules\Swatches\Swatches; |
| 9 |
|
| 10 |
class Group_Meta { |
| 11 |
|
| 12 |
|
| 13 |
public function index(\WC_Product $product){ |
| 14 |
|
| 15 |
if( $product->get_status() !== 'publish' ){ |
| 16 |
return false; |
| 17 |
} |
| 18 |
|
| 19 |
$product_id = $product->get_id(); |
| 20 |
|
| 21 |
$formatted_attributes[ $product_id ] = []; |
| 22 |
|
| 23 |
if ( $product->has_attributes() ) { |
| 24 |
$attributes = $product->get_attributes(); |
| 25 |
|
| 26 |
foreach ( $attributes as $attribute ) { |
| 27 |
$attribute_id = $attribute->get_name(); |
| 28 |
|
| 29 |
$assigned_terms = $attribute->get_options(); |
| 30 |
|
| 31 |
$formatted_attributes[ $product_id ][ $attribute_id ]['name'] = wc_attribute_label( $attribute->get_name() ); |
| 32 |
$formatted_attributes[ $product_id ][ $attribute_id ]['plain_name'] = $attribute->get_name(); |
| 33 |
$formatted_attributes[ $product_id ][ $attribute_id ]['taxonomy'] = ''; |
| 34 |
|
| 35 |
if ( $attribute->is_taxonomy() ) { |
| 36 |
$txo_meta = Helper::get_tax_attribute( $attribute->get_name() ); |
| 37 |
|
| 38 |
$formatted_attributes[ $product_id ][ $attribute_id ]['taxonomy'] = $txo_meta->attribute_type; |
| 39 |
|
| 40 |
foreach ( $assigned_terms as $assigned_term ) { |
| 41 |
$term = get_term_by( 'id', $assigned_term, $attribute->get_name() ); |
| 42 |
|
| 43 |
if ( $txo_meta->attribute_type === Swatches::PA_COLOR ) { |
| 44 |
$t_val = get_term_meta( $assigned_term, 'shopengine_color', true ); |
| 45 |
|
| 46 |
$formatted_attributes[ $product_id ][ $attribute_id ]['taxonomy_value'][ $assigned_term ] = $t_val; |
| 47 |
} |
| 48 |
|
| 49 |
$formatted_attributes[ $product_id ][ $attribute_id ]['value'][] = $term->name; |
| 50 |
} |
| 51 |
} else { |
| 52 |
foreach ( $assigned_terms as $term ) { |
| 53 |
$formatted_attributes[ $product_id ][ $attribute_id ]['value'][] = $term; |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
//die(json_encode($formatted_attributes)); |
| 60 |
|
| 61 |
return $formatted_attributes; |
| 62 |
|
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
} |