Вот способ получить атрибуты продукта в пользовательском шорткоде, где вы будете определять идентификатор продукта в качестве аргумента шорткода id
.
Код функции:
if ( ! function_exists( 'display_product_additional_information' ) ) {
function display_product_additional_information($atts) {
// Shortcode attribute (or argument)
$atts = shortcode_atts( array(
'id' => ''
), $atts, 'product_additional_information' );
// If the "id" argument is not defined, we try to get the post Id
if ( ! ( ! empty($atts['id']) && $atts['id'] > 0 ) ) {
$atts['id'] = get_the_id();
}
// We check that the "id" argument is a product id
if ( get_post_type($atts['id']) === 'product' ) {
$product = wc_get_product($atts['id']);
}
// If not we exit
else {
return;
}
ob_start(); // Start buffering
do_action( 'woocommerce_product_additional_information', $product );
return ob_get_clean(); // Return the buffered outpout
}
add_shortcode('product_additional_information', 'display_product_additional_information');
}
Код помещается в файл function.php вашей активной дочерней темы (или активной темы).Проверено и работает.
ИСПОЛЬЗОВАНИЕ SHORTCODE
с определенным идентификатором продукта:
[product_attributes id='37']
Или в php:
echo do_shortcode("[product_attributes id='37']");
На существующей странице товара (например, когда убрана вкладка "дополнительная информация") :
[product_attributes]
Или вphp:
echo do_shortcode("[product_attributes]");
Вы получите что-то вроде этого: