Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Woocommerce 3+;_Php_Wordpress_Woocommerce_Custom Fields_Hook Woocommerce - Fatal编程技术网

Php 在Woocommerce 3+;

Php 在Woocommerce 3+;,php,wordpress,woocommerce,custom-fields,hook-woocommerce,Php,Wordpress,Woocommerce,Custom Fields,Hook Woocommerce,不久前,我的合作伙伴帮我在结帐页面上添加了一个自定义字段,我们想在人们预订我们的活动时问他们“你是怎么听说我们的?” 我们把它放在一起,但不久之后我们就不能正确地看到结果了 在发送给管理员的“新订单”电子邮件中,会出现“你是如何听说我们的”标签,但没有回答 在订单页面中,它出现在两个位置: 1) 在账单信息下。标签在那里。值为:数组 2) 在“自定义字段”部分下。标签在那里。值为:选项0或选项1等 我的第一个问题是如何解决这个问题,以便在电子邮件和订单页面上显示选项,例如“社交媒体” 我的第二个

不久前,我的合作伙伴帮我在结帐页面上添加了一个自定义字段,我们想在人们预订我们的活动时问他们“你是怎么听说我们的?”

我们把它放在一起,但不久之后我们就不能正确地看到结果了

在发送给管理员的“新订单”电子邮件中,会出现“你是如何听说我们的”标签,但没有回答

在订单页面中,它出现在两个位置:

1) 在账单信息下。标签在那里。值为:数组

2) 在“自定义字段”部分下。标签在那里。值为:选项0或选项1等

我的第一个问题是如何解决这个问题,以便在电子邮件和订单页面上显示选项,例如“社交媒体”

我的第二个问题是,我如何从整体上分析这些数据?我们希望能够回答以下问题:;2018年有多少人选择了选项1?;12月份有多少人选择了方案2?;有多少人购买了选择方案3的x?;有多少人购买了y并选择了选项4

提前感谢您的帮助

/**
* Add the field to the checkout
*/
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );

function my_custom_checkout_field( $checkout ) {

echo '<div id="my_custom_checkout_field"><h2>' . __('Please help us understand our customers so that we can improve future events (Optional)') . '</h2>';

woocommerce_form_field( 'hearaboutus', array(
    'type'          => 'select',
    'class'         => array('my-field-class form-row-wide'),
    'label'         => __('How did you hear about us? &nbsp;'),
    'options'       => array(
 'Option_0' => 'Please select...',
 'option_1' => 'Social Media (e.g Facebook)',
 'option_2' => 'Search Engine (e.g Google)',
 'option_3' => 'Meditation Class',
 'option_4' => 'Leaflets/Flyers/Posters',
 'option_5' => 'Website',
 'option_6' => 'Email Newsletter',
 'option_7' => 'Other',
 )
    ), $checkout->get_value( 'hearaboutus' ));

echo '</div>';

}

/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );

function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['hearaboutus'] ) ) {
    update_post_meta( $order_id, 'How did you hear about us?', 
sanitize_text_field( $_POST['hearaboutus'] ) );
}
}

/* Add the fields to order email */

add_filter( 'woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys' );
function my_custom_checkout_field_order_meta_keys( $keys ) {
            echo '<h3>How did you hear about us?:</h3>';
    $keys[''] = 'hearaboutus';
    return $keys;
}

/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );

