将自定义 Select2 搜索字段添加到 WooCommerce 产品数据元框中
我已成功在编辑产品页面中添加产品搜索字段,但我需要能够清除选择。我怎样才能实现这个目标?
或者:如果我在选择组件上设置了多个产品,如何设法只能选择 1 个产品?
我的代码如下所示:
add_action('woocommerce_product_data_panels', function() {
global $post, $woocommerce;
$product_ids = get_post_meta( $post->ID, '_stock_sync_data_ids', true );
if( empty($product_ids) )
$product_ids = array();
?>
<div id="stock_sync_data" class="panel woocommerce_options_panel hidden">
<?php if ( $woocommerce->version >= '3.0' ) : ?>
<p class="form-field stock_sync_data">
<label for="stock_sync_data"><?php _e( 'Select product to sync with. (Only select 1 item)', 'woocommerce' ); ?></label>
<select class="wc-product-search" style="width: 50%;" id="stock_sync_data" name="stock_sync_data[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
<?php
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> <?php echo wc_help_tip( __( 'Select product to syncronize stock.', 'woocommerce' ) ); ?>
</p>
<?php endif; ?>
</div>
<?php
});
I have successfully added a product search field in my edit product page, but I need to be able to clear the selection. How can I achieve this?
Alternatively: how do I manage to only be able to select 1 product if I set multiple on the select component?
My code looks like below:
add_action('woocommerce_product_data_panels', function() {
global $post, $woocommerce;
$product_ids = get_post_meta( $post->ID, '_stock_sync_data_ids', true );
if( empty($product_ids) )
$product_ids = array();
?>
<div id="stock_sync_data" class="panel woocommerce_options_panel hidden">
<?php if ( $woocommerce->version >= '3.0' ) : ?>
<p class="form-field stock_sync_data">
<label for="stock_sync_data"><?php _e( 'Select product to sync with. (Only select 1 item)', 'woocommerce' ); ?></label>
<select class="wc-product-search" style="width: 50%;" id="stock_sync_data" name="stock_sync_data[]" data-placeholder="<?php esc_attr_e( 'Search for a product…', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
<?php
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> <?php echo wc_help_tip( __( 'Select product to syncronize stock.', 'woocommerce' ) ); ?>
</p>
<?php endif; ?>
</div>
<?php
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的代码尝试非常接近,但我稍微调整了它并删除了一些不必要的检查(例如 WooCommerce 版本,因为它是 2017 年的 3.0),
所以您得到:
结果:
所以你得到:
相关:从 WooCommerce 后端的 Select2 字段保存值的问题
You're very close with your code attempt, but I've tweaked it slightly and removed some unnecessary checks (like the WooCommerce version, as it's 3.0.. from 2017)
So you get:
Result:
So you get:
Related: Issue with saving value from Select2 field in WooCommerce backend