![](/img/trans.png)
[英]Add a multi-select field for featured Posts in WooCommerce Product Category settings
[英]Add a custom multi-select field to admin product options settings in Woocommerce
我遵循了這個答案How to add more custom field in Linked Product of Woocommerce將自定義選擇字段添加到我在 Woocommerce 的鏈接產品屏幕上。 這個新字段的元鍵是subscription_toggle_product
。
它工作正常,但是一旦您選擇了產品,就無法刪除它(沒有“空”選項或十字符號)。 我不知道如何允許取消選擇。 我試過添加一個帶有空值的<option>
,但它沒有用。
這是我的代碼:
// 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 );
}
}
這種選擇字段僅適用於定義的multiple
屬性並使用值數組。 所以你不能將它用於簡單的 ID。 如果您添加到您的選擇字段multiple="multiple"
屬性,它將起作用。
此外,自從 Woocommerce 3 事情發生了變化:
- 有更好的鈎子來保存數據。
- 您現在可以使用 CRUD 對象和相關方法。
以下代碼適用於多個產品 ID(產品 ID 數組):
// 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 );
}
代碼位於活動子主題(活動主題)的 function.php 文件中。 測試和工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.