Created
April 19, 2023 19:57
-
-
Save artikus11/e8bbcdb8002bb528e9c47c4926873797 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Вывод избранных атрибутов в карточке товара | |
* | |
* @param $product | |
* | |
* @return array | |
*/ | |
public function get_attributes_to_product( $product ) { | |
$product_attributes = []; | |
$attributes = $this->get_base_attributes( $product ); | |
if ( ! $attributes ) { | |
return $product_attributes; | |
} | |
foreach ( $this->get_base_attributes( $product ) as $attribute ) { | |
$values = []; | |
if ( $attribute->is_taxonomy() ) { | |
$attribute_taxonomy = $attribute->get_taxonomy_object(); | |
$attribute_values = wc_get_product_terms( $product->get_id(), $attribute->get_name(), [ 'fields' => 'all' ] ); | |
foreach ( $attribute_values as $attribute_value ) { | |
$value_name = esc_html( $attribute_value->name ); | |
if ( $attribute_taxonomy->attribute_public ) { | |
$values[] = sprintf( | |
'<a href="%s" rel="tag">%s</a>', | |
esc_url( | |
get_term_link( | |
$attribute_value->term_id, | |
$attribute->get_name() | |
) | |
), | |
$value_name | |
); | |
} else { | |
$values[] = $value_name; | |
} | |
} | |
} else { | |
$values = $attribute->get_options(); | |
foreach ( $values as &$value ) { | |
$value = make_clickable( esc_html( $value ) ); | |
} | |
} | |
$sep = ';'; | |
$product_attributes[ 'attribute_' . sanitize_title_with_dashes( $attribute->get_name() ) ] = [ | |
'label' => wc_attribute_label( $attribute->get_name() ), | |
'value' => wptexturize( implode( $sep, $values ) ), | |
]; | |
} | |
return $product_attributes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment