• Resolved torbent

    (@torbentschechnegmailcom)


    Hello,
    I want to create WooCommerce Tabs dynamically on basis of the product variation. So I do this:

    foreach ($variant_attributes as $variant_id => $variant_attribute) {
        $tabs[$variant_attribute['attribute_art']] = [
            'title'    => $variant_attribute['attribute_art'],
            'priority' => 16,
            'callback' => 'custom_tab_content'
        ];
    }

    I am now asking myself how can I create the callback dynamically in order to show different content for each tabs? I need to have the variant_id in the tab contents to distinguish which content should be displayed.

    I have a few questions:

    1. Can I somehow pass data to my callback, so that the function is called as this custom_tab_content($variant_id)?

    2. I solved it like this, but I am sure that this is not the best solution (I am using eval here).

    
    foreach ($variant_attributes as $variant_id => $variant_attribute) {
    	$tabs[$variant_attribute['attribute_art']] = [
    		'title'    => $variant_attribute['attribute_art'],
    		'priority' => 16,
    		'callback' => 'custom_tab_content_'.$variant_attribute['attribute_art']
    	];
    
    	eval('function custom_tab_content_'.$variant_attribute['attribute_art'].' () {
    		echo '.$variant_attribute['attribute_art'].';
    	}');
    
    }
    

    Can you give guidance how I can solve this in the best way?

    Thanks!

    • This topic was modified 7 years ago by torbent.
Viewing 1 replies (of 1 total)
  • Thread Starter torbent

    (@torbentschechnegmailcom)

    I solved it:

    foreach ($variant_attributes as $variant_id => $variant_attribute) {
    				
    	$tabs[$variant_attribute['attribute_art']] = [
    		'title'    => $variant_attribute['attribute_art'],
    		'priority' => 16,
    		'variant_id' => $variant_id,
    		'callback' => function($arg, $params) {
    
    			echo $params['variant_id'];
    		}
    	];
    }
Viewing 1 replies (of 1 total)
  • The topic ‘Dynamically create woocommerce tabs’ is closed to new replies.