/**
* Add woo attributes to a custom field with the same name
*
* @param $custom_fields
* @param $post_id
*
* @return mixed
*/
public function filter_custom_fields($custom_fields, $post_id)
{
if (!isset($custom_fields)) {
$custom_fields = array();
}
// Get the product correponding to this post
$product = new WC_Product($post_id);
foreach ($product->get_attributes() as $attribute) {
//$terms = wc_get_product_terms( $product->id, $attribute['name'], array( 'fields' => 'names' ) );
// Remove the eventual 'pa_' prefix from the attribute name
$attribute_name = $attribute['name'];
if (substr($attribute_name, 0, 3) == 'pa_') {
$attribute_name = substr($attribute_name, 3, strlen($attribute_name));
}
$custom_fields[$attribute_name] = explode(',', $product->get_attribute($attribute['name']));
}
return $custom_fields;
}