Woocommerce 中产品名称的短代码

问题描述 投票:0回答:3

我想知道 woocommerce 中产品名称的简码。我想在一些电子邮件的主题中添加产品名称。但尽管进行了很多搜索,我找不到任何显示产品名称的短代码。有人可以帮忙吗?

php wordpress woocommerce
3个回答
6
投票

WooCommerce 没有用于显示产品名称的短代码,但是,您可以轻松创建自己的短代码。

示例:

function displayProductName($item) {
    $productName = get_the_title($item['id']);
    return $productName;
}

add_shortcode('product_name', 'displayProductName');

用途:

将上述代码添加到您的

functions.php
文件中,然后使用
[product_name id="10"]
输出产品标题,其中
id
是相关产品的 id。上面的示例使用
get_the_title()
内置 WordPress 函数:https://developer.wordpress.org/reference/functions/get_the_title/

有关如何创建短代码的更多信息,请访问:https://developer.wordpress.org/reference/functions/add_shortcode/


1
投票

没有简单的方法可以做到这一点,因为没有在电子邮件主题中包含产品名称的简码。您需要通过 FTP 在子主题的 function.php 文件中添加一些代码。

我编写了这段代码,它将挂钩新订单主题,并将默认值(来自 WooCommerce 设置页面的那个)更改为下面的值

New Customer Order (#Some order number) from Some On - date; Products: Product name1; Product name2

add_filter('woocommerce_email_subject_new_order', 'change_the_new_order_email_subject', 1, 2);
function change_the_new_order_email_subject( $subject, $order ) {

    //get the products' name from the order
    $product_name = '';
    foreach($order->get_items() as $item) {
        $product_name .= $item['name'] . "; ";

    }

    $subject = sprintf( 'New Customer Order (# %s) from %s %s - %s; Products: %s', $order->id, $order->billing_first_name, $order->billing_last_name, $order->order_date, $product_name );

    return $subject;
}

请注意,我尚未测试代码。


0
投票

我尝试使用 [product_name] 显示当前产品页面的产品标题,但它不起作用,有帮助吗?

© www.soinside.com 2019 - 2024. All rights reserved.