Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
Hristiyan Hristov

Add a custom column via php?

Hey guys,

I’m using a plugin called Extra product options, in order to give the customers an option to choose between different colors, fonts and etc when making an order.

I want to see the options they choose in my Admin Orders page. Unfortunately I wasn’t able to do this via AdminColumns, so I contacted the developer of Extra product options and they gave me a PHP code which is working fine as you can see here https://prnt.sc/136fmuc.

The problem is that, If I turn AdminColumns plugin on, it’s deleting my custom fields I made using PHP.

I will paste the PHP code here, any chance you can help me use Admincolumns + my custom PHP code as a column?

<?php

// add listable checkout add-on column titles to the orders list table
add_filter( 'manage_edit-shop_order_columns', 'my_render_column_titles', 15 );
 
// add listable checkout add-on column content to the orders list table
add_action( 'manage_shop_order_posts_custom_column', 'my_render_column_content', 5 );
 
function my_render_column_titles( $columns ) {
    $new_columns = array();
    foreach ( $columns as $name => $value ) {
        if ($name=='order_status'){
            $new_columns['order_meta']  = __( 'Meta', 'woocommerce' );
        }
        $new_columns[$name] = $value;
    }
    return $new_columns;
}
function my_render_column_content( $column ) {
    if ($column!="order_meta"){
        return;
    }
    global $post, $woocommerce, $the_order;
 
    if ( empty( $the_order ) || $the_order->get_id() != $post->ID ) {
        $the_order = wc_get_order( $post->ID );
    }
    $order=$the_order;
    $order_currency = $order->get_currency();
    $mt_prefix = $order_currency;
    foreach ( $the_order->get_items() as $item_id => $item ) {
        $product        = apply_filters( 'woocommerce_order_item_product', $the_order->get_product_from_item( $item ), $item );
 
        $item_meta = function_exists( 'wc_get_order_item_meta' ) ? wc_get_order_item_meta( $item_id, '', FALSE ) : $order->get_item_meta( $item_id );
 
        $html = '';
 
        $has_epo = is_array($item_meta)
                && isset($item_meta) 
                && isset($item_meta['_tmcartepo_data']) 
                && isset($item_meta['_tmcartepo_data'][0])
                && isset($item_meta['_tm_epo']);
        
        $has_fee = is_array($item_meta)
                && isset($item_meta) 
                && isset($item_meta['_tmcartfee_data']) 
                && isset($item_meta['_tmcartfee_data'][0]);
 
        if ($has_epo || $has_fee){
            $current_product_id=$item['product_id'];
            $original_product_id = floatval(THEMECOMPLETE_EPO_WPML()->get_original_id( $current_product_id,'product' ));
            if (THEMECOMPLETE_EPO_WPML()->get_lang()==THEMECOMPLETE_EPO_WPML()->get_default_lang() && $original_product_id!=$current_product_id){
                $current_product_id = $original_product_id;
            }
            $wpml_translation_by_id=THEMECOMPLETE_EPO_WPML()->get_wpml_translation_by_id( $current_product_id );
        }
 
        if ($has_epo){
            $epos = maybe_unserialize($item_meta['_tmcartepo_data'][0]);
 
            if ($epos && is_array($epos) ){
 
                $html_toadd='';
                $hasit=false;
                $html_epo='';
                foreach ($epos as $key => $epo) {
                    if ($epo && is_array($epo)){
 
                        $new_currency = false;
                        if (isset($epo['price_per_currency'])){
                            $_current_currency_prices = $epo['price_per_currency'];
                            if ($mt_prefix!=='' 
                                    && $_current_currency_prices!=='' 
                                    && is_array($_current_currency_prices) 
                                    && isset($_current_currency_prices[$mt_prefix])
                                    && $_current_currency_prices[$mt_prefix]!=''){
 
                                $new_currency = true;
                                $epo['price'] = $_current_currency_prices[$mt_prefix];
                                
                            }
                        }
                        if(!$new_currency){
                            $type="";
                            if (isset($epo['element']) && isset($epo['element']["_"]) &&isset($epo['element']["_"]['price_type'])){
                                $type=$epo['element']["_"]['price_type'];
                            }
                            $epo['price'] = apply_filters('wc_epo_get_current_currency_price',$epo['price'],$type);
                        }
 
                        $hasit=true;
                        if(!isset($epo['quantity'])){
                            $epo['quantity'] = 1;
                        }
                        if(isset($wpml_translation_by_id[$epo['section']])){
                            $epo['name'] = $wpml_translation_by_id[$epo['section']];
                        }
                        // local mode
                        if ( !isset($epo['price_per_currency']) && taxonomy_exists( $epo['name'] ) ) {
                            $epo['name'] = wc_attribute_label( $epo['name'] );
                        }
                        if(isset($wpml_translation_by_id["options_".$epo['section']]) 
                            && is_array($wpml_translation_by_id["options_".$epo['section']]) 
                            && !empty($epo['multiple']) 
                            && !empty($epo['key'])){
                            
                            $pos = strrpos($epo['key'], '_');
                            
                            if($pos!==false) {
                                
                                $av=array_values( $wpml_translation_by_id["options_".$epo['section']] );
                                
                                if (isset($av[substr($epo['key'], $pos+1)])){
                                    
                                    $epo['value'] = $av[substr($epo['key'], $pos+1)];
                                    
                                }
 
                            }
 
                        }
                        $display_value = $epo['value'];
                        if (is_array($epo['value'])){
                            $display_value = array_map('html_entity_decode', $display_value, version_compare(phpversion(), '5.4', '<') ? ENT_COMPAT : (ENT_COMPAT | ENT_HTML401) ,'UTF-8');
                        }else{
                            $display_value = html_entity_decode($display_value, version_compare(phpversion(), '5.4', '<') ? ENT_COMPAT : (ENT_COMPAT | ENT_HTML401) ,'UTF-8');
                        }
                        
                        if (!empty($epo['use_images']) && !empty($epo['images']) && $epo['use_images']=="images"){
                            $display_value ='<div class="cpf-img-on-cart"><img alt="" class="attachment-shop_thumbnail wp-post-image epo-option-image" src="'.apply_filters("tm_image_url",$epo['images']).'" /></div>'.esc_attr($display_value);
                        }
 
                        $html_epo .= '<div class="tm-50">'.apply_filters('tm_translate',$epo['name']).'</div>';
                        
                        // value
                        $display_value = apply_filters('tm_translate',$display_value);
 
                        if (isset($epo["element"]) && isset($epo["element"]['type']) && $epo["element"]['type'] == "upload"){
                            $check = wp_check_filetype( $epo['value'] );
                            if ( !empty( $check['ext'] ) ) {
                                $image_exts = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png' );
                                if ( in_array( $check['ext'], $image_exts ) ){
                                    $display_value ='<a target="_blank" href="'.$display_value.'"><span class="cpf-img-on-cart"><img alt="" class="attachment-shop_thumbnail wp-post-image epo-option-image epo-upload-image" src="'.
                                        apply_filters("tm_image_url",$epo['value']).'" /></span></a>';
                                }
                            }
                        }
 
                        if (!empty($epo['multiple_values'])){
                            $display_value_array=explode($epo['multiple_values'],$display_value);
                            $display_value="";
                            foreach ($display_value_array as $d => $dv) {
                                $display_value .='<span class="cpf-data-on-cart">'.$dv.'</span>';
                            }
                        }
 
                        $html_epo .= '<div class="view"><div class="tm-51">'.make_clickable( $display_value ).'</div></div>';
                         
                    }
                }
 
                if ($hasit){
                    $html .= $html_toadd.$html_epo;
                }
            }
        }
 
        if ($has_fee){
            $epos = maybe_unserialize($item_meta['_tmcartfee_data'][0]);
            if (isset($epos[0])){
                $epos = $epos[0];               
            }else{
                $epos = false;
            }
 
            if ($epos && is_array($epos) && !empty($epos[0])){
                
                foreach ($epos as $key => $epo) {
                    if ($epo && is_array($epo)){
                        if(!isset($epo['quantity'])){
                            $epo['quantity'] = 1;
                        }
                        if(isset($wpml_translation_by_id[$epo['section']])){
                            $epo['name'] = $wpml_translation_by_id[$epo['section']];
                        }
                        if(isset($wpml_translation_by_id["options_".$epo['section']]) && is_array($wpml_translation_by_id["options_".$epo['section']]) && !empty($epo['multiple']) && !empty($epo['key'])){
                            $pos = strrpos($epo['key'], '_');
                            if($pos!==false) {
                                $av=array_values( $wpml_translation_by_id["options_".$epo['section']] );
                                if (isset($av[substr($epo['key'], $pos+1)])){
                                    $epo['value'] = $av[substr($epo['key'], $pos+1)];
                                    if (!empty($epo['use_images']) && !empty($epo['images']) && $epo['use_images']=="images"){
                                        $epo['value'] ='<div class="cpf-img-on-cart"><img alt="" class="attachment-shop_thumbnail wp-post-image epo-option-image" src="'.apply_filters("tm_image_url",$epo['images']).'" /></div>'.$epo['value'];
                                    }
                                }
                            }
                        }
 
                        $html .= '<div class="view"><div class="tm-51">'.apply_filters('tm_translate',$epo['name']).'</div><div class="tm-50">'.apply_filters('tm_translate',$epo['value']).'</div></div>';
 
                    }
                }
            }
        }
 
        
        
        
 
        echo $html;
    
    }
}   
2 years, 11 months ago
Stefan van den Dungen Gronovius
Developer

You should be able to manage the columns and add the ‘Meta’ column again.
This custom column should be listed in the ‘Default’ section when adding a new column.
If you have once stored the columns with our plugin, any columns added with custom code (or other plugins) should not automatically be added to the page and needs to be added to the stored column set first.

2 years, 11 months ago

You must be logged in to reply to this topic.