在产品标题中添加特色图片网址,以便WooCommerce "New Order"通知



我做了一个过滤器来更新订单在woocommerce上的显示方式。基本上,我需要店主能够单击每个产品的名称(现在链接到特色图片),并且他也能够看到URL(因为图像文件名对于他们跟踪产品很有用)

我需要它只影响发送给店主的新订单电子邮件。

我放在函数中的代码.php确实在所有电子邮件中更新,并且在网站上也更新了订单确认表。

问题?我怎样才能只影响新订单电子邮件?我想我在这里错过了一些东西。

// 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>';
}

@LoicTheAztec - 它不起作用! href 作为图像?完整图像作为缩略图?(大电子邮件)缺少到产品的永久链接...修复功能display_product_title_as_link

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(), 'thumbnail' );
    $product_name = $product->get_name();
    $product_link = get_permalink( $product->get_id() );
    return '<a href="'. $product_link .'" target="_blank"><img width="70" height="70" src="'.$image[0].'" alt="'. $product_name .'">'. $product_name .'</a> ';
}

首先,代码中存在一些错误,例如:

  • 功能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 );
}

代码进入函数.php活动子主题的文件(活动主题或任何插件文件)。

在WooCommerce 3+中经过测试和工作

相关内容

最新更新