【问题标题】:WordPress: WooCommerce email_order_items_table remove priceWordPress:WooCommerce email_order_items_table 删除价格
【发布时间】:2017-08-12 18:43:15
【问题描述】:

我是 PHP 新手,也是 WooCommerce 新手。 我想编辑woo-custom-emails 插件。 作为用户,我可以在输出产品名称的文本中设置{email_order_items_table},然后是其数量,然后是价格。我要去掉价格!

这是它在代码中的作用:

    // file: my_plugin/admin/class-wcemails-instance.php

    $this->find[]    = '{email_order_items_table}';
    $this->replace[] = $this->object->email_order_items_table();

这是该函数的作用:

    // file: woocommerce/includes/abstracts/abstract-wc-order.php

/**
 * Output items for display in html emails.
 *
 * @param array $args Items args.
 * @param null $deprecated1 Deprecated arg.
 * @param null $deprecated2 Deprecated arg.
 * @param null $deprecated3 Deprecated arg.
 * @param null $deprecated4 Deprecated arg.
 * @param null $deprecated5 Deprecated arg.
 * @return string
 */
public function email_order_items_table( $args = array(), $deprecated1 = null, $deprecated2 = null, $deprecated3 = null, $deprecated4 = null, $deprecated5 = null ) {
    ob_start();

    if ( ! is_null( $deprecated1 ) || ! is_null( $deprecated2 ) || ! is_null( $deprecated3 ) || ! is_null( $deprecated4 ) || ! is_null( $deprecated5 ) ) {
        _deprecated_argument( __FUNCTION__, '2.5.0' );
    }

    $defaults = array(
        'show_sku'      => false,
        'show_image'    => false,
        'image_size'    => array( 32, 32 ),
        'plain_text'    => false,
        'sent_to_admin' => false
    );

    $args     = wp_parse_args( $args, $defaults );
    $template = $args['plain_text'] ? 'emails/plain/email-order-items.php' : 'emails/email-order-items.php';

    wc_get_template( $template, apply_filters( 'woocommerce_email_order_items_args', array(
        'order'               => $this,
        'items'               => $this->get_items(),
        'show_download_links' => $this->is_download_permitted() && ! $args['sent_to_admin'],
        'show_sku'            => $args['show_sku'],
        'show_purchase_note'  => $this->is_paid() && ! $args['sent_to_admin'],
        'show_image'          => $args['show_image'],
        'image_size'          => $args['image_size'],
        'plain_text'          => $args['plain_text'],
        'sent_to_admin'       => $args['sent_to_admin']
    ) ) );

    return apply_filters( 'woocommerce_email_order_items_table', ob_get_clean(), $this );
}

这就是模板:

    // file: woocommerce/templates/emails/email-order-items.php

<?php
/**
 * Email Order Items
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/email-order-items.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see         https://docs.woocommerce.com/document/template-structure/
 * @author      WooThemes
 * @package     WooCommerce/Templates/Emails
 * @version     2.1.2
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

foreach ( $items as $item_id => $item ) :
    $_product     = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
    $item_meta    = new WC_Order_Item_Meta( $item, $_product );

    if ( apply_filters( 'woocommerce_order_item_visible', true, $item ) ) {
    ?>
    <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_order_item_class', 'order_item', $item, $order ) ); ?>">
        <td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; word-wrap:break-word;"><?php

            // Show title/image etc
            if ( $show_image ) {
                echo apply_filters( 'woocommerce_order_item_thumbnail', '<div style="margin-bottom: 5px"><img src="' . ( $_product->get_image_id() ? current( wp_get_attachment_image_src( $_product->get_image_id(), 'thumbnail') ) : wc_placeholder_img_src() ) .'" alt="' . esc_attr__( 'Product Image', 'woocommerce' ) . '" height="' . esc_attr( $image_size[1] ) . '" width="' . esc_attr( $image_size[0] ) . '" style="vertical-align:middle; margin-right: 10px;" /></div>', $item );
            }

            // Product name
            echo apply_filters( 'woocommerce_order_item_name', $item['name'], $item, false );

            // SKU
            if ( $show_sku && is_object( $_product ) && $_product->get_sku() ) {
                echo ' (#' . $_product->get_sku() . ')';
            }

            // allow other plugins to add additional product information here
            do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order, $plain_text );

            // Variation
            if ( ! empty( $item_meta->meta ) ) {
                echo '<br/><small>' . nl2br( $item_meta->display( true, true, '_', "\n" ) ) . '</small>';
            }

            // File URLs
            if ( $show_download_links ) {
                $order->display_item_downloads( $item );
            }

            // allow other plugins to add additional product information here
            do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order, $plain_text );

        ?></td>
        <td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo apply_filters( 'woocommerce_email_order_item_quantity', $item['qty'], $item ); ?></td>
        <td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
    </tr>
    <?php
}

if ( $show_purchase_note && is_object( $_product ) && ( $purchase_note = get_post_meta( $_product->id, '_purchase_note', true ) ) ) : ?>
    <tr>
        <td colspan="3" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo wpautop( do_shortcode( wp_kses_post( $purchase_note ) ) ); ?></td>
    </tr>
<?php endif; ?>

我可以替换 cmets 中描述的模板,但它会覆盖所有电子邮件以隐藏价格,我实际上希望在所有其他未通过插件发送的电子邮件中保留价格。我不知道如何设置这个条件。

所以这是我的问题:如何挂钩 email_order_items_table() 函数以不输出价格?

PS:如何查看 $this-&gt;object 内部的内容,或者如何以人类可读的形式输出 $thisobject 中的所有内容?

【问题讨论】:

    标签: php wordpress email woocommerce


    【解决方案1】:

    在 woocommerce 中,如果您将其放在主题上,所有模板都可以被覆盖, 所以:

    woocommerce/templates/emails/email-order-items.php
    

    如果你戴上它可以被覆盖:

    wp-content/themes/{your theme}/woocomerce/{the template}
    

    在这种情况下

    content/themes/{your theme}/woocommerce/emails/email-order-items.php
    

    然后你可以改变模板,所以它不绘制价格。

    从你的主题目录中的文件中取出这一行。小心一些时候你改变了插件目录上的东西,你会在下次更新时丢失它。

    <td class="td" style="text-align:left; vertical-align:middle; border: 1px solid #eee; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;"><?php echo $order->get_formatted_line_subtotal( $item ); ?></td>
    

    br

    【讨论】:

      【解决方案2】:

      您可以使用其中任何一个来查看以人类可读形式打印为数组的对象。

      echo '<pre>';print_r($this->object);exit;
      echo '<pre>';print_r($this);exit;
      

      或者您可能需要将对象记录到如下错误日志文件中

      error_log(print_r($this->object, 1), true);
      

      【讨论】:

        猜你喜欢
        • 2022-01-20
        • 2020-12-10
        • 2023-02-10
        • 2022-06-10
        • 2013-04-28
        • 1970-01-01
        • 2022-06-11
        • 1970-01-01
        • 2016-08-17
        相关资源
        最近更新 更多