我正在使用 Woocommerce,并且在管理面板中构建了一个选择框。我通过平面文件在选择框中填充信息。 一切正常(几乎)。
我坚持的部分是在我选择我想要的“选择”和保存我得到数组 $key 位置而不是实际 $值。我很接近,但我不能把我的手指放在上面。
更新:这是我的完整代码:
function woo_add_custom_admin_product_tab() {
?>
<li class="custom_tab"><a href="#custom_tab_data"><?php _e('Additional Information', 'woocommerce'); ?></a></li>
<?php
}
add_action( 'woocommerce_product_write_panel_tabs', 'woo_add_custom_admin_product_tab' );
function woo_add_custom_admin_fields() {
global $woocommerce, $post;
echo '<div id="custom_tab_data" class="panel woocommerce_options_panel">';
echo '<div class="options_group">';
// Select - Breed1
if (file_exists ( plugin_dir_path(__FILE__) .'breed.txt')) {
$breedData = file_get_contents ( plugin_dir_path(__FILE__) .'breed.txt');
$breedArray = explode ("\n", $breedData);
}
woocommerce_wp_select(array(
'id' => '_select_breed1',
'label' => __( 'Select Primary Breed', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Select the primary breed of the pet.', 'woocommerce' ),
'options' => $breedArray
) );
echo '</div>';
echo '</div>';
}
add_action( 'woocommerce_product_write_panels', 'woo_add_custom_admin_fields' );
// Save Fields;
function woo_add_custom_general_fields_save( $post_id ){
// Text Field - Pet Name
$woocommerce_text_field = $_POST['_pet_name'];
if( !empty( $woocommerce_text_field ) )
update_post_meta( $post_id, '_pet_name', esc_attr( $woocommerce_text_field ) );
// Select Field - Breed
$woocommerce_select = $_POST['_select_breed1'];
if( !empty( $woocommerce_select ) )
update_post_meta( $post_id, '_select_breed1', esc_attr( $woocommerce_select ) );
}
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
我的 breed.txt 文件包含 3 行(项目):
Please Select a breed...
Abyssinian
Affenpinscher
生成的数组如下所示:
Array (
[0] => Please Select a breed...
[1] => Abyssinian
[2] => Affenpinscher
)
例如,当我选择 "Affenpinscher" 时,我得到的值是 "2" 而不是“Affenpinscher” .
我做错了什么?我该如何解决这个问题?
谢谢
最佳答案
— 已更新— (已测试且有效)
这绝对是下拉选择器的正常行为
The changes are: You have now in in 用法举例(获取这个值):<select> 。您只需在代码中添加一些小东西,使其以不同的方式工作。
— first, when the array of values from the external text file is available, I store it in wordpress options.
— Second, In the last saving function, I get the stored array and with the selected key that I get from $_POST['_select_breed_key1'];, I retrieve the corresponding value that I store in a new entry (new row in wp_postmeta table.//Create the fields
function woo_add_custom_admin_fields() {
global $woocommerce, $post;
echo '<div id="custom_tab_data" class="panel woocommerce_options_panel">';
echo '<div class="options_group">';
// Select - Breed1
if (file_exists ( plugin_dir_path(__FILE__) .'breed.txt')) {
$breedData = file_get_contents ( plugin_dir_path(__FILE__) .'breed.txt');
$breedArray = explode ("\n", $breedData);
//Storing the array in wp_options table
if( get_option( 'wc_product_add_info_tab' ) )
update_option( 'wc_product_add_info_tab', $breedArray );
else
add_option( 'wc_product_add_info_tab', $breedArray );
}
woocommerce_wp_select( array(
'id' => '_select_breed_key1',
'label' => __( 'Select Primary Breed', 'woocommerce' ),
'desc_tip' => 'true',
'description' => __( 'Select the primary breed of the pet.', 'woocommerce' ),
'options' => $breedArray
) );
echo '</div>';
echo '</div>';
}
add_action( 'woocommerce_product_write_panels', 'woo_add_custom_admin_fields' );
// Save Created Fields;
function woo_add_custom_general_fields_save( $post_id ){
// Select Field - Breed
$wc_select = $_POST['_select_breed_key1'];
if( !empty( $wc_select ) )
update_post_meta( $post_id, '_select_breed_key1', esc_attr( $wc_select ) );
// Saving the corresponding value (from "$wc_select" selected key) to database
if(get_option('wc_product_add_info_tab')) {
// Getting the array
$breed_arr = get_option('wc_product_add_info_tab');
// Saving the corresponding value
update_post_meta( $post_id, '_select_breed_value1', $breed_arr[$wc_select] );
}
}
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
wp_postmeta table for a Product ID (post_id), 2 meta_keys:
- '_select_breed_key1' that stores the selected key
- '_select_breed_value1' that stores the corresponding value<?php
// Third parameter is set to "true" as it is a string (Not an array)
$breed_value1 = get_post_meta( $post_id, '_select_breed_value1', true );
echo $breed_value1;
?>
关于php - WooCommerce - 提交后检索选择框的正确数据值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39502403/
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
状态:我正在构建一个应用程序,其中需要一个可供用户选择颜色的字段,该字段将包含RGB颜色代码字符串。我已经测试了一个看起来很漂亮但效果不佳的。它是“挑剔的颜色”,并托管在此存储库中:https://github.com/Astorsoft/picky-color.在这里我打开一个关于它的一些问题的问题。问题:请建议我在Rails3应用程序中使用一些颜色选择器。 最佳答案 也许页面上的列表jQueryUIDevelopment:ColorPicker为您提供开箱即用的产品。原因是jQuery现在包含在Rails3应用程序中,因此使用基
我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_
无论您是想搭建桌面端、WEB端或者移动端APP应用,HOOPSPlatform组件都可以为您提供弹性的3D集成架构,同时,由工业领域3D技术专家组成的HOOPS技术团队也能为您提供技术支持服务。如果您的客户期望有一种在多个平台(桌面/WEB/APP,而且某些客户端是“瘦”客户端)快速、方便地将数据接入到3D应用系统的解决方案,并且当访问数据时,在各个平台上的性能和用户体验保持一致,HOOPSPlatform将帮助您完成。利用HOOPSPlatform,您可以开发在任何环境下的3D基础应用架构。HOOPSPlatform可以帮您打造3D创新型产品,HOOPSSDK包含的技术有:快速且准确的CAD
本教程将在Unity3D中混合Optitrack与数据手套的数据流,在人体运动的基础上,添加双手手指部分的运动。双手手背的角度仍由Optitrack提供,数据手套提供双手手指的角度。 01 客户端软件分别安装MotiveBody与MotionVenus并校准人体与数据手套。MotiveBodyMotionVenus数据手套使用、校准流程参照:https://gitee.com/foheart_1/foheart-h1-data-summary.git02 数据转发打开MotiveBody软件的Streaming,开始向Unity3D广播数据;MotionVenus中设置->选项选择Unit
文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co
我正在尝试在Rails上安装ruby,到目前为止一切都已安装,但是当我尝试使用rakedb:create创建数据库时,我收到一个奇怪的错误:dyld:lazysymbolbindingfailed:Symbolnotfound:_mysql_get_client_infoReferencedfrom:/Library/Ruby/Gems/1.8/gems/mysql2-0.3.11/lib/mysql2/mysql2.bundleExpectedin:flatnamespacedyld:Symbolnotfound:_mysql_get_client_infoReferencedf
文章目录1.开发板选择*用到的资源2.串口通信(个人理解)3.代码分析(注释比较详细)1.主函数2.串口1配置3.串口2配置以及中断函数4.注意问题5.源码链接1.开发板选择我用的是STM32F103RCT6的板子,不过代码大概在F103系列的板子上都可以运行,我试过在野火103的霸道板上也可以,主要看一下串口对应的引脚一不一样就行了,不一样的就更改一下。*用到的资源keil5软件这里用到了两个串口资源,采集数据一个,串口通信一个,板子对应引脚如下:串口1,TX:PA9,RX:PA10串口2,TX:PA2,RX:PA32.串口通信(个人理解)我就从串口采集传感器数据这个过程说一下我自己的理解,
SPI接收数据左移一位问题目录SPI接收数据左移一位问题一、问题描述二、问题分析三、探究原理四、经验总结最近在工作在学习调试SPI的过程中遇到一个问题——接收数据整体向左移了一位(1bit)。SPI数据收发是数据交换,因此接收数据时从第二个字节开始才是有效数据,也就是数据整体向右移一个字节(1byte)。请教前辈之后也没有得到解决,通过在网上查阅前人经验终于解决问题,所以写一个避坑经验总结。实际背景:MCU与一款芯片使用spi通信,MCU作为主机,芯片作为从机。这款芯片采用的是它规定的六线SPI,多了两根线:RDY和INT,这样从机就可以主动请求主机给主机发送数据了。一、问题描述根据从机芯片手