我正在尝试在单个产品的“ additional_information”标签中显示变量产品的元字段“ _ts_gtin”。
我刚刚发现可以通过这种方式获得特定变体的值:
get_post_meta( $variations[ 'variation_id' ], '_ts_gtin', true )
有人可以帮助我吗?
答案 0 :(得分:0)
您可以将其添加到您的functions.php或插件中:
add_action( 'woocommerce_product_additional_information', 'print_variation_html' );
function print_variation_html($product){
$variations_id = $product->get_variation_id();
if(!$variations_id) return;
$variation_meta = get_post_meta( $variations_id , '_ts_gtin', true );
print_r( $variation_meta );
}
答案 1 :(得分:0)
添加以下代码段以完成上述操作-
add_action( 'woocommerce_product_additional_information', 'add_info_in_product_additional_information', 99 );
function add_info_in_product_additional_information( $product ) {
if( $product ) {
$children_ids = $product->get_children();
if( $children_ids ) {
echo '<ul id="variation-gtin">';
foreach ( $children_ids as $v_id ) {
echo '<li class="item-'.$v_id.'">#' . $v_id . ': ' . get_post_meta( $v_id , '_ts_gtin', true ) . '</li>';
}
echo '</ul>';
}
}
}
// scripts
add_action( 'wp_footer', 'add_scripts_on_found_variation' );
function add_scripts_on_found_variation() {
?>
<script type="text/javascript">
( function( $ ) {
$( 'form.variations_form').on( 'found_variation', function( event, variation ) {
$( '#variation-gtin .item-'+variation.variation_id ).show().siblings().hide();
} );
} )( jQuery );
</script>
<?php
}
代码进入活动主题的功能。php