Я пытаюсь отобразить только значение настраиваемого поля для варианта продукта, выбранного в Woocommerce.У меня есть этот код в файле functions.php, который отображает настраиваемые поля, но для всех вариантов, а не для выбранного.
/**
* Template hook points at product_attributes.php
* Adds BTU Custom Field into Specification tab
* @template product-attributes.php
*/
add_action('custom_template_hook', 'display_custom_field' , 10 , 1);
function display_custom_field($product) {
// Check if product is variable
if( $product->is_type( 'variable' ) ){
// If so, get all available variations!
$variations = $product->get_available_variations();
// Loop through variations
foreach ($variations as $variation) {
// Check if meta field isn't empty, if not display the field!
if (!empty(get_post_meta( $variation['variation_id'], 'btu_cf', true))) {
// get product object from variation ID
$product_obj = wc_get_product( $variation['variation_id'] );
?>
<tr class="woocommerce-product-attributes-item">
<th class="woocommerce-product-attributes-item__label"><div class="woocommerce_custom_field">BTU <span></th>
<td class="woocommerce-product-attributes-item__value"><?php echo wp_kses_post( get_post_meta( $variation['variation_id'], 'btu_cf', true ) ); ?></td>
</tr>
<?
}
}
}
}
Я считаю, что мне нужно добавить Javascript / jQuery к приведенному выше коду в моемfunctions.php, но я в растерянности.Любая помощь очень ценится.Спасибо