簡體   English   中英

WooCommerce:向產品變體添加自定義字段

[英]WooCommerce : add custom fields to product variations

我需要幫助將自定義字段添加到我的產品變體和簡單產品頁面。 我嘗試使用 RemiCorson 的信息 ( http://www.remicorson.com/woocommerce-custom-fields-for-variations/ ),但我不斷收到錯誤消息。 我試圖復制我在該用戶的 ISBN 帖子中看到的內容,但它對我不起作用。

下面代碼的問題在於它沒有顯示在實時站點上。 非常感謝所有幫助,我今天大部分時間都在試圖弄清楚我做錯了什么。

向 Woocommerce 中的簡單產品和可變產品頁面添加 6 個自定義文本字段和 1 個復選框。 這些不是由購物者提供(即填寫)的字段,而是我希望在我的產品頁面上顯示的自定義信息(而不是在選項卡中)。

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );

function woo_add_custom_general_fields() {

  global $woocommerce, $post;

  echo '<div class="options_group">';

  // Custom fields will be created here...

// Text Field
woocommerce_wp_text_input( 
    array( 
        'id'          => '_ISBN_field', 
        'label'       => __( 'ISBN', 'woocommerce' ), 
        'placeholder' => '',
        'desc_tip'    => 'true',
        'description' => __( 'ISBN.', 'woocommerce' ) 
    )
);
function woo_add_custom_general_fields_save( $post_id ){

// Customer text ISBN Field
$woocommerce_text_field = $_POST['_ISBN_field'];
if( !empty( $woocommerce_text_field ) )
    update_post_meta( $post_id, '_ISBN_field', esc_attr( $woocommerce_text_field ) );
else
    update_post_meta( $post_id, '_ISBN_field', '' );
}
// Display Custom Field Value
echo get_post_meta( $post->ID, '_ISBN_field', true );

}
/* WooCommerce */

/* ----------------------------------------------------------------------------------- */
/* Start WooThemes Functions - Please refrain from editing this section */
/* ----------------------------------------------------------------------------------- */

我從來不需要打擾woocommerce_product_after_variable_attributes_js ,你只需要添加輸入然后處理保存它。

自從 Remi 的文章發表以來發生的另一件事是,WooCommerce 變體元數據不再打印在<Table>元素中......而現在是一個<div>元素。 這對於您如何構建新內容很重要。

以下是向變體添加元字段的方法:

// regular variable products
add_action( 'woocommerce_product_after_variable_attributes', 'add_to_variations_metabox', 10, 3 );
add_action( 'woocommerce_save_product_variation', 'save_product_variation', 20, 2 );


/*
 * Add new inputs to each variation
 *
 * @param string $loop
 * @param array $variation_data
 * @return print HTML
 */
function add_to_variations_metabox( $loop, $variation_data, $variation ){

    $custom = get_post_meta( $variation->ID, '_custom', true ); ?>

        <div class="variable_custom_field">
            <p class="form-row form-row-first">
                <label><?php echo __( 'Custom Data:', 'plugin_textdomain' ); ?></label>
                <input type="text" size="5" name="variation_custom_data[<?php echo $loop; ?>]" value="<?php echo esc_attr( $custom ); ?>" />
            </p>
        </div>

    <?php 

}

/*
 * Save extra meta info for variable products
 *
 * @param int $variation_id
 * @param int $i
 * return void
 */
function save_product_variation( $variation_id, $i ){

    // save custom data
    if ( isset( $_POST['variation_custom_data'][$i] ) ) {
        // sanitize data in way that makes sense for your data type
        $custom_data = ( trim( $_POST['variation_custom_data'][$i]  ) === '' ) ? '' : sanitize_title( $_POST['variation_custom_data'][$i] );
        update_post_meta( $variation_id, '_custom', $custom_data );
    }

}

我從這里找到了以下代碼與 WooCommerce 4.8.0 一起使用

/**
 * Create new fields for variations
 *
*/
function hrx_variation_settings_fields( $loop, $variation_data, $variation ) {

    // Text Field
    woocommerce_wp_text_input( 
        array( 
            'id'          => 'isbn_field[' . $variation->ID . ']', 
            'label'       => __( 'ISBN', 'texdomain' ), 
            'placeholder' => '',
            'desc_tip'    => 'true',
            'description' => __( 'ISBN', 'texdomain' ),
            'value'       => get_post_meta( $variation->ID, 'isbn_field', true )
        )
    );

}
add_action( 'woocommerce_product_after_variable_attributes', 'hrx_variation_settings_fields', 10, 3 );

/**
 * Save new fields for variations
 *
*/
function hrx_save_variation_settings_fields( $post_id ) {

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

}
add_action( 'woocommerce_save_product_variation', 'hrx_save_variation_settings_fields', 10, 2 );

WooCommerce:向產品變體添加自定義字段

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM