Skip to content

Instantly share code, notes, and snippets.

@devidw
Last active July 25, 2022 13:29
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 devidw/950a1b63b18575cf847b409b311ac9cd to your computer and use it in GitHub Desktop.
Save devidw/950a1b63b18575cf847b409b311ac9cd to your computer and use it in GitHub Desktop.
<?php
/**
* Move the product meta to the attributes table on the single product page.
*/
// Remove the product meta box.
// This has to be placed inside the theme's functions.php file.
// Won't work if placed inside a plugin.
// remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40);
// Add the product meta to the attributes table.
// Besides the theme's functions.php file, this can also safely be placed inside a plugin.
add_filter(
hook_name: 'woocommerce_display_product_attributes',
accepted_args: 2,
callback: function (array $product_attributes, object $product): array {
$new_attributes = [];
// SKU
if (wc_product_sku_enabled() && ($product->get_sku() || $product->is_type('variable'))) {
$new_attributes[] = [
'label' => esc_html('SKU', 'woocommerce'),
'value' => '<span class="sku_wrapper"><span class="sku">' . ($sku = $product->get_sku()) ? $sku : esc_html__('N/A', 'woocommerce') . '</span></span>',
];
}
// Categories
$cats = wc_get_product_category_list($product->get_id());
if (!empty($cats)) {
$new_attributes[] = [
'label' => esc_html('Categories', 'woocommerce'),
'value' => $cats,
];
}
// Tags
$tags = wc_get_product_tag_list($product->get_id());
if (!empty($tags)) {
$new_attributes[] = [
'label' => esc_html('Tags', 'woocommerce'),
'value' => $tags,
];
}
return array_merge($new_attributes, $product_attributes);
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment