我创建了一个名为[pureair]的contact Form7标记,并在其中显示了一个计算器。用户可以添加房间和窗户来计算所需的值。
该表单位于Wordpress插件样板部分中
<div id="depcore-pureair-caclulator" class='depcore-pureair-caclulator'>
<section class="room" data-room-number='1'>
<h3 class="room-title"><?= __('Room', 'depcore-pureair') ?></h3>
<div class="room-fields">
<p class="form-field"><label for="room-height-1"><?= __('Height', 'depcore-pureair') ?></label><input type="number" class='room' name="pureair[][room-height-1]" id="room-height-1" min=1 step=1> cm</p>
<p class="form-field"><label for="room-width-1"><?= __('Width', 'depcore-pureair') ?></label><input type="number" class='room' name="pureair[][room-width-1]" id="room-width-1" min=1 step=1> cm</p>
<p class="form-field"><label for="room-length-1"><?= __('Length', 'depcore-pureair') ?></label><input type="number" class='room' name="pureair[][room-length-1]" id="room-length-1" min=1 step=1> cm</p>
<p class="calculation-result"><label><?= __('Volume', 'depcore-pureair') ?></label> <span></span>m<sup>3</sup></p>
</div>
<div class="windows">
<h3 class="window-title"><?= __('Glass', 'depcore-pureair') ?></h3>
<div class="window" data-window-number="1">
<p class="form-field"><label for="room-1-window-1-height"><?= __('Height', 'depcore-pureair') ?></label><input class='window' type="number" name="pureair[][room-1-window-1-height]" id="room-1-window-1-height" min=1 step=1> cm</p>
<p class="form-field"><label for="room-1-window-1-width"><?= __('Width', 'depcore-pureair') ?></label><input class='window' type="number" name="pureair[][room-1-window-1-width]" id="room-1-window-1-width" min=1 step=1> cm</p>
<p class="window-calculation-result"><label><?= __('Surface area', 'depcore-pureiar') ?></label><span></span>m<sup>2</sup></p>
<div class="window-actions">
<div class="remove-window depcore-remove-button"><svg viewBox='0 0 30 29'>
<use xlink:href='#minus-icon'></use>
</svg><span><?= __('Remove window', 'depcore-pureair') ?></span></div>
<div class="add-window depcore-add-button"><svg viewBox='0 0 30 29'>
<use xlink:href='#plus-icon'></use>
</svg><span><?= __('Add window', 'depcore-pureair') ?></span></div>
</div>
</div>
</div>
<div class="depcore-pureair-warning">
<p><?= __('This area is too small to effectively clear the room. Add a window', 'depcore-pureiar') ?></p>
</div>
<div class="room-actions">
<div class="add-room depcore-add-button"><svg viewBox='0 0 28 29'>
<use xlink:href='#plus-icon'></use>
</svg><span><?= __('Add room', 'depcore-pureair') ?></span></div>
<div class="remove-room depcore-remove-button"><svg viewBox='0 0 28 29'>
<use xlink:href='#minus-icon'></use>
</svg><span><?= __('Remove room', 'depcore-pureair') ?></span></div>
</div>
</section>
</div>我使用筛选器$this->loader>add_filter('wpcf7_special_mail_tags', $plugin_admin, 'calculator_wpcf7_pureair_mail_tag', 10, 3 );来显示电子邮件中的字段
public function calculator_wpcf7_pureair_mail_tag($output, $name, $html){
$name = preg_replace('/^wpcf7\./', '_', $name); // for back-compat
$submission = WPCF7_Submission::get_instance();
if (! $submission) {
return $output;
}
if ('pureair' == $name) {
return $submission->get_posted_data("pureair");
}
return $output;
}问题是电子邮件中的值显示为逗号分隔的字符串(例如270,200,300)。我尝试使用$this->loader>add_filter('wpcf7_posted_data', $plugin_admin, 'calculator_wpcf7_posted_data');过滤器,但是所有的值都被删除了。
我想要实现的是循环通过数组,并在电子邮件消息中创建一个带有数据的格式化结果。例如
1号房间高:270 xxx,宽:200 xxx,长:400 xxx窗口: 1:高:90 xxx,宽:110 xxx费用:xxx
我已经搜索过了,但是找不到ho来获取过滤器中的数组形式的值。
在深入研究Howard E.的建议之后,我尝试使用wpcf7_before_send_mail,如下所示,以测试是否可以更改这些值
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$posted_data = $submission->get_posted_data();
foreach ($posted_data as $key => $value) {
if($key == 'pureair') $posted_data['pureair'] = "<table><thead><tr><th>test</th></thead></table>";
if($key == "your-name") $posted_data['your-name'] = "blabla@pl.pl";
}
}但这不管用。知道为什么吗?
发布于 2021-11-30 12:55:17
如果使用Smart Grid-layout扩展,一种实现类似结果的方法。它允许您构建重复的选项卡式表单部分以及重复的字段表构造(有关如何实现这一点,请参阅此online tutorial )。
您的表单将有一个重复的房间选项卡式部分,每个房间有一个选项卡,其中包含高度/长度/宽度字段,以及窗口的每个选项卡中的表结构。表格的每一行都有window-height/window-width,允许用户向每个房间添加多个窗口。此外,每个选项卡还可以有一个只读的room-cost字段。
然后,您的用户将能够添加多个房间选项卡,并在每个选项卡中显示房间的尺寸以及多排窗户。用户每次向表结构添加新行时,都会在表上触发一个事件,允许您根据添加的房间/窗口在room-cost字段中显示计算值。
提交表单时,表字段以房间数组和窗口数组的形式提交。您可以将这些字段在通知电子邮件(请参阅此tutorial)中的显示方式过滤为HTML <table/>,这是在HTML邮件正文中显示表结构的唯一方法,如下所示
add_filter( 'cf7sg_mailtag_pureair', 'filter_cf7_mailtag_pureair', 10, 3);
function filter_cf7_mailtag_pureair($tag_replace, $submitted, $cf7_key){
//$submitted an array of submitted fields
if('my-pureair-form'==$cf7_key ){
$tag_replace = '<table>'.PHP_EOL;
foreach($submitted['room-height'] as $idx=>$height){ //tables_to_repair.
$tag_replace .= '<tr><td>Room '.($idx+1).'</td>';
$tag_replace .= '<td>height: '.$height.'cm, width: '.$submitted['room-width'][$idx].'cm, length: '.$submitted['room-length'][$idx].'cm </td>';
foreach($submitted['window-height'][$idx] as $rdx=>$wheight){
$tag_replace .= '<td>Windows: '.($rdx+1).': height: '.$wheight.'cm, width: '.$submitted['window-width'][$idx][$rdx].'cm </td>';
}
$tag_replace .= 'Cost: '.$submitted['room-cost'].'USD</tr>'.PHP_EOL;
}
$tag_replace .= '</table>'.PHP_EOL;
}
return $tag_replace;
}https://stackoverflow.com/questions/70072252
复制相似问题