php - 使用供应商商店名称 (Dokan) 在 WooCommerce 中自定义电子邮件主题

标签 php wordpress woocommerce email-notifications dokan

我们发现了这个问题https://wordpress.stackexchange.com/questions/320924/woocommerce-order-processing-email-subject-not-changing并且运行良好。现在,我们希望通过在订单完成邮件中显示订单上的供应商来扩展此功能。

但是我们无法输出供应商商店名称。

我们的代码中是否存在明显的错误?

add_filter('woocommerce_email_subject_customer_completed_order',

'change_completed_email_subject', 1, 2 );
function change_completed_email_subject( $subject, $order ) {
global $woocommerce;

// Order ID 
$order->get_items();
     
// Author id
$author_id = $product->post->post_author;
        
// Shopname
$vendor = dokan()->vendor->get( $author_id );
$shop_name = $vendor->get_shop_name();

// Blogname
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

// Output subject
$subject = sprintf( '%s, Deine %s Bestellung (#%s) wurde versendet! Vendor: %s', $order->billing_first_name, $blogname, $order->get_order_number(), $shop_name );
return $subject;
}

更新:

我已经尝试通过 $shop_name = dokan()->vendor->get( $author_id )->get_shop_name(); 获取名称,但没有成功。

最佳答案

  • 不必使用全局 $woocommerce
  • 您使用 $order->get_items();,但不对其执行任何操作
  • $product 未定义
  • 使用$order->get_billing_first_name() VS $order->billing_first_name

所以你得到:

function filter_woocommerce_email_subject_customer_completed_order( $subject, $order ) {
    // Empty array
    $shop_names = array();
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Get product object
        $product = $item->get_product();

        // Author id
        $author_id = $product->post->post_author;
        
        // Shopname
        $vendor = dokan()->vendor->get( $author_id );
        $shop_name = $vendor->get_shop_name();
        
        // OR JUST USE THIS FOR SHOPNAME
        // Shop name
        // $shop_name = dokan()->vendor->get( $author_id )->get_shop_name();
        
        // NOT in array
        if ( ! in_array( $shop_name, $shop_names ) ) {
            // Push to array
            $shop_names[] = $shop_name;
        }
    }

    // Blogname
    $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);

    // Set subject
    $subject = sprintf( __( '%s, Deine %s Bestellung (#%s) wurde versendet! Vendor: %s', 'woocommerce' ), $order->get_billing_first_name(), $blogname, $order->get_order_number(), implode( ', ', $shop_names ) );

    // Return
    return $subject;
}
add_filter( 'woocommerce_email_subject_customer_completed_order', 'filter_woocommerce_email_subject_customer_completed_order', 10, 2 );

关于php - 使用供应商商店名称 (Dokan) 在 WooCommerce 中自定义电子邮件主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65993944/

相关文章:

woocommerce - 根据产品差异提高价格(woocommerce)

phpExcel:getCalculatedValue() 返回#VALUE

php - Windows 10 上的 Memcache PHP 扩展间歇性工作

php - Mysql根据点赞数从多个表中选择

wordpress - WooCommerce REST API - 获取订单的自定义字段

wordpress - 如何从 Wordpress 的 WooCommerce 插件中删除购物车?

PHP/MySQL 插入空值

javascript - WordPress 中的 iFrame URL 参数?

javascript - 如何在 WordPress 中异步入队 jQuery?

php - ajax 帖子未返回成功