問題描述
我遵循了這個答案
這是我的代碼:
//在鏈接產品"部分顯示自定義字段add_action('woocommerce_product_options_related', 'woocom_linked_products_data_custom_field');//保存到自定義字段add_action('woocommerce_process_product_meta', 'woocom_linked_products_data_custom_field_save');//生成自定義字段的函數函數 woocom_linked_products_data_custom_field() {全球 $woocommerce, $post;$product = wc_get_product( $post->ID );?><p class="form-field"><label for="subscription_toggle_product"><?php _e('訂閱切換產品', 'woocommerce');?></label><select class="wc-product-search" style="width: 50%;"id="subscription_toggle_product" name="subscription_toggle_product" data-placeholder="<?php esc_attr_e('搜索產品…', 'woocommerce'); ?>"data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>"><?php$product_id = get_post_meta( $post->ID, '_subscription_toggle_product_id', true );如果($product_id){$product = wc_get_product( $product_id );如果 ( is_object( $product ) ) {echo '<option value="' . esc_attr( $product_id ) . '"' .選擇(真,真,假).'>'.wp_kses_post( $product->get_formatted_name() ) .'</選項>';}}?></選擇></p><?php}//函數保存自定義字段函數 woocom_linked_products_data_custom_field_save( $post_id ){如果(isset($_POST['subscription_toggle_product'])){$product_field_type = $_POST['subscription_toggle_product'];update_post_meta( $post_id, '_subscription_toggle_product_id', $product_field_type );}}
這種選擇字段僅適用于定義的 multiple
屬性和值數組.所以你不能將它用于簡單的 ID.如果您添加到您的選擇字段 multiple="multiple"
屬性,它將起作用.
此外,自從 Woocommerce 3 事情發生了變化:
- 有更好的鉤子來保存數據.
- 您現在可以使用 CRUD 對象和相關方法.
以下代碼適用于多個產品 ID(產品 ID 數組):
//在鏈接產品"部分顯示自定義選擇字段add_action('woocommerce_product_options_related', 'display_linked_products_data_custom_field');函數 display_linked_products_data_custom_field() {全球 $product_object, $post;?><p class="form-field"><label for="subscription_toggle_products"><?php _e( '訂閱切換產品', 'woocommerce');?></label><select class="wc-product-search" multiple="multiple" style="width: 50%;"id="subscription_toggle_ids" name="_subscription_toggle_ids[]" data-placeholder="<?php esc_attr_e('搜索產品…', 'woocommerce'); ?>"data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>"><?php$product_ids = $product_object->get_meta('_subscription_toggle_ids');foreach ( $product_ids 作為 $product_id ) {$product = wc_get_product( $product_id );如果 ( is_object( $product ) ) {echo '<option value="' . esc_attr( $product_id ) . '"' .選擇(真,真,假).'>'.wp_kses_post( $product->get_formatted_name() ) .'</選項>';}}?></選擇></p><?php}//將值保存到產品中add_action('woocommerce_admin_process_product_object', 'save_linked_products_data_custom_field_value', 10, 1);函數 save_linked_products_data_custom_field_value( $product ){$data = isset( $_POST['_subscription_toggle_ids'] ) ?array_map('intval', (array) $_POST['_subscription_toggle_ids']): array();$product->update_meta_data('_subscription_toggle_ids', $data);}
代碼位于您的活動子主題(活動主題)的 function.php 文件中.經測試有效.
I have followed this answer How to add more custom field in Linked Product of Woocommerce to add a custom select field to my Linked Product screen in Woocommerce. This new field is meta key is subscription_toggle_product
.
It's working fine, but once you have selected a product, you can't delete it (there is no "Empty" option or cross symbol). I haven't got a clue how I can allow the selection to be deselected. I've tried adding an <option>
with an empty value, but it hasn't worked.
Here is my code:
// Display the custom fields in the "Linked Products" section
add_action( 'woocommerce_product_options_related', 'woocom_linked_products_data_custom_field' );
// Save to custom fields
add_action( 'woocommerce_process_product_meta', 'woocom_linked_products_data_custom_field_save' );
// Function to generate the custom fields
function woocom_linked_products_data_custom_field() {
global $woocommerce, $post;
$product = wc_get_product( $post->ID );
?>
<p class="form-field">
<label for="subscription_toggle_product"><?php _e( 'Subscription Toggle Product', 'woocommerce' ); ?></label>
<select class="wc-product-search" style="width: 50%;" id="subscription_toggle_product" name="subscription_toggle_product" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
<?php
$product_id = get_post_meta( $post->ID, '_subscription_toggle_product_id', true );
if ( $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select>
</p>
<?php
}
// Function the save the custom fields
function woocom_linked_products_data_custom_field_save( $post_id ){
if (isset($_POST['subscription_toggle_product'])) {
$product_field_type = $_POST['subscription_toggle_product'];
update_post_meta( $post_id, '_subscription_toggle_product_id', $product_field_type );
}
}
This kind of select field only works with a defined multiple
attribute and work with an array of values. so you can't use it for a simple ID. If you add to your select field multiple="multiple"
attribute it will work.
Also since Woocommerce 3 things have changed:
- There are better hooks to save the data.
- You can now use CRUD Objects and related methods.
The following code will work for multiple product IDs (an array of products IDs):
// Display a custom select field in "Linked Products" section
add_action( 'woocommerce_product_options_related', 'display_linked_products_data_custom_field' );
function display_linked_products_data_custom_field() {
global $product_object, $post;
?>
<p class="form-field">
<label for="subscription_toggle_products"><?php _e( 'Subscription Toggle Products', 'woocommerce' ); ?></label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="subscription_toggle_ids" name="_subscription_toggle_ids[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
<?php
$product_ids = $product_object->get_meta( '_subscription_toggle_ids' );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
if ( is_object( $product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select>
</p>
<?php
}
// Save the values to the product
add_action( 'woocommerce_admin_process_product_object', 'save_linked_products_data_custom_field_value', 10, 1 );
function save_linked_products_data_custom_field_value( $product ){
$data = isset( $_POST['_subscription_toggle_ids'] ) ? array_map( 'intval', (array) $_POST['_subscription_toggle_ids'] ) : array();
$product->update_meta_data( '_subscription_toggle_ids', $data );
}
Code goes in function.php file of your active child theme (active theme). Tested and works.
這篇關于在 Woocommerce 中添加自定義多選字段以管理產品選項設置的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!