gpt4 book ai didi

wordpress - 无法保存多个产品变体字段

转载 作者:行者123 更新时间:2023-12-02 03:11:30 25 4
gpt4 key购买 nike

当我保存第一个产品变体字段时,它们会被保存,但当我尝试保存第二个、第三个、第四个和更多时,它们不会被保存。

我也在为每个产品变体填写 SKU,所以它应该可以工作。但是很明显,这是我在这里缺少的东西吗?这是最新的 WordPress 和最新的 WooCommerce 版本。

//Display Fields
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 3 );

//Save variation fields
add_action( 'woocommerce_save_product_variation', 'save_variable_fields', 10, 1 );


/**
* Create new fields for variations
*
*/
function variable_fields( $loop, $variation_data, $variation ) {
?>
<tr>
<td>
<?php
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea['.$loop.']',
'label' => __( 'Contains', 'woocommerce' ),
'placeholder' => '',
'description' => __( '<br />Contains', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_textarea', true ),
)
);
?>
</td>
</tr>

<tr>
<td>
<?php
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea_2['.$loop.']',
'label' => __( 'Observation message', 'woocommerce' ),
'placeholder' => '',
'description' => __( '<br />Observation message', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_textarea_2', true ),
)
);
?>
</td>
</tr>

<?php
}


/**
* Save new fields for variations
*
*/
function save_variable_fields( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :

$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];

// Textarea
$_textarea = $_POST['_textarea'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $_textarea[$i] ) ) {
update_post_meta( $variation_id, '_textarea', stripslashes( $_textarea[$i] ) );
}
endfor;


// Textarea
$_textarea_2 = $_POST['_textarea_2'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $_textarea_2[$i] ) ) {
update_post_meta( $variation_id, '_textarea_2', stripslashes( $_textarea_2[$i] ) );
}
endfor;

endif;
}

最佳答案

我修改了用于保存的操作和函数:

//Save variation fields
add_action( 'woocommerce_save_product_variation', 'save_variable_fields', 10, 2 );
/**
* Save new fields for variations
*
*/
function save_variable_fields( $variation_id, $i ) {
if ( empty( $variation_id ) ) return;
if ( isset( $_POST['_textarea'][$i] ) ) {
update_post_meta( $variation_id, '_textarea', stripslashes( $_POST['_textarea'][$i] ) );
}
if ( isset( $_POST['_textarea_2'][$i] ) ) {
update_post_meta( $variation_id, '_textarea_2', stripslashes( $_POST['_textarea_2'][$i] ) );
}
}

关于wordpress - 无法保存多个产品变体字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39655279/

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