我使用以下代码在 wp admin 产品数据框的常规选项卡中显示每个可变产品的 id 和 sku。知道如何获取变量产品属性名称 - 在本例中为“测试 1”和“测试 2”吗?
变化
常规选项卡
代码
<?php
add_action( 'woocommerce_product_options_general_product_data', 'echo_product_id_sku_general_tab' );
function echo_product_id_sku_general_tab() {
$children_ids = wc_get_product()->get_children();
$count = 0;
// Loop through the variations Ids
foreach( $children_ids as $child_id ) {
$count++;
$pr_id_variable = wc_get_product($child_id)->get_id();
$pr_sku_variable = wc_get_product($child_id)->get_sku();
?>
<p class="form-field">
<label><?php _e( 'Variation', 'woocommerce' ); ?> <?php echo $count; ?> ID</label>
<input type="text" value="ID<?php echo $pr_id_variable; ?>"></input>
</p>
<p class="form-field">
<label><?php _e( 'Variation', 'woocommerce' ); ?> <?php echo $count; ?> SKU</label>
<textarea><?php echo $pr_sku_variable; ?></textarea>
</p>
<?php } ?>
<?php } ?>
最佳答案
对于变体属性名称(变体 Id 和 sku),您可以使用以下内容:
add_action( 'woocommerce_product_options_general_product_data', 'echo_product_id_sku_general_tab' );
function echo_product_id_sku_general_tab() {
global $product_object;
if( $product_object->is_type('variable') ) {
$count = 1;
foreach( $product_object->get_children() as $variation_id ) {
$variation = wc_get_product($variation_id);
$name = array();
foreach( $variation->get_attributes() as $taxonomy => $term_slug ) {
$name[] = get_term_by( 'slug', $term_slug, $taxonomy )->name;
}
echo '<p><strong>'. sprintf( __("Variation %s Name: %s", "woocommerce"), $count, '</strong>' . implode(' - ', $name) ) .'</p>
<p class="form-field">
<label>' . sprintf( __("Variation %s ID", "woocommerce"), $count) . '</label>
<input type="text" value="ID' . $variation_id . '"></input>
</p>
<p class="form-field">
<label>' . sprintf( __("Variation %s SKU", "woocommerce"), $count) . '</label>
<textarea>' . $variation->get_sku() . '</textarea>
</p>';
$count++;
}
}
}
代码位于事件子主题(或事件主题)的functions.php 文件中。经过测试并有效。
Note: this works for product attribute taxonomies only (starting with "pa_"), but not with custom attributes.
关于php - 在管理产品常规框中获取 WooCommerce 产品变体属性术语,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66365318/