沃梦达 / 编程问答 / php问题 / 正文

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

Adding product hyperlink to low stock notification email in WooCommerce(将产品超链接添加到WooCommerce中的低库存通知电子邮件)

本文介绍了将产品超链接添加到WooCommerce中的低库存通知电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  • &Q;产品-标题&Q;库存不足。剩下&Q;XX&Q;。

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


我发现我可以为此使用以下筛选器挂钩

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

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

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

相关:Add product link to out of stock email notification in WooCommerce

这篇关于将产品超链接添加到WooCommerce中的低库存通知电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:将产品超链接添加到WooCommerce中的低库存通知电子邮件

基础教程推荐