Php 在Woocommerce中销售产品时,向卖家发送自定义电子邮件
我有一个网站,允许用户(卖家)在前端输入产品。当有产品出售时,我想给卖家发电子邮件,让他们知道产品已经售出 这是我到目前为止得到的Php 在Woocommerce中销售产品时,向卖家发送自定义电子邮件,php,wordpress,woocommerce,orders,email-notifications,Php,Wordpress,Woocommerce,Orders,Email Notifications,我有一个网站,允许用户(卖家)在前端输入产品。当有产品出售时,我想给卖家发电子邮件,让他们知道产品已经售出 这是我到目前为止得到的 add_filter ('woocommerce_payment_complete', 'send_seller_email'); function send_seller_email($order_id) { $order = wc_get_order( $order_id ); $items = $order->get_items(); /
add_filter ('woocommerce_payment_complete', 'send_seller_email');
function send_seller_email($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
//foreach loop to get sellers email from order start
foreach ( $items as $item ) {
$product_name = $item->get_name();
$product_id = $item->get_product_id();
$sent_to = wp_get_object_terms( $product_id, 'soldby', false );
$seller_send_email = $sent_to[0]->name;
$seller_email = get_user_by('ID', $seller_send_email);
$email_to_sent_sold_conformation .= $seller_email->user_email;
}//foreach loop to get sellers email end
//Send seller email start
$to = $email_to_sent_sold_conformation;
$subject = 'Sold! ship now: ' . get_the_title($product_id);
$message = '';
$message .= '<p>' . get_the_excerpt($product_id) . '…</p>';
$message .= '<p><a href="' . get_permalink($product_id) . '"></a></p>';
wp_mail($to, $subject, $message );
//Send seller email end
}
add_filter('woocmerce_payment_complete'、'send_seller_email');
函数发送卖方电子邮件($order\u id){
$order=wc\u get\u order($order\u id);
$items=$order->get_items();
//foreach循环从订单开始获取卖家电子邮件
foreach($items作为$item){
$product_name=$item->get_name();
$product_id=$item->get_product_id();
$sent_to=wp_get_object_术语($product_id,'soldby',false);
$seller\u send\u email=$send\u to[0]->name;
$seller\u email=get\u user\u by('ID',$seller\u send\u email);
$email\u to\u sent\u selled\u确认=$seller\u email->user\u email;
}//foreach循环以获取卖家电子邮件结束
//发送卖家电子邮件开始
$to=$email\u to\u sent\u sell\u确认书;
$subject='sall!ship now:'。获取标题($product\u id);
$message='';
$message.=''。获取摘录($product\u id)。“…”;
$message.='';
wp_邮件($to,$subject,$message);
//发送卖家电子邮件结束
}
我完全重新审视了您的代码:
- 主代码位于由两个挂钩函数调用的实用函数中
- 挂接在
中的函数将触发付费订单woocmerce\u new\u order
- 挂接在
中的函数,该函数将在更新状态更改时触发已验证的订单woocmerce\u order\u status\u changed
// Utility function sending the email notification
function send_seller_email( $order ) {
$data = array();
// Loop through each order item
foreach ( $order->get_items() as $item_id => $item ) {
if( get_post_meta( $order->get_id(), '_sent_to_seller_'.$item_id, true ) )
continue; // Go to next loop iteration
$product_id = $item->get_product_id();
// Untested part
$soldby = wp_get_object_terms( $product_id, 'soldby', false );
if( empty($soldby) ) continue; // Go to next loop iteration
$seller_id = $soldby[0]->name;
$seller = get_user_by( 'ID', $seller_id );
$seller_email = $seller->user_email;
// Set the data in an array (avoiding seller email repetitions)
$data[$seller_email][] = array(
'excerpt' => get_the_excerpt($product_id),
'title' => get_the_title($product_id),
'link' => get_permalink($product_id),
);
// Update order to avoid notification repetitions
update_post_meta( $order->get_id(), '_sent_to_seller_'.$item_id, true );
}
if( count($data) == 0 ) return;
// Loop through custom data array to send mails to sellers
foreach ( $data as $email_key => $values ) {
$to = $email_key;
$subject_arr = array();
$message = '';
foreach ( $values as $value ) {
$subject_arr[] = $value['title'];
$message .= '<p><a href="'.$value['link'].'">'.$value['title'].'</a></p>';
$message .= '<p>'.$value['excerpt'].'…</p>';
}
$subject = 'Sold! ship now: '.implode( ', ', $subject_arr );
// Send email to seller
wp_mail( $to, $subject, $message );
}
exit();
}
add_action('woocommerce_new_order', 'new_order_seller_notification', 10, 1 );
function new_order_seller_notification( $order_id ) {
$order = wc_get_order( $order_id );
if( ! ( $order->has_status('processing') || $order->has_status('completed') ) )
return; // Exit
send_seller_email( $order );
}
add_action( 'woocommerce_order_status_changed', 'order_status_seller_notification', 20, 4 );
function order_status_seller_notification( $order_id, $status_from, $status_to, $order ) {
if( ! ( $status_to == 'processing' || $status_to == 'completed' ) )
return; // Exit
send_seller_email( $order );
}
//发送电子邮件通知的实用程序函数
功能发送\卖家\电子邮件($order){
$data=array();
//循环浏览每个订单项
foreach($order->get\u items()作为$item\u id=>$item){
if(get_post_meta($order->get_id(),“_发送给_卖家”.$item_id,true))
继续;//转到下一个循环迭代
$product_id=$item->get_product_id();
//未经试验的零件
$soldby=wp\u get\u object\u术语($product\u id,'soldby',false);
if(empty($soldby))continue;//转到下一个循环迭代
$seller_id=$soldby[0]->name;
$seller=get_user_by('ID',$seller_ID);
$seller\u email=$seller->user\u email;
//在数组中设置数据(避免卖方电子邮件重复)
$data[$seller\u email][]=数组(
“摘录”=>获取摘录($product\U id),
“title”=>获取标题($product\u id),
'link'=>get\u permalink($product\u id),
);
//更新订单以避免重复通知
更新发布元数据($order->get\u id(),“\u发送给\u卖家\u”。$item\u id,true);
}
如果(计数($data)==0)返回;
//循环通过自定义数据数组向卖家发送邮件
foreach($email\u key=>$value形式的数据){
$to=$email\u key;
$subject_arr=array();
$message='';
foreach($value作为$value){
$subject_arr[]=$value['title'];
$message.='';
$message.=''.$value['extract']..…';
}
$subject='salled!ship now:'。内爆(',',$subject_arr);
//发送电子邮件给卖家
wp_邮件($to,$subject,$message);
}
退出();
}
添加行动(“商业、新订单、新订单、卖方通知”,10,1);
功能新订单卖方通知($order\u id){
$order=wc\u get\u order($order\u id);
如果(!($order->has_status('processing')| |$order->has_status('completed'))
return;//退出
发送卖方电子邮件($order);
}
添加操作('WOOMerce\u order\u status\u changed'、'order\u status\u seller\u notification',20,4);
功能订单\状态\卖方\通知($order\ id,$status\ from,$status\ to,$order){
如果(!($status_to=='processing'| |$status_to=='completed'))
return;//退出
发送卖方电子邮件($order);
}
代码进入活动子主题(或主题)的function.php文件
代码没有经过真正的测试,但不会出错。它应该会起作用