· 4 years ago · Apr 29, 2021, 05:30 PM
1/**
2* Adding custom WooCommerce Tabs
3*/
4add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
5function woo_new_product_tab( $tabs ) {
6 // Array with category ID that doesn't require extra tabs
7 $target_products_ids = array(
8 18, // Supplies
9 43, // Glass
10 80, // Education
11 15 // Uncat
12 );
13
14 // Get the global product object
15 global $product;
16
17 // Stores the current product's category ID(s)
18 $product_cats_ids = wc_get_product_term_ids( $product->get_id(), 'product_cat' );
19
20 // Comapres both arrays and prints out which element was not found from $target_products_ids in $product_cats_ids
21 $difference = array_diff($target_products_ids, $product_cats_ids);
22
23 // As long as one of the elements from $difference is removed, the tabs for the current product should be unset
24 if(count($difference) != count($target_products_ids)) return $tabs;
25
26 // Creating global variables to be used within inner-functions (not 'product')
27 global $product, $current_meta_data, $indexFeature, $indexFaqs, $indexTechnicalDetails, $indexSupport;
28 $id = $product->get_id();
29
30 // Connects to WC Rest API
31 $woocommerce = new Client(
32 'www.example.com',
33 '###',
34 '###',
35 [
36 'wp_api' => true,
37 'version' => 'wc/v3',
38 'query_string_auth' => true # Force Basic Authentication as query string true and using under HTTPS
39 ]
40 );
41
42 $current_product = $woocommerce->get('products/'.$id);
43 $current_meta_data = $current_product->meta_data;
44
45 // Retrieves the current meta data for this current product
46 $keys = array_column($current_meta_data, 'key');
47
48 // Within that array of meta data, it finds the index of the given string (meta data name)
49 $indexFeature = array_search('_features', $keys);
50 $indexFaqs = array_search('_faqs', $keys);
51 $indexTechnicalDetails = array_search('_technical_details', $keys);
52 $indexSupport = array_search('_support', $keys);
53
54 // Will add the tab to the current product if the meta data's content is not empty
55 if(!empty( $current_meta_data[$indexFeature]->value )) {
56 $tabs['features2'] = array(
57 'title' => __( 'Features', 'woocommerce' ),
58 'priority' => 50,
59 'callback' => function() {
60 global $current_meta_data, $indexFeature;
61 echo $current_meta_data[$indexFeature]->value;
62 }
63 );
64 }
65 if(!empty( $current_meta_data[$indexFaqs]->value )) {
66 $tabs['faqs2'] = array(
67 'title' => __( 'FAQs', 'woocommerce' ),
68 'priority' => 51,
69 'callback' => function() {
70 global $current_meta_data, $indexFaqs;
71 echo $current_meta_data[$indexFaqs]->value;
72 }
73 );
74 }
75 if(!empty( $current_meta_data[$indexTechnicalDetails]->value )) {
76 $tabs['technical_details2'] = array(
77 'title' => __( 'Technical Details', 'woocommerce' ),
78 'priority' => 52,
79 'callback' => function() {
80 global $current_meta_data, $indexTechnicalDetails;
81 echo $current_meta_data[$indexTechnicalDetails]->value;
82 }
83 );
84 }
85 if(!empty( $current_meta_data[$indexSupport]->value )) {
86 $tabs['support2'] = array(
87 'title' => __( 'Support', 'woocommerce' ),
88 'priority' => 53,
89 'callback' => function() {
90 global $current_meta_data, $indexSupport;
91 echo $current_meta_data[$indexSupport]->value;
92 }
93 );
94 }
95
96 // Returns the changed tabs to this current product
97 return $tabs;
98
99}