将产品简短说明添加到Woocommerce管理订单编辑页面

时间:2018-04-10 13:57:06

标签: php wordpress woocommerce backend orders

我尝试将我的产品简短说明添加到我的订单页面作为新标签,这样我们就可以更轻松地订购产品,而无需进入其中。

我在产品下看到了当前的SKU显示,理想情况下它会有一个产品简短描述。

这是我迄今为止设法得到的,但没有输出简短的desc

// 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
}

结果目前说

  

&#34;没有摘录,因为这是受保护的帖子。&#34;

我觉得它没有在产品中循环并试图获取订单摘录因此为什么它说它受到保护但我对此并不太熟悉。

感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

请尝试以下操作(代码已注释)

// 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文件中。经过测试和工作

答案 1 :(得分:0)

@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>';
}
相关问题