在我的WooCommerce网店,我销售天然产品,如洗发水和肥皂。目前我只有2个产品。
我想添加一个特定的文本字符串,其中包含有关正在购买的产品的背景信息的超链接,以阻止人们混淆:
所以产品A等于超链接A,产品B等于超链接B.
这是我到目前为止所得到的:
file= open("C:\Users\Dell\Desktop\ll\file3.txt", "r")
但它不起作用。
如何根据购买的产品在客户电子邮件通知中添加一些带有特定链接的自定义文本?
答案 0 :(得分:0)
你的代码对于Woocommerce 3+来说有点过时,应该是这样的:
foreach ( $order->get_items() as $item_id => $item ) {
$product_name = $item->get_name(); // The product name
$product_id = $item->get_product_id(); // The product ID
$product = $item->get_product(); // The WC_Product Object
$product_description = $product->get_description(); // The Product description
}
现在,您可以根据产品ID在一些客户电子邮件通知中显示您的链接文本,在此自定义附加功能中定义您的产品ID,链接,文本,标题......还要记住,您可以在订单中包含许多不同的产品。 / p>
以下是代码:
// Add custom text hyperlinks to Order items
add_action( 'woocommerce_email_after_order_table', 'add_product_custom_link', 9, 4 );
function add_product_custom_link( $order, $sent_to_admin, $plain_text, $email )
{
// Only for customer "Processing" and "Completed" Order email notifications
if( ! ($email->id == 'customer_processing_order' || $email->id == 'customer_completed_order') ) return;
// HERE (Below) define your product IDs, links, texts, title …
$product_id1 = 37;
$product_id2 = 53;
$link1 = home_url( '/some-path/product-info1/' );
$link2 = home_url( '/some-path/product-info1/' );
$text1 = __( 'Your linked text1' );
$text2 = __( 'Your linked text1' );
$title = __("Product Documentation Link");
$instroduction = __("Some introduction text paragraph here, blabla bla blabla blablabla bla blabla. …");
// Iterating through order "line items"
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id(); // The product ID
// Set all the product IDs in this order in an array
$product_ids[$product_id] = $product_id;
}
$has_product = false;
// CSS style
$styles = '<style>
table.product-info{width: 100%; font-family: \'Helvetica Neue\', Helvetica, Roboto, Arial, sans-serif;
color: #737373; border: 1px solid #e4e4e4; margin-bottom:8px;}
table.product-info th, table.product-info td{text-align: left; border-top-width: 4px;
color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
table.product-info td{text-align: left; border-top-width: 4px; color: #737373; border: 1px solid #e4e4e4; padding: 12px;}
</style>
';
// HTML (start)
$html_output = '<h2>'.$title.'</h2>
<p>'.$instroduction.'</p>
<table class="product-info" cellspacing="0" cellpadding="6">';
// Iterating through the product ids (when multiple products are in the order)
foreach ( $product_ids as $value_id ) {
// ==> HERE Replace links path and linked text by yours (for each product)
if ( $product_id == $product_id1 ) {
// PRODUCT 1
$html_output .= '<tr><td><a href="' . $link1 . '">' . $text1 . '</a></td></tr>'; // HTML
$has_product = true;
} elseif ( $product_id == $product_id2 ) {
// PRODUCT 2
$html_output .= '<tr><td><a href="' . $link2 . '">' . $text2 . '</a></td></tr>'; // HTML
$has_product = true;
}
}
if( $has_product ){
$html_output .= '</table><br>'; // HTML (end)
// Output CSS and HTML
echo $styles . $html_output;
}
}
在WooCommerce 3+上测试并且有效。你会得到类似的东西: