'[field=parent.ID]', 'Parent product SKU:' => '[cf=parent._sku]', 'Variation attributes:' => '[variation-all-attr separator=", "]', ); public $display_name = "All Variation’s Attributes"; public function getValue($post, $field) { $separator = isset($field['args']['separator']) ? $field['args']['separator'] : ', '; $showLabel = isset($field['args']['show-attr-name']) && 'true' === $field['args']['show-attr-name']; $product = wc_get_product($post->ID); if (empty($product)) { return ''; } if (isset($post->orderItem)) { $allAttrsValues = array(); foreach ($post->orderItem->get_all_formatted_meta_data('') as $metaData) { if (substr($metaData->key, 0, 1) !== '_') { $attributeValue = $metaData->value; if (0 === strpos($metaData->key, 'pa_')) { $option_term = get_term_by('slug', $attributeValue, $metaData->key); $attributeValue = $option_term && ! is_wp_error($option_term) ? str_replace(',', '\\,', $option_term->name) : str_replace(',', '\\,', $attributeValue); $taxonomy = get_taxonomy($metaData->key); $attributeLabel = !empty($taxonomy) ? $taxonomy->labels->singular_name : $metaData->key; } else { $attributeLabel = $metaData->key; } $allAttrsValues[] = ($showLabel ? $attributeLabel . ': ' : '') . $attributeValue; } } return implode($separator, array_filter($allAttrsValues)); } elseif ('product_variation' === $post->post_type) { if (!method_exists($product, 'get_variation_attributes')) { return ''; } $attrsInfo = $product->get_variation_attributes(false); array_walk($attrsInfo, function(&$attribute, $attribute_name) use ($showLabel) { if (0 === strpos($attribute_name, 'pa_')) { $option_term = get_term_by('slug', $attribute, $attribute_name); $attribute = $option_term && ! is_wp_error($option_term) ? str_replace(',', '\\,', $option_term->name) : str_replace(',', '\\,', $attribute); $taxonomy = get_taxonomy($attribute_name); $attributeLabel = !empty($taxonomy) ? $taxonomy->labels->singular_name : $attribute_name; } else { $attributeLabel = $attribute_name; } if ($showLabel && !empty($attribute)) { $attribute = $attributeLabel . ': ' . $attribute; } }); return implode($separator, array_filter($attrsInfo)); } else { return ''; } } }