在管理产品常规框中获取 WooCommerce 产品变体属性条款
Get WooCommerce product variation attribute terms in admin products general box
我使用以下代码在 wp 管理产品数据框的常规选项卡中显示 每个 变量产品的 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 } ?>
I display the id and sku of each variable product in the general tab.
在 WooCommerce 中,可变产品的常规选项卡不可用(可见),除了 启用税费。在 WooCommerce > 设置 > 常规 > 启用税收下。
因为它用于设置父级税收 class 和税收状态,所以如果您没有启用税收,那里将不会显示任何内容。
回答你的问题
替换这部分
// 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">
有
// Loop through the variations Ids
foreach( $children_ids as $child_id ) {
$count++;
$child = wc_get_product( $child_id );
$pr_attribute_name = implode( " / ", $child->get_variation_attributes() );
$pr_id_variable = $child->get_id();
$pr_sku_variable = $child->get_sku();
?>
<p> <?php echo $pr_attribute_name; ?> </p>
<p class="form-field">
对于变体属性名称 (变体 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.
我使用以下代码在 wp 管理产品数据框的常规选项卡中显示 每个 变量产品的 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 } ?>
I display the id and sku of each variable product in the general tab.
在 WooCommerce 中,可变产品的常规选项卡不可用(可见),除了 启用税费。在 WooCommerce > 设置 > 常规 > 启用税收下。
因为它用于设置父级税收 class 和税收状态,所以如果您没有启用税收,那里将不会显示任何内容。
回答你的问题
替换这部分
// 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">
有
// Loop through the variations Ids
foreach( $children_ids as $child_id ) {
$count++;
$child = wc_get_product( $child_id );
$pr_attribute_name = implode( " / ", $child->get_variation_attributes() );
$pr_id_variable = $child->get_id();
$pr_sku_variable = $child->get_sku();
?>
<p> <?php echo $pr_attribute_name; ?> </p>
<p class="form-field">
对于变体属性名称 (变体 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.