使用 wc_display_item_meta 更改电子邮件订单项目的 html

发布于 2025-01-10 11:54:41 字数 2541 浏览 0 评论 0原文

最初输出的 html 是:

<tr class="order_item">
  <td class="td">
    Product Name
   <ul class="wc-item-meta">
     <li>
       <strong class="wc-item-meta-label">Label:</strong>
       <p>Value</p>
     </li>
     <!-- several more li--> 
   </ul>
 </td>
 <td class="td">1 </td>
 <td class="td">
    <span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">CHF</span>49.00</span>
 </td>
 </tr>

我想摆脱这种笨重且不适合移动设备的 ul、li 和 p 结构,并像这样简化 html:

<tr class="order_item">
    <th>Model</th>
    <td>Model Name</td>
</tr>
<tr>
    <th>Label</th>
    <td>Value</td>
</tr>
etc.

我的第一步是摆脱 email-order 中的 thead-tag-详细信息.php。我更喜欢像 th-tag 这样的标签。

我还需要更改 email-order-items.php 中的 html 结构,

但后来我陷入了函数 wc_display_item_meta 的困境。我不知道如何让这个函数显示上面显示的所需的 html 标记。

function wc_display_item_meta( $item, $args = array() ) {
$strings = array();
$html    = '';
$args    = wp_parse_args(
    $args,
    array(
        'before'       => '<tr class="order_item">',
        'after'        => '</tr>',
        'separator'    => '</tr><tr>',
        'value_before' => '<td class="wc-item-meta-value">',
        'value_after'  => '</td>',
        'echo'         => true,
        'autop'        => false,
        'my_label_before' => '<th class="wc-item-meta-label">',
        'my_label_after'  => ':</th> ',
    )
);
 
foreach ( $item->get_formatted_meta_data() as $meta_id => $meta ) {
    $value     = $args['autop'] ? wp_kses_post( $meta->display_value ) : wp_kses_post( make_clickable( trim( $meta->display_value ) ) );
    $strings[] = $args['my_label_before'] . wp_kses_post( $meta->display_key ) . $args['my_label_after'] . $args['value_before'] . $value . $args['value_before'];
}

if ( $strings ) {
    $html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
}

$html = apply_filters( 'woocommerce_display_item_meta', $html, $item, $args );

if ( $args['echo'] ) {
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    echo $html;
} else {
    return $html;
}
}

如何更改 foreach 循环以获得下面所需的 html 输出显示?

<tr>
  <th></th>
  <td></td>
</tr>

Originally the html that is output is:

<tr class="order_item">
  <td class="td">
    Product Name
   <ul class="wc-item-meta">
     <li>
       <strong class="wc-item-meta-label">Label:</strong>
       <p>Value</p>
     </li>
     <!-- several more li--> 
   </ul>
 </td>
 <td class="td">1 </td>
 <td class="td">
    <span class="woocommerce-Price-amount amount"><span class="woocommerce-Price-currencySymbol">CHF</span>49.00</span>
 </td>
 </tr>

I want to get rid of this unwieldy and mobile-unfriendly ul, li and p structure and simplify the html like so:

<tr class="order_item">
    <th>Model</th>
    <td>Model Name</td>
</tr>
<tr>
    <th>Label</th>
    <td>Value</td>
</tr>
etc.

My first move is to get rid of the thead-tag in email-order-details.php. I prefer labels like the th-tag.

I also need to change the html structure in email-order-items.php

But then i get stuck with the function wc_display_item_meta. I don't know how to make this function display the desired html markup displayed above.

function wc_display_item_meta( $item, $args = array() ) {
$strings = array();
$html    = '';
$args    = wp_parse_args(
    $args,
    array(
        'before'       => '<tr class="order_item">',
        'after'        => '</tr>',
        'separator'    => '</tr><tr>',
        'value_before' => '<td class="wc-item-meta-value">',
        'value_after'  => '</td>',
        'echo'         => true,
        'autop'        => false,
        'my_label_before' => '<th class="wc-item-meta-label">',
        'my_label_after'  => ':</th> ',
    )
);
 
foreach ( $item->get_formatted_meta_data() as $meta_id => $meta ) {
    $value     = $args['autop'] ? wp_kses_post( $meta->display_value ) : wp_kses_post( make_clickable( trim( $meta->display_value ) ) );
    $strings[] = $args['my_label_before'] . wp_kses_post( $meta->display_key ) . $args['my_label_after'] . $args['value_before'] . $value . $args['value_before'];
}

if ( $strings ) {
    $html = $args['before'] . implode( $args['separator'], $strings ) . $args['after'];
}

$html = apply_filters( 'woocommerce_display_item_meta', $html, $item, $args );

if ( $args['echo'] ) {
    // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    echo $html;
} else {
    return $html;
}
}

How do i change the foreach loop to get my desired html output display below?

<tr>
  <th></th>
  <td></td>
</tr>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

世界如花海般美丽 2025-01-17 11:54:41

上面提到的函数wc_display_item_meta有效。我不知道,我走在正确的轨道上。

但最好以不同的方式命名,然后在 «email-order-items.php» 中调用它。这样,wc_display_item_meta 的其他用途(例如在“order/order-details-item.php”中将不会受到标记的影响,该标记专门用于电子邮件)。

因此,在functions.php中,我有特殊的电子邮件html标记函数:

function my_display_item_meta( $item, $args = array() ) {
 etc.
}

在“email-order-items.php”中,我调用我的函数而不是wc_display_item元:

my_display_email_item_meta($item);

注意:您可以仔细查看标记您的电子邮件模板,以获得适合您要求的结果。由于 html 发生了变化,电子邮件样式也需要更新。

就我而言,woocommerce 生成的电子邮件在手机上看起来更好,并且根据屏幕呈现消息要容易得多,即响应式。

总而言之,我一开始就不应该问。这只是浪费资源。不过,我不会删除该帖子。但如果管理员另有决定,我不会介意。再见

The function wc_display_item_meta mentioned above works. I wasn’t aware, that i was on the right track.

But it is better to name it differently, and then call it in «email-order-items.php». This way, other uses of wc_display_item_meta (for instance in «order/order-details-item.php» won’t be affected be the markup, that is exclusively for e-mails).

Therefore in functions.php i have my special e-mail html markup function:

function my_display_item_meta( $item, $args = array() ) {
 etc.
}

and in «email-order-items.php» i call my function instead of wc_display_item meta:

my_display_email_item_meta($item);

Note: You may look closely at the mark up of your e-mail templates in order to achieve a result that suits your requirements. And since the html changes, the email styles need to be updated.

In my case the e-mail messages generated by woocommerce look better on mobile phones and it is much easier to render the message according to the screen, that is responsive.

To sum up, i shouldn’t have asked in the first place. This is only wasting resources. However, i won’t delete the post. But if admin decides otherwise, i wouldn’t mind. Ciao

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文