【问题标题】:Add "description" field contents for variable product to woocommerce "Completed order" email将可变产品的“描述”字段内容添加到 woocommerce“已完成订单”电子邮件
【发布时间】:2021-05-22 23:50:50
【问题描述】:

我正在销售多种产品,每种产品都有 2 个变体,每个变体都需要在“已完成”电子邮件中包含一段自定义文本(嵌入 URL)。 大量自定义电子邮件:每个产品变体。我找到了很多 functions.php 的选项,但它们都是多年前的 woo 版本。

非常流行的“Woo Custom Emails Per Product”插件没有每个变体的功能。我不想让每个变体都成为自己的产品(因此可以使用该插件),因为我希望每个产品都有一个单独的产品页面,顾客可以在其中选择他们想要的变体。

所以我决定为每个变体添加信息的最佳方式是在变体的“描述”字段中。

这是我想要的,在我认为的 woocommerce_email_order_items_table 之上:

screen grab of email showing where text should go

我尝试将其添加到 functions.php 但它是从 2015 年开始的,用于“处理”而不是“完成”电子邮件:

add_filter( 'woocommerce_product_variation_title_include_attributes', '__return_false' );
function render_product_description($item_id, $item, $order){
    $_product = $order->get_product_from_item( $item );
    echo "<br>" . $_product->post->post_content; 

}

add_action('woocommerce_order_item_meta_end', 'render_product_description',10,3);

我试过这个,但它是多年前的,并没有完成我需要的: Add the product description to WooCommerce email notifications

这很接近:Add Custom Product Field in WooCommerce 'Order Completed' Emails,但不是我想要的,因为没有特定于每个变体的自定义字段;似乎每个变体的唯一自定义是“描述”。

我想找到一种方法来编辑电子邮件模板,因为我认为这将是最理想的方式。如果它可以简单地列出订购的每个项目变体的描述内容,我可以将该文本格式化为对顾客不言自明,然后订单摘要框(带有产品/数量/价格)将保持干净并仅列出项目.

这是我设置的测试页面:https://www.chambermusicpittsburgh.org/concerts-and-tickets-new-store/。只有 Escher 和 Dover 产品具有变量,在 Web 链接的描述中带有测试人员短语和 URL(如果您选择该选项,它将弹出,但我最终将使用 CSS 将其隐藏在这里,但我已将其留待测试)。

我觉得在电子邮件中添加变体描述应该超级直接/简单,也许我没有足够的经验或没有找对地方,但那条特定的数据似乎很难挂钩和显示在订单确认电子邮件中。

谢谢!

【问题讨论】:

  • 直接从 Automattic 更新:“这可以使用自定义代码完成,但需要大量工作。” 直接从 WooCommerce 更新:“这是一个相当复杂的开发主题。”我只是困惑为什么产品变体中的描述字段似乎没有挂钩或 API 调用,可以插入到电子邮件模板中。我显然对 WooCommerce 没有足够的经验,不知道为什么会这样。感谢任何帮助!

标签: php wordpress woocommerce


【解决方案1】:

这可行,但在处理和已完成的邮件和管理邮件中都添加了描述。状态检查$email-&gt;id == 'customer_completed_order' 似乎有问题。我添加了$status = $order-&gt;get_status(); 并将其更改为寻找$status == "completed"。它对我有用,但如果这是一个好方法,请添加您的想法或 cmets。

add_action( 'woocommerce_email_before_order_table', 'add_content_before_order_table', 99, 4 );
function add_content_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {

  // checking if it's the order status we want
    $status = $order->get_status();
    if ( $status == "completed" ) {
        
        // initializes the array with all product descriptions
        $descriptions = array();

        // for each product ordered
        foreach ( $order->get_items() as $order_item  ) {
            $product = $order_item->get_product();
            // get the product description
            if ( $product->get_description() ) {
                $descriptions[] = $product->get_description();
            }
        }

        // if the $descriptions is not empty it shows it
        if ( ! empty( $descriptions ) ) {
            foreach ( $descriptions as $content ) {
                echo '<p>' . $content . '</p>';
            }
        }

    }
}

【讨论】:

    【解决方案2】:

    此时要向电子邮件模板添加自定义内容:

    您可以使用woocommerce_email_before_order_table 挂钩。请参阅here 了解更多信息。

    下面的函数会得到每个订购产品的描述 (如果描述不为空)

    描述是从产品变化中获得的不是 可变产品

    // adds the product description before the order table in the completed order email
    add_action( 'woocommerce_email_before_order_table', 'add_content_before_order_table', 99, 4 );
    function add_content_before_order_table( $order, $sent_to_admin, $plain_text, $email ) {
    
        // only for the email of the completed order
        if ( ! $email->id == 'customer_completed_order' ) {
            return;
        }
    
        // initializes the array with all product descriptions
        $descriptions = array();
    
        // for each product ordered
        foreach ( $order->get_items() as $order_item  ) {
            $product = $order_item->get_product();
            // get the product description
            if ( $product->get_description() ) {
                $descriptions[] = $product->get_description();
            }
        }
    
        // if the $descriptions is not empty it shows it
        if ( ! empty( $descriptions ) ) {
            foreach ( $descriptions as $content ) {
                echo '<p>' . $content . '</p>';
            }
        }
    
    }
    

    代码已经过测试并且可以运行。将其添加到活动主题的 functions.php 中。

    【讨论】:

    • 谢谢!!这是完美的,完全符合我的期望。我会仔细研究你的代码,并确保我理解你所做的一切。非常感谢!
    猜你喜欢
    • 2021-01-29
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-31
    • 1970-01-01
    • 2020-12-15
    相关资源
    最近更新 更多