在 WooCommerce 中将产品超链接添加到低库存通知电子邮件

问题描述

认情况下,低库存通知电子邮件包含此文本。

  • “Product-title”库存不足。还剩“XX”。

我想编辑此消息,以便将产品超链接添加到产品标题中。


我发现我可以为此使用以下过滤器钩子

add_filter( 'woocommerce_email_content_low_stock','low_stock_dspixel',10,2 );

function low_stock_dspixel( $message,$product ) {

    $message = sprintf(/* translators: 1: product name 2: items in stock */
            __( '%1$s is low in stock. There are %2$d left.','woocommerce' ),html_entity_decode( wp_strip_all_tags( $product->get_formatted_name() ),ENT_QUOTES,get_bloginfo( 'charset' ) ),html_entity_decode( wp_strip_all_tags( $product->get_stock_quantity() ) )
        );
 
    return $message;
}

如何进一步调整以添加产品超链接

解决方法

您可以添加/使用 WC_Product::get_permalink() – 产品固定链接来自定义 $message 以满足您的需求。

所以你得到:

function filter_woocommerce_email_content_low_stock ( $message,$product ) {
    // Edit message
    $message = sprintf(
        /* translators: 1: product name 2: items in stock */
        __( '%1$s is low in stock. There are %2$d left.','woocommerce' ),'<a href="' . $product->get_permalink() . '">' . html_entity_decode( wp_strip_all_tags( $product->get_formatted_name() ),ENT_QUOTES,get_bloginfo( 'charset' ) ) . '</a>',html_entity_decode( wp_strip_all_tags( $product->get_stock_quantity() ) )
    );
    
    return $message;
}
add_filter( 'woocommerce_email_content_low_stock','filter_woocommerce_email_content_low_stock',10,2 );

重要提示:这个答案默认不起作用,因为 wp_mail() 用作邮件功能,其中的内容类型是 text/plain不允许使用 HTML

因此,要使用 WordPress wp_mail() 发送 HTML 格式的电子邮件,请添加这段额外的代码

function filter_wp_mail_content_type() {
    return "text/html";
}
add_filter( 'wp_mail_content_type','filter_wp_mail_content_type',0 );