问题描述
我们希望从订单管理订单详细信息页面显示商店名称、供应商名称和供应商联系方式。
我正在使用 WooCommerce show vendor store-name (Dokan) in admin order details overview 应答代码显示发票中每个产品的商店名称。
我的尝试:
// Adding vendor Details admin shop_order pages
add_action( 'woocommerce_admin_order_data_after_billing_address','action_woocommerce_admin_order_data_after_billing_address',10,1 );
function action_woocommerce_admin_order_data_after_billing_address( $order ) {
// Empty array
$shop_names = array();
// Output
echo '<h4>' . __( 'Store Name: ','woocommerce' ) . '</h4>';
// 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;
// Output
echo '<p>' . $shop_name . '</p>';
}
}
// Empty array
$store_phones = array();
// Output
echo '<h4>' . __( 'Seller Contact: ','woocommerce' ) . '</h4>';
// 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 );
$store_phone = $vendor->get_phone();
// OR JUST USE THIS FOR SHOPNAME
// Shop name
// $store_phone = dokan()->vendor->get( $author_id )->get_store_phone();
// NOT in array
if ( ! in_array( $store_phone,$store_phones ) ) {
// Push to array
$store_phone[] = $store_phone;
// Output
echo '<p>' . $store_phone . '</p>';
}
}
}
通过使用此代码,我们获得了商店名称,但我们无法获得供应商名称和供应商联系方式。
解决方法
您在此代码中的问题 $store_phone[] = $store_phone;
替换为此 $store_phones[] = $store_phone;
// Adding Vendor Details admin shop_order pages
add_action( 'woocommerce_admin_order_data_after_billing_address','action_woocommerce_admin_order_data_after_billing_address',10,1 );
function action_woocommerce_admin_order_data_after_billing_address( $order ) {
// Empty array
$shop_names = array();
// Output
echo '<h4>' . __( 'Store Name: ','woocommerce' ) . '</h4>';
// 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;
// Output
echo '<p>' . $shop_name . '</p>';
}
}
// Empty array
$store_phones = array();
// Output
echo '<h4>' . __( 'Seller Contact: ','woocommerce' ) . '</h4>';
// 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 );
$store_phone = $vendor->get_phone();
// OR JUST USE THIS FOR SHOPNAME
// Shop name
// $store_phone = dokan()->vendor->get( $author_id )->get_store_phone();
// NOT in array
if ( ! in_array( $store_phone,$store_phones ) ) {
// Push to array
$store_phones[] = $store_phone;
// Output
echo '<p>' . $store_phone . '</p>';
}
}
}
经过测试并有效