次のコードを使用して、バリエーションとして 2 つのカスタム フィールドを作成しました (Remi Corso に感謝します)。
関数.php
バリエーション設定を追加
add_action( 'woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3 );
バリエーション設定の保存
add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );
バリエーション用の新しいフィールドを作成する
function variation_settings_fields( $loop, $variation_data, $variation ) {
woocommerce_wp_text_input(
array(
'id' => '_pdf_ficha_tecnica[' . $variation->ID . ']',
'label' => __( 'PDF FICHA TÉCNICA', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'aqui', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_pdf_ficha_tecnica', true )
)
);
woocommerce_wp_text_input(
array(
'id' => '_pdf_ficha_caracteristicas[' . $variation->ID . ']',
'label' => __( 'PDF FICHA CARACTERÍSTICAS', 'woocommerce' ),
'placeholder' => 'http://',
'desc_tip' => 'true',
'description' => __( 'aqui', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_pdf_ficha_caracteristicas', true )
)
);
}
バリエーション用に新しいフィールドを保存
function save_variation_settings_fields( $post_id ) {
$text_field = $_POST['_pdf_ficha_tecnica'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_pdf_ficha_tecnica', esc_attr( $text_field ) );
}
$text_field = $_POST['_pdf_ficha_caracteristicas'][ $post_id ];
if( ! empty( $text_field ) ) {
update_post_meta( $post_id, '_pdf_ficha_caracteristicas', esc_attr( $text_field ) );
}
}
これらのカスタム フィールドには URL が保存され、リンクとして表示されます。これらのフィールドを表示しようとしていますが、正しい解決策を見つけるのに苦労しています。
誰でも私を案内できますか?ファイル「variable.php」に焦点を当てる必要がありますか? そしてJS?または、フックでフィールドをレンダリングできますか?
前もって感謝します!