Created
July 13, 2024 18:20
-
-
Save jmabbas/0e1b991daeeab570aeec898d1b23a52a to your computer and use it in GitHub Desktop.
Electro - Arcade specification tab
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' ); | |
function woo_custom_product_tabs( $tabs ) { | |
// phpinfo();die; | |
$product_id = get_the_id(); | |
$technical_specifications = get_field( "technical_specification",$product_id ); | |
$console_x_technical_specifications = get_field('console_x_technical_specifications',$product_id ); | |
$lego_technical_specifications = get_field('lego_technical_specifications',$product_id ); | |
/* Add New in Woocommerce - Specification Tab */ | |
if(!empty($technical_specifications)){ | |
if(!empty(array_filter(array_map('array_filter', $technical_specifications)))){ | |
$allcar = array("retro-handheld-gaming-consoles", "accessories-upgrades", "mini-pcs"); | |
$primery_cat = get_primary_category( $product_id ,$allcar); | |
if($primery_cat == true){ | |
$tabs['technical_specifications'] = array( | |
'title' => __( 'Specification', 'woocommerce' ), | |
'priority' => 120, | |
'callback' => 'woo_technical_specifications_products_tab_content' | |
); | |
} | |
} | |
}else if(!empty($lego_technical_specifications)){ | |
if(!empty(array_filter(array_map('array_filter', $lego_technical_specifications)))){ | |
$allcar = array('Block & Brick sets'); | |
$primery_cat = get_primary_category( $product_id ,$allcar); | |
if($primery_cat){ | |
$tabs['technical_specifications'] = array( | |
'title' => __( 'Specification', 'woocommerce' ), | |
'priority' => 120, | |
'callback' => 'woo_technical_specifications_products_tab_content' | |
); | |
} | |
} | |
}elseif(!empty($console_x_technical_specifications)){ | |
if(!empty(array_filter(array_map('array_filter', $console_x_technical_specifications)))){ | |
$allcar = array('Arcade Machines'); | |
$primery_cat = get_primary_category( $product_id ,$allcar); | |
if($primery_cat){ | |
$tabs['technical_specifications'] = array( | |
'title' => __( 'Specification', 'woocommerce' ), | |
'priority' => 120, | |
'callback' => 'woo_technical_specifications_products_tab_content' | |
); | |
} | |
} | |
} | |
return $tabs; | |
} | |
/* Callback Function of Specification Tab */ | |
function woo_technical_specifications_products_tab_content(){ | |
echo do_shortcode( '[custom_addition]'); | |
} | |
/* Callback Function of Video Tab */ | |
function woo_video_products_tab_content(){ | |
$Post_id = get_the_id(); | |
echo get_field('video',$Post_id); | |
} | |
/* Remove tab frpm single product page in woocommerce */ | |
add_filter( 'woocommerce_product_tabs', 'remove_product_tabs', 98 ); | |
function remove_product_tabs( $tabs ) { | |
unset( $tabs['specification'] ); | |
return $tabs; | |
} | |
function get_primary_category($product_id , $allcar ) { | |
global $wpdb; | |
// Get the table name with the correct prefix | |
$table_name = $wpdb->prefix . 'aioseo_posts'; | |
// Assuming $postId holds the ID of the post you want to retrieve data for | |
$postId = $product_id; | |
// Prepare the SQL query | |
$query = $wpdb->prepare("SELECT * FROM $table_name WHERE post_id = %d", $postId); | |
// Execute the query | |
$results = $wpdb->get_results($query); | |
$retur = false; | |
if($results[0]->primary_term == ''){ | |
$retur = true; | |
}else{ | |
$data = json_decode($results[0]->primary_term, true); | |
// Access the value associated with the key "product_cat" | |
$product_cat_value = isset($data['product_cat']) ? $data['product_cat'] : null; | |
$category = get_term_by('id',$product_cat_value,'product_cat'); | |
$primery_cat = $category->slug; | |
if(in_array($primery_cat,$allcar)){ | |
$retur = true; | |
} | |
} | |
return $retur; | |
} | |
add_shortcode('custom_addition','woo_custom_specification'); | |
function woo_custom_specification(){ | |
$Post_id = get_the_id(); | |
$categories = wp_get_post_terms( $Post_id, 'product_cat' ); | |
$allcat = array(); | |
foreach ( $categories as $category ) { | |
$allcat[] = $category->name; | |
} | |
$field = get_field_object('technical_specification',$Post_id ); | |
$lego_technical_specifications = get_field_object('lego_technical_specifications',$Post_id ); | |
$console_x_technical_specifications = get_field_object('console_x_technical_specifications',$Post_id ); | |
$lego_name = '_'.$lego_technical_specifications['_name']; | |
$field_name = '_'.$field['_name']; | |
$console_name = '_'.$console_x_technical_specifications['_name']; | |
$custom_fildes = get_post_meta($Post_id ); | |
/* lego_technical_specifications */ | |
if(in_array("Block & Brick sets", $allcat) ){ | |
if(!empty(array_filter(array_map('array_filter', $lego_technical_specifications['value'])))){ | |
if(array_key_exists($lego_name,$custom_fildes)){ | |
$legoTechnical_value = $lego_technical_specifications['value']; | |
$lego_technical_label = $lego_technical_specifications['sub_fields']; | |
for( $i=0; $i<= count($lego_technical_specifications['sub_fields']); $i++ ){ | |
$array_key = $lego_technical_label[$i]['name']; | |
$html .= '<div class = "technical_specification" id = "tab-'.$i.'"><h2><span style="color: #0c0d5f;">'.$lego_technical_label[$i]['label'].'</span></h2>'; | |
foreach( $lego_technical_label[$i]['sub_fields'] as $subfield_key => $subfields ){ | |
$key = $subfields['name']; | |
$html .= ' | |
<table width="100%"> | |
<tr>'; | |
if(!empty($legoTechnical_value[$array_key][$key])){ | |
$html .= '<td style="width: 43.2928%;"><b>'.$subfields['label'].':'.'</b></td>'; | |
} | |
$html .= '<td style="width: 53.5237%;">'.$legoTechnical_value[$array_key][$key].'</td> | |
</tr> | |
</table>'; | |
} | |
$html .= '</div>'; | |
} | |
echo $html ; | |
} | |
} | |
} | |
/* technical_specification */ | |
$allcar = array("Retro Handheld Gaming Consoles", "Accessories & Upgrades", "Mini PC"); | |
if(!empty($field['value'])){ | |
if(!empty(array_filter(array_map('array_filter', $field['value'])))){ | |
if(array_key_exists($field_name,$custom_fildes)){ | |
if($field['key'] == $custom_fildes[$field_name][0]){ | |
$fields_values = $field['value']; | |
$fields_labels = $field['sub_fields']; | |
for( $i=0; $i<= count($field['sub_fields']); $i++ ){ | |
$array_key = $fields_labels[$i]['name']; | |
$html .= '<div class = "technical_specification" id = "tab-'.$i.'"><h2><span style="color: #0c0d5f;">'.$fields_labels[$i]['label'].'</span></h2>'; | |
foreach( $fields_labels[$i]['sub_fields'] as $subfield_key => $subfields ){ | |
$key = $subfields['name']; | |
$html .= '<table width="100%" >'; | |
$html .= '<tr>'; | |
if(!empty($fields_values[$array_key][$key])){ | |
$html .= '<td style="width: 43.2928%;"><b>'.$subfields['label'].':'.'</b></td>'; | |
} | |
$html .='<td style="width: 43.2928%;">'.$fields_values[$array_key][$key].'</td> | |
</tr>'; | |
$html .= '</table>'; | |
} | |
$html .= '</div>'; | |
} | |
echo $html; | |
} | |
} | |
} | |
} | |
/* console_x_technical_specifications */ | |
if(in_array("Arcade Machines", $allcat) ){ | |
if(!empty(array_filter(array_map('array_filter', $console_x_technical_specifications['value'])))){ | |
if(array_key_exists($console_name,$custom_fildes)){ | |
if($console_x_technical_specifications['key'] == $custom_fildes[$console_name][0] ){ | |
$console_x_values = $console_x_technical_specifications['value']; | |
$console_x_labels = $console_x_technical_specifications['sub_fields']; | |
for( $i=0; $i<= count($console_x_technical_specifications['sub_fields']); $i++ ){ | |
$array_key = $console_x_labels[$i]['name']; | |
$html .= '<div class = "technical_specification" id = "tab-'.$i.'"><h2><span style="color: #0c0d5f;">'.$console_x_labels[$i]['label'].'</span></h2>'; | |
foreach( $console_x_labels[$i]['sub_fields'] as $subfield_key => $subfields ){ | |
$key = $subfields['name']; | |
$html .= ' | |
<table> | |
<tr>'; | |
if(!empty($fields_values[$array_key][$key])){ | |
$html .= '<td style="width: 43.2928%;"><b>'.$console_x_values['label'].':'.'</b></td>'; | |
} | |
$html .= '<td style="width: 43.2928%;">'.$console_x_values[$array_key][$key].'</td> | |
</tr> | |
</table>'; | |
} | |
} | |
echo $html ; | |
} | |
$html .= '</div>'; | |
} | |
} | |
} | |
} | |
function footer_data(){ | |
?> | |
<script> | |
let tr = jQuery('.technical_specification').children('table').children('tbody').children() | |
jQuery(tr).each(function() { | |
jQuery.each(this.cells, function(){ | |
if(jQuery(this).text() == ""){ | |
jQuery(this).parent().css("display","none") | |
console.log(jQuery(this).parent().parent().children().length) | |
} | |
}); | |
}); | |
jQuery('.technical_specification').each(function(){ | |
tablecount = jQuery(this).children('table').length; | |
count=0; | |
jQuery(this).children('table').each(function(){ | |
if(jQuery(this).children().find('td').text() == ''){ | |
count++; | |
} | |
}) | |
if(tablecount == count){ | |
// jQuery(this).children('h2').css('display','none') | |
console.log(jQuery(this).remove()); | |
} | |
}) | |
</script> | |
<?php | |
} | |
add_action('wp_footer','footer_data'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment