gpt4 book ai didi

php - 如何获取 Woocommerce 当前版本 ID?

转载 作者:搜寻专家 更新时间:2023-10-31 21:10:26 25 4
gpt4 key购买 nike

我正在尝试在我的 Woocommerce 后端创建一些附加功能,我在其中将自定义字段添加到 Woocommerce 后端的变体部分。然而,我终其一生都无法弄清楚如何获取当前变体 ID,以便我可以获得帖子元数据。

这是我一直在使用的:

<?php

// Get variations
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $post->ID
);
$variations = get_posts( $args );

foreach ( $variations as $variation ) {

$variation_id = absint( $variation->ID );$variable_id = $this['variation_id'];
$variation_post_status = esc_attr( $variation->post_status );
$variation_data = get_post_meta( $variation_id );
$variation_data['variation_post_id'] = $variation_id;
echo get_post_meta( $variation_data['variation_post_id'], '_my_custom_field', true) . ' - TEST';

}

?>

当我检查后端时,它似乎正在将所有后元数据拉入每个变体,如下所示:

enter image description here

但是,如果我使用如下所示的实际变体 ID,那么它适用于该变体:

echo get_post_meta( 134, '_my_custom_field', true) . ' - Test Variation #134';

最佳答案

@德里克,

您试图在模板的哪个位置显示自定义字段数据?

如果它在产品循环中,你可以这样做:

<?php
// Get the post IDs of all the product variations
$variation_ids = $product->children;

// Each product variation id
foreach( $variation_ids as $var_id ) :

// Get all of the custom field data in an array
$all_cfs = get_post_custom($var_id);

// Show all of the custom fields
var_dump($all_cfs);

// Get specific data from the certain custom fields using get_post_meta( $post_id, $key, $single );
$test = get_post_meta( $var_id, '_text_field', true );

endforeach;
?>

关于php - 如何获取 Woocommerce 当前版本 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21116386/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com