[英]Add product short description to Woocommerce admin orders preview
我正在嘗試將我的產品簡短描述作為新標簽添加到我的訂單頁面中,這樣我們就可以更輕松地訂購產品而無需進入其中。
我看到當前 SKU 顯示在產品下,理想情況下它會有一個產品簡短描述。
到目前為止,這是我設法得到的結果,但是沒有輸出短說明
// Adds tab
add_action( 'woocommerce_admin_order_item_headers', 'pd_admin_order_items_headers' );
function pd_admin_order_items_headers($order){
?>
<th class="line_customtitle sortable" data-sort="your-sort-option">
Product MPN
</th>
<?php
}
// Shows Short Desc
add_action( 'woocommerce_admin_order_item_values', 'pd_admin_order_item_values' );
function pd_admin_order_item_values( $product ) {
?>
<td class="line_customtitle">
<?php the_excerpt(); ?>
</td>
<?php
}
目前的結果是
“沒有摘錄,因為這是一個受保護的帖子。”
我覺得它沒有遍歷產品並試圖獲取訂單摘錄,因此為什么它說它受到保護,但我對此沒有太多經驗。
任何幫助表示贊賞。
請嘗試以下操作(代碼已注釋) :
// Add a custom column to the order "line items" html table
add_action( 'woocommerce_admin_order_item_headers', 'custom_admin_order_items_headers', 20, 1 );
function custom_admin_order_items_headers( $order ){
echo '<th class="line_custom-title sortable" data-sort="your-sort-option">';
echo __('Short description', 'woocommerce') . '</th>';
}
// Custom column content in the order "line items" html table
add_action( 'woocommerce_admin_order_item_values', 'custom_admin_order_item_values', 20, 3 );
function custom_admin_order_item_values( $_product, $item, $item_id ) {
// Only for "line_item" items type
if( ! $item->is_type('line_item') ) return;
// For product variation, we get the parent variable product (in case of)
if( $_product->is_type('variation') ){
$parent_product = $_product->get_parent();
// The product variation description (as short description doesn't exist)
$excerpt = $_product->get_description();
// If product variation description doesn't exist we display the short description of the parent variable product
$excerpt = empty($excerpt) ? $parent_product->get_short_description() : $excerpt;
}
// For other product types
else {
$excerpt = $_product->get_short_description();
}
// Output
echo '<td class="line_custom-description">' . $excerpt . '</td>';
}
代碼位於活動子主題(或活動主題)的 function.php 文件中。 測試和工作
@LoicTheAztec 答案對我不起作用,我修改了他的答案並將custom_admin_order_item_values
函數替換為下面的函數,然后它就起作用了。
// Custom column content in the order "line items" html table
function custom_admin_order_item_values( $item_name, $item, $is_visible ){
$product_id = $item->get_product_id(); // Get the product Id
$excerpt = get_the_excerpt( $product_id ); // Get the short description
// Output
echo '<td class="line_custom-description">' . $excerpt . '</td>';
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.