function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('How did you hear about us?').':</strong> ' . get_post_meta( $order->id, $key='', 'hearaboutus', true ) . '</p>';
}
/**
*将该字段添加到签出
*/
添加操作(“订单注释后的woocommerce”、“我的自定义”签出字段”);
函数my_custom_checkout_字段($checkout){
echo“”。uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
woocommerce_表单_字段('hearaboutus',数组(
'类型'=>'选择',
'class'=>array('my-field-class表单行宽'),
“label”=>(你是怎么听说我们的?),
“选项”=>数组(
'选项0'=>'请选择…',
“选项1”=>“社交媒体(如Facebook)”,
'选项2'=>'搜索引擎(如谷歌)',
'选项3'=>'冥想课程',
“选项4”=>“传单/传单/海报”,
'选项5'=>'网站',
'选项6'=>'电子邮件通讯',
'选项7'=>'其他',
)
),$checkout->get_值('hearaboutus');
回声';
}
/**
*使用字段值更新订单元
*/
添加操作('woocommerce\u checkout\u update\u order\u meta'、'my\u custom\u checkout\u field\u update\u order\u meta');
函数my\u custom\u checkout\u field\u update\u order\u meta($order\u id){
如果(!empty($_POST['hearaboutus'])){
更新“post”meta($order\u id,“你是怎么听说我们的?”,
清理文本字段($_POST['hearaboutus']);
}
}
/*将字段添加到订单电子邮件*/
添加过滤器(“woocommerce\u email\u order\u meta\u keys”、“my\u custom\u checkout\u field\u order\u meta\u keys”);
功能我的\自定义\签出\字段\顺序\元\键($keys){
echo“你是怎么听说我们的?”;
$keys['']=“hearaboutus”;
返回$keys;
}
/**
*在订单编辑页面上显示字段值
*/
添加操作('woocommerce\u admin\u order\u data\u after\u billing\u address'、'my\u custom\u checkout\u field\u display\u admin\u order\u meta',10,1);
函数my\u custom\u checkout\u field\u display\u admin\u order\u meta($order){
回显“”。(你是怎么听说我们的?)::。获取帖子元($order->id,$key='',hearaboutus',true)。

; }
您的代码中有一些错误…请尝试以下重新访问的代码:

// get "hearaboutus" select field options data
function wc_get_hearaboutus_options(){
    return array(
        ''          => 'Please select...',
        'option_1'  => 'Social Media (e.g Facebook)',
        'option_2'  => 'Search Engine (e.g Google)',
        'option_3'  => 'Meditation Class',
        'option_4'  => 'Leaflets/Flyers/Posters',
        'option_5'  => 'Website',
        'option_6'  => 'Email Newsletter',
        'option_7'  => 'Other',
    );
}

// Add the field to the checkout
add_action( 'woocommerce_after_order_notes', 'my_custom_checkout_field' );
function my_custom_checkout_field( $checkout ) {

    echo '<div id="my_custom_checkout_field"><h3>' . __('Please help us understand our customers so that we can improve future events (Optional)') . '</h3>';

    woocommerce_form_field( '_hearaboutus', array(
        'type'    => 'select',
        'class'   => array('my-field-class form-row-wide'),
        'label'   => __('How did you hear about us? &nbsp;'),
        'options' => wc_get_hearaboutus_options(),
    ), $checkout->get_value( '_hearaboutus' ) );

    echo '</div>';

}

// Update the order meta with field value
add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_create_order', 10, 2 );
function custom_checkout_field_create_order( $order, $data ) {
    if ( isset($_POST['_hearaboutus']) && ! empty($_POST['_hearaboutus']) ) {
         $order->update_meta_data( '_hearaboutus', sanitize_text_field($_POST['_hearaboutus']) );
    }
}

// Add the fields to order email
add_action('woocommerce_email_order_details', 'action_after_email_order_details', 25, 4 );
function action_after_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
    if( $hearaboutus = $order->get_meta('_hearaboutus') ) {
        // The data
        $label = __('How did you hear about us?');
        $value = wc_get_hearaboutus_options()[$hearaboutus];

        // The HTML Structure
        $html_output = '<h2>' . __('Extra data') . '</h2>
        <div class="discount-info"><table cellspacing="0" cellpadding="6"><tr>
        <th>' . $label . '</th><td>' . $value . '</td>
        </tr></tbody></table></div><br>';

        // The CSS styling
        $styles = '<style>
            .discount-info table{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
                color: #737373; border: 2px solid #e4e4e4; margin-bottom:8px;}
            .discount-info table th, table.tracking-info td{ text-align: left; color: #737373; border: none; padding: 12px;}
            .discount-info table td{ text-align: left; color: #737373; border: none; padding: 12px; }
        </style>';

        // The Output CSS + HTML
        echo $styles . $html_output;
    }
}

// Display field value on the order edit page
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ) {
    if( $hearaboutus = $order->get_meta('_hearaboutus') ) {
        $value = wc_get_hearaboutus_options()[$hearaboutus];
        echo '<p><strong>'.__('How did you hear about us?').'</strong> ' . $value . '</p>';
    }
}
//获取“hearaboutus”选择字段选项数据
函数wc_get_hearaboutus_options(){
返回数组(
''=>'请选择…',
“选项1”=>“社交媒体(如Facebook)”,
'选项2'=>'搜索引擎(如谷歌)',
'选项3'=>'冥想课程',
“选项4”=>“传单/传单/海报”,
'选项5'=>'网站',
'选项6'=>'电子邮件通讯',
'选项7'=>'其他',
);
}
//将该字段添加到签出
添加操作(“订单注释后的woocommerce”、“我的自定义”签出字段”);
函数my_custom_checkout_字段($checkout){
echo“”。uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
woocommerce_form_字段(''uHearAboutus',数组(
'类型'=>'选择',
'class'=>array('my-field-class表单行宽'),
“label”=>(你是怎么听说我们的?),
“选项”=>wc\u get\u hearaboutus\u options(),
),$checkout->get_value('u hearaboutus');
回声';
}
//使用字段值更新订单元
添加操作('woocmerce\u checkout\u create\u order'、'custom\u checkout\u field\u create\u order',10,2);
函数自定义\签出\字段\创建\订单($order$data){
如果(设置为($发布['''u hearaboutus'])和&!空($发布[''u hearaboutus'])){
$order->update_meta_data('u hearaboutus',清理_text_字段($_POST['u hearaboutus'));
}
}
//将字段添加到订单电子邮件
添加行动(“商业、电子邮件、订单、详细信息”、“电子邮件、订单、详细信息后的行动”,25、4);
功能操作\u在\u电子邮件\u订单\u详细信息($order、$sent\u to\u admin、$plain\u text、$email)之后\u{
如果($hearaboutus=$order->get_meta(“'u hearaboutus')){
//数据
$label=u uuu('你是怎么听说我们的?');
$value=wc_get_hearaboutus_options()[$hearaboutus];
//HTML结构
$html\u输出=''。\uuuu('额外数据')。'
“.$label.”“.$value.”

",; //CSS样式 $styles='1 .折扣信息表{宽度:100%;字体系列:\'Helvetica Neue\',Helvetica,Roboto,Arial,无衬线; 颜色:#737373;边框:2px实心#e4;边距底部:8px;} .折扣信息表th,table.tracking-info td{文本对齐:左;颜色:#737373;边框:无;填充:12px;} .折扣信息表td{文本对齐:左;颜色:#737373;边框:无;填充:12px;} '; //输出CSS+HTML echo$style.$html\u输出; } } //在订单编辑页面上显示字段值 添加操作('woocommerce\u admin\u order\u data\u after\u billing\u address'、'my\u custom\u checkout\u field\u display\u admin\u order\u meta',10,1); 函数my\u custom\u checkout\u field\u display\u admin\u order\u meta($order){ 如果($hearaboutus=$order->get_meta(“'u hearaboutus')){