在WooCommerce“New Order”产品标题中添加精选图片网址
问题描述:
我已经制作了一个过滤器来更新woocommerce上的订单显示方式。 基本上我需要店主能够点击每个产品的名称(现在链接到特色图片),他也能够看到URL(因为图片文件名对他们来说是有用的,以便跟踪产品)在WooCommerce“New Order”产品标题中添加精选图片网址
我需要这个只影响发送给店主的NEW ORDER电子邮件。
我放在functions.php中的代码确实更新了所有电子邮件中的BUT,并在网站上订购了确认表。
有问题?我怎样才能影响新的订单电子邮件?我想我在这里错过了一些东西。
// item name link to product
add_filter('woocommerce_order_item_name', 'display_product_title_as_link', 10, 2);
function display_product_title_as_link($item_name, $item) {
$_product = get_product($item['variation_id'] ? $item['variation_id'] : $item['product_id']);
$image = wp_get_attachment_image_src(get_post_thumbnail_id($_product->post->ID), 'full');
return '<a href="'. $image[0] .'" rel="nofollow">'. $item_name .'</a>
<div style="color:blue;display:inline-block;clear:both;">'.$image[0].'</div>';
}
答
首先出现的是在你的代码像一些错误:
- 功能
get_product()
显然是不合时宜的,并已取代wc_get_product()
- 由于Woocommerce 3+
WC_Product
属性会直接访问,而是使用可用的方法。
这里是让你期待(在“新秩序”管理员只通知)什么是正确的方法:
// Your custom function revisited
function display_product_title_as_link($item_name, $item) {
$product = wc_get_product($item['variation_id'] ? $item['variation_id'] : $item['product_id']);
$image = wp_get_attachment_image_src($product->get_image_id(), 'full');
$product_name = $product->get_name();
return '<a href="'. $image[0] .'" rel="nofollow">'. $product_name .'</a>
<div style="color:blue;display:inline-block;clear:both;">'.$image[0].'</div>';
}
// The hooked function that will enable your custom product title links for "New Order" notification only
add_action('woocommerce_email_order_details', 'custom_email_order_details', 1, 4);
function custom_email_order_details($order, $sent_to_admin, $plain_text, $email){
// Only for "New Order" and admin email notification
if ('new_order' != $email->id && ! $sent_to_admin) return;
// Here we enable the hooked function
add_filter('woocommerce_order_item_name', 'display_product_title_as_link', 10, 3);
}
代码放在您的活动子主题的function.php文件(活动主题或任何插件文件)。
测试和WooCommerce工程3+
感谢您的及时答复并予补正的,我真的很感激。我已经测试过并能够正常工作,但仍显示客户端收到的电子邮件中的更改。这两个新的订单通知管理员和客户电子邮件有修改后的标题链接到图像。我只需要在发送给店主的电子邮件中提供此信息。 – GauchoCode
@GauchoCode ...对我来说,在我的测试服务器上它完美的工作(在WooCommerce版本3.1.2下)。我在测试之前从未发布过答案......重要提示:在添加此代码之前,您是否删除了旧代码? – LoicTheAztec
我已经仔细检查过,没有我旧代码的痕迹。就像你制作的一样,这两个电子邮件(客户端和管理员)看起来都一样,都有修改过的标题并显示图片网址。用Woo最新版本进行测试。我已经做了几个测试,并在两封电子邮件中都显示相同... http://i63.tinypic.com/6hoo4l.png – GauchoCode