我正在尝试在 Woocommerce 产品变体中创建 4 个多选选项.
例如:我在卖树,想显示树可用的季节.所以我们有 4 个季节(春、夏、秋、冬),有些树有两个或树的季节.
我将此代码添加到我的functions.php,但它不会保存选定的选项.当我保存选项并重新加载页面时,选项再次变为空白.
我还想知道如何将单个产品页面(前端)上的选定选项显示为图标.
现在,带有选项的功能适用于产品变体.请查看此屏幕截图 (具有多选选项的产品变体):
我的代码:
//添加变化设置add_action('woocommerce_product_after_variable_attributes', 'variation_settings_fields', 10, 3);/*** 创建自定义字段类型**/函数 woocommerce_wp_select_multiple( $field ) {全球 $thepostid, $post, $woocommerce;$thepostid = 空( $thepostid )?$post->ID : $thepostid;$field['class'] = isset( $field['class'] ) ?$field['class'] : '选择短';$field['wrapper_class'] = isset( $field['wrapper_class'] ) ?$field['wrapper_class'] : '';$field['name'] = isset( $field['name'] ) ?$field['name'] : $field['id'];$field['value'] = isset( $field['value'] ) ?$field['value'] : ( get_post_meta( $thepostid, $field['id'], true ) ? get_post_meta( $thepostid, $field['id'], true ) : array() );echo '<p class="form-field '.esc_attr($field['id']).'_field'.esc_attr($field['wrapper_class']).'"><label for="'. esc_attr( $field['id'] ) . '">'.wp_kses_post( $field['label'] ) .'</label><select id="' . esc_attr( $field['id'] ) . '" name="' . esc_attr( $field['name'] ) . '" class="' .esc_attr( $field['class'] ) . '" multiple="multiple">';foreach ( $field['options'] as $key => $value ) {echo '<option value="' . esc_attr( $key ) . '" ' .( in_array( $key, $field['value'] ) ? 'selected="selected"' : '' ) .'>'.esc_html( $value ) .'</选项>';}回声'</选择>';if ( !empty( $field['description'] ) ) {if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {echo '<img class="help_tip" data-tip="' . esc_attr( $field['description'] ) . '" src="' . esc_url( WC()->plugin_url() ) . '/assets/images/help.png" height="16" width="16"/>';} 别的 {echo ''.wp_kses_post( $field['description'] ) .'</span>';}}回声'</p>';}/*** 为变体创建新字段**/函数variation_settings_fields( $loop, $variation_data, $variation ) {woocommerce_wp_select_multiple(数组('id' =>'季节_' .$variation->ID,'类' =>'季节','标签' =>__('季节', 'woocommerce'),'价值' =>get_post_meta( $variation->ID, '_season', true ),'选项' =>大批('春天' =>'春天','夏天' =>'夏天','秋天' =>'秋天','冬天' =>'冬天',)));}add_action( 'woocommerce_save_product_variation', 'save_variation_settings_fields', 10, 2 );函数 save_variation_settings_fields( $post_id ) {$select = $_POST["season_$post_id"];如果(!空($选择)){update_post_meta( $post_id, '_season', esc_attr( $select ) );}}处理多选字段的可变产品是另一回事,需要进行一些更改才能使其工作.我在以下之后做了 2 个回答:
因此在 WooCommerce 后端启用多选字段的主要功能将是:
function woocommerce_wp_multi_select( $field, $variation_id = 0 ) {全球 $thepostid, $post;如果( $variation_id == 0 )$the_id = 空( $thepostid )?$post->ID : $thepostid;别的$the_id = $variation_id;$field['class'] = isset( $field['class'] ) ?$field['class'] : '选择短';$field['wrapper_class'] = isset( $field['wrapper_class'] ) ?$field['wrapper_class'] : '';$field['name'] = isset( $field['name'] ) ?$field['name'] : $field['id'];$meta_data = may_unserialize( get_post_meta( $the_id, $field['id'], true ) );$meta_data = $meta_data ?$meta_data : 数组() ;$field['value'] = isset( $field['value'] ) ?$field['value'] : $meta_data;echo '<p class="form-field ' .esc_attr( $field['id'] ) .'_场地 ' .esc_attr( $field['wrapper_class'] ) .'><label for=''.esc_attr( $field['id'] ) .'>'.wp_kses_post( $field['label'] ) .'</label><select id="'.esc_attr( $field['id'] ) .'"名称="'.esc_attr( $field['name'] ) .'"类=".esc_attr( $field['class'] ) .'"多个=多个">';foreach ( $field['options'] as $key => $value ) {回声'<选项值=".esc_attr( $key ) .'"' .( in_array( $key, $field['value'] ) ? 'selected="selected"' : '' ) .'>'.esc_html( $value ) .'</选项>';}回声'</选择>';if ( !empty( $field['description'] ) ) {if ( isset( $field['desc_tip'] ) && false !== $field['desc_tip'] ) {echo 'plugin_url()).'/assets/images/help.png';高度=16"宽度=16"/>';} 别的 {echo ''.wp_kses_post( $field['description'] ) .'</span>';}}}代码位于活动子主题(或活动主题)的 function.php 文件中.此功能现在将处理任何类型的产品,包括产品变体.
相关:
2).对于所有其他产品类型(除了我们隐藏自定义字段的产品变体):
//为产品常规选项设置添加自定义字段(对于可变产品隐藏它)add_action('woocommerce_product_options_general_product_data', 'add_custom_settings_fields', 20);函数 add_custom_settings_fields() {全球 $post;echo '
以编程方式将可下载文件添加到 Woocommerce 产品Add programmatically a downloadable file to Woocommerce products(以编程方式将可下载文件添加到 Woocommerce 产品)
获取今天 Woocommerce 中每种产品的总订单数Get today#39;s total orders count for each product in Woocommerce(获取今天 Woocommerce 中每种产品的总订单数)
在 WooCommerce 和电话字段验证问题中添加自定义注Add Custom registration fields in WooCommerce and phone field validation issue(在 WooCommerce 和电话字段验证问题中添加自定义注册字段
在 Woocommerce 简单产品中添加一个将更改价格的选Add a select field that will change price in Woocommerce simple products(在 Woocommerce 简单产品中添加一个将更改价格的选择字段)
在 WooCommerce 3 中将自定义列添加到管理产品列表Add custom columns to admin products list in WooCommerce 3(在 WooCommerce 3 中将自定义列添加到管理产品列表)
自定义结帐“下订单"按钮输出htmlCustomizing checkout quot;Place Orderquot; button output html(自定义结帐“下订单按钮输出html)