Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在订单视图列中显示自定义字段值_Php_Wordpress_Woocommerce_Metadata_Orders - Fatal编程技术网

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

Php 在订单视图列中显示自定义字段值,php,wordpress,woocommerce,metadata,orders,Php,Wordpress,Woocommerce,Metadata,Orders,我有一个在产品页面上创建下拉列表的代码。它保存所选数据并显示在订单上。我需要的帮助是在自定义列中显示值 代码如下: add_action( 'woocommerce_product_options_general_product_data', 'costcenter_management_group' ); function costcenter_management_group() { global $post; echo '<div class="options_group">'

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

代码如下:

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;
}
add_action('woocommerce_product_options_general_product_data'、'costcenter_management_group');
职能成本中心\管理\集团(){
全球$员额;
回声';
woocommerce_wp_select(阵列)(
'id'=>''成本中心',
'label'=>\('Cost Center','woocommerce'),
“选项”=>数组(
“一号”=>uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
“二号”=>uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu,
)));
回声';
}
//保存成本中心设置
添加行动('woocommerce\u process\u product\u meta'、'costcenter\u management\u group\u save');
功能成本中心\u管理\u组\u保存($post\u id){
如果(isset($邮政[''成本中心])
更新发布元($发布id,''成本中心',esc属性($发布[''成本中心]);
}
//点播
添加操作('woocommerce\u checkout\u create\u order\u line\u item','costcenter\u management\u group\u display',20,4);
功能成本中心\管理\组\显示($item、$cart\ item\u key、$values、$order){
如果($cost\u center=$values['data']->get\u meta('u cost\u center')){
$item->update_meta_数据('Cost Center',$Cost_Center);
}}
//在订单视图中添加成本中心列(WC管理员)
添加过滤器(“管理编辑车间订单列”、“成本中心管理组列”,10,1);
功能成本中心\管理\组\列($columns){
$actions_key=isset($columns['wc_actions'])?'wc_actions':'order_actions';
$order_actions=$columns[$actions_key];
未设置($columns[$actions_key]);
$columns['cost_center']=uuu(“成本中心”、“商业”);
$columns[$actions\u key]=$order\u actions;
返回$columns;
}

我知道我可以使用
echo
,但我不知道应该使用哪个值?

要在自定义列中显示此自定义订单项目元数据,请使用以下命令:

// Display data to custom column in admin orders list
add_action( 'manage_shop_order_posts_custom_column' , 'display_enclosed_invoice_order_column_data' );
function display_enclosed_invoice_order_column_data( $column ) {
    global $the_order, $post;

    if( $column  == 'cost_center' ) {
        $values = []; // Initializing

        // Loop through order items
        foreach ( $the_order->get_items() as $item ) {
            if( $cost_centre = $item->get_meta( 'Cost Center' ) ) {
                $values[] = $cost_centre;
            }
        }
        // Display the value(s)
        if( sizeof( $values ) > 0 ) {
            echo implode( ', ', $values); // Convert the array to a coma separated string
        }
    }
}

代码位于活动子主题(或活动主题)的function.php文件中。它应该可以工作。

要在自定义列中显示此自定义订单项目元数据,请使用以下命令:

// Display data to custom column in admin orders list
add_action( 'manage_shop_order_posts_custom_column' , 'display_enclosed_invoice_order_column_data' );
function display_enclosed_invoice_order_column_data( $column ) {
    global $the_order, $post;

    if( $column  == 'cost_center' ) {
        $values = []; // Initializing

        // Loop through order items
        foreach ( $the_order->get_items() as $item ) {
            if( $cost_centre = $item->get_meta( 'Cost Center' ) ) {
                $values[] = $cost_centre;
            }
        }
        // Display the value(s)
        if( sizeof( $values ) > 0 ) {
            echo implode( ', ', $values); // Convert the array to a coma separated string
        }
    }
}

代码位于活动子主题(或活动主题)的function.php文件中。它应该可以工作。

我在另一篇文章中找到了您的代码,当时正在研究其他内容。我在这里问了一个问题。你介意看一下吗?可以那太糟糕了。有太多的答案对“如果添加了5个项目”和类似的事情都有很好的提示,所以我尝试自己修改代码。我希望在删除了正常的“添加到购物车”消息后,改用“甜蜜提醒”会更容易。我已经更新了问题并添加了链接。请看一看,谢谢。我在另一篇文章中找到了你的代码,同时研究了其他内容。我在这里问了一个问题。你介意看一下吗?可以那太糟糕了。有太多的答案对“如果添加了5个项目”和类似的事情都有很好的提示,所以我尝试自己修改代码。我希望在删除了正常的“添加到购物车”消息后,改用“甜蜜提醒”会更容易。我已经更新了问题并添加了链接。请看一看,谢谢。