保存变体产品的自定义字段

时间:2015-03-09 作者:Irwan

目前,我正在使用WooCommerce for WordPress,并尝试为变体产品添加自定义字段。在做了一些研究之后,我找到了一些代码并尝试对其进行修改。

这是我的完整代码:https://gist.github.com/alphadc/da163cc95cfd1cede34a

add_action( \'woocommerce_product_after_variable_attributes\', \'variable_fields\', 10, 2 );
add_action( \'woocommerce_product_after_variable_attributes_js\', \'variable_fields_js\' );
add_action( \'woocommerce_process_product_meta_variable\', \'save_variable_fields\', 10, 1 );
add_action( \'woocommerce_process_product_meta_variable-subscription\' , \'save_variable_fields\' , 10 , 1 ) ;

function variable_fields( $loop, $variation_data ) {?>
<tr>
<td>
  <?php
  woocommerce_wp_textarea_input( 
    array( 
      \'id\'          => \'_weightdesc[\'.$loop.\']\', 
      \'label\'       => __( \'Weight Description\', \'woocommerce\' ), 
      \'placeholder\' => \'\', 
      \'description\' => __( \'Enter the custom value here.\', \'woocommerce\' ),
      \'value\'       => $variation_data[\'_weightdesc\'][0],
    )
  );
  ?>
</td>
 </tr>
<?php }
function variable_fields_js()?>
<tr>
<td>
  <?php
  woocommerce_wp_textarea_input( 
    array( 
      \'id\'          => \'_weightdesc[ + loop + ]\', 
      \'label\'       => __( \'My Textarea\', \'woocommerce\' ), 
      \'placeholder\' => \'\', 
      \'description\' => __( \'Enter the custom value here.\', \'woocommerce\' ),
      \'value\'       => $variation_data[\'_weightdesc\'][0],
    )
  );
  ?>
   </td>
  </tr>
<?php }
function save_variable_fields( $post_id ) {
  if (isset( $_POST[\'variable_sku\'] ) ) :

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

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


endif;
}
该字段显示在我的后端,但当我试图保存该值时,它不起作用。我也试图修改它,但仍然不起作用。

我从多个来源找到了这段代码,其中一个来源是:http://www.remicorson.com/woocommerce-custom-fields-for-variations/#comment-14159

我认为这是因为WooCommerce的更新(我使用的是2.3.5)。

谁能帮帮我吗?

3 个回复
最合适的回答,由SO网友:Irwan 整理而成

好的,根据上面链接上的答案(我在那里得到了旧代码,有人帮助回答),我为我的网站添加了修改代码。我试过了,效果很好。

更改:

add_action( \'woocommerce_product_after_variable_attributes\', \'variable_fields\', 10, 2 );
进入:

add_action( \'woocommerce_variation_options\', \'variable_fields\', 10, 3 );
和更改:

\'value\'       => $variation_data[\'_weightdesc\'][0],
进入:

\'value\' => get_post_meta($variation->ID, \'_weightdesc\', true)

SO网友:CᴴᵁᴮᴮʸNᴵᴺᴶᴬ

我知道这篇帖子很旧,但要更新这个问题:

截至WooCommerce 2.4.4

woocommerce_process_product_meta_variable 不再工作,必须将其更改为woocommerce_save_product_variation

所以

更改:

add_action( \'woocommerce_process_product_meta_variable\', \'save_variable_fields\', 10, 1 );
进入:

add_action( \'woocommerce_save_product_variation\', \'save_variable_fields\', 10, 1 );

SO网友:Unicco
function variation_settings_fields( $loop, $variation_data, $variation ) {

    // Text Field
    woocommerce_wp_text_input(
        array(
            \'id\'          => \'_text_field[\' . $variation->ID . \']\',
            \'label\'       => __( \'My Text Field\', \'woocommerce\' ),
            \'placeholder\' => \'\',
            \'desc_tip\'    => \'true\',
            \'description\' => __( \'Enter the custom value here.\', \'woocommerce\' ),
            \'value\'       => get_post_meta( $variation->ID, \'_text_field\', true )
        )
    );

}
add_action( \'woocommerce_product_after_variable_attributes\', \'variation_settings_fields\', 10, 3 );

function save_variation_settings_fields( $post_id ) {

    // Text Field
    $text_field = $_POST[\'_text_field\'][ $post_id ];
    if ( ! empty( $text_field ) ) {
        update_post_meta( $post_id, \'_text_field\', esc_attr( $text_field ) );
    }
}

add_action( \'woocommerce_save_product_variation\', \'save_variation_settings_fields\', 10, 2 );
结束

相关推荐