dtqu72830
2019-02-27 13:59
浏览 228
已采纳

在订单视图列WooCommerce中显示自定义字段值

I have this code which creates a drop-down on the product page. It saves the data chosen and it displays on the order. What I am need help with is displaying the value in the custom column.

Here's the code:

add_action( 'woocommerce_product_options_general_product_data', 'costcenter_management_group' );
function costcenter_management_group() {
global $post;
echo '<div class="options_group">';
woocommerce_wp_select( array(
'id'      => '_cost_center',
'label'   => __( 'Cost Center', 'woocommerce' ),
'options' => array(
'One' => __( 'Office One', 'woocommerce' ),
'Two' => __( 'Office Two', 'woocommerce' ),
)));
echo '</div>';
}

// save cost center settings
add_action( 'woocommerce_process_product_meta', 'costcenter_management_group_save' );
function costcenter_management_group_save( $post_id ){
if( isset( $_POST['_cost_center'] ) )
update_post_meta( $post_id, '_cost_center', esc_attr( $_POST['_cost_center'] ) );
}

// display on order
add_action('woocommerce_checkout_create_order_line_item', 'costcenter_management_group_display', 20, 4);
function costcenter_management_group_display($item, $cart_item_key, $values, $order) {
if ( $cost_center = $values['data']->get_meta('_cost_center') ) {
$item->update_meta_data( 'Cost Center', $cost_centre );
}}

// add cost center column on order view (WC admin)
add_filter('manage_edit-shop_order_columns', 'costcenter_management_group_column', 10, 1 );
function costcenter_management_group_column( $columns ) {
$actions_key = isset($columns['wc_actions']) ? 'wc_actions' : 'order_actions';
$order_actions = $columns[$actions_key];
unset($columns[$actions_key]);
$columns['cost_center'] = __("Cost Center", "woocommerce");
$columns[$actions_key] = $order_actions;
return $columns;
}

I know I can use echo but I don't know which value I'm supposed to use?

图片转代码服务由CSDN问答提供 功能建议

我有这个代码在产品页面上创建一个下拉列表。 它保存所选数据并显示在订单上。 我需要帮助的是在自定义列中显示值。

以下是代码:

  add_action('woocommerce_product_options_general_product_data',  'costcenter_management_group'); 
function costcenter_management_group(){
global $ post; 
echo'&lt; div class =“options_group”&gt;'; 
woocommerce_wp_select(array(
'id'=&gt;'_cost_center',
  'label'=&gt; __('成本中心','woocommerce'),
'options'=&gt;数组(
'One'=&gt; __('Office One','woocommerce'),
  'Two'=&gt; __('Office Two','woocommerce'),
))); 
echo'&lt; / div&gt;'; 
} 
 
 //保存成本中心设置
add_action(  'woocommerce_process_product_meta','costcenter_management_group_save'); 
function costcenter_management_group_save($ post_id){
if(isset($ _POST ['_ cost_center']))
update_post_meta($ post_id,'_ cost_center',esc_attr($ _POST ['_ cost_center']  )); 
} 
 
 //在订单上显示
add_action('woocommerce_checkout_create_order_line_item','costcenter_management_group_display'  ,20,4); 
function costcenter_management_group_display($ item,$ cart_item_key,$ values,$ order){
if($ cost_center = $ values ['data']  - &gt; get_meta('_ cost_center')){
 $  item-&gt; update_meta_data('Cost Center',$ cost_centre); 
}} 
 
 //在订单视图上添加费用中心列(WC管理员)
add_filter('manage_edit-shop_order_columns','costcenter_management_group_column',10  ,1); 
function costcenter_management_group_column($ columns){
 $ actions_key = isset($ columns ['wc_actions'])?  'wc_actions':'order_actions'; 
 $ order_actions = $ columns [$ actions_key]; 
unset($ columns [$ actions_key]); 
 $ columns ['cost_center'] = __(“成本中心”,“woocommerce  “); 
 $ columns [$ actions_key] = $ order_actions; 
return $ columns; 
} 
   
 
 

我知道我可以使用 echo < / code>但我不知道我应该使用哪个值?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题