Skip to content

Instantly share code, notes, and snippets.

@artikus11
Created April 19, 2023 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artikus11/e8bbcdb8002bb528e9c47c4926873797 to your computer and use it in GitHub Desktop.
Save artikus11/e8bbcdb8002bb528e9c47c4926873797 to your computer and use it in GitHub Desktop.
/**
* Вывод избранных атрибутов в карточке товара
*
* @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