Title: PHP Snippet
Last modified: October 12, 2021

---

# PHP Snippet

 *  Resolved [amanfredini](https://wordpress.org/support/users/amanfredini/)
 * (@amanfredini)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/php-snippet-3/)
 * Hi,
 * how can I modify custom tab value from PHP snippets?
 * I need to create in a foreach loop woocommerce product. I’d like to insert a 
   value in a Custom Tab. Can I?

Viewing 11 replies - 1 through 11 (of 11 total)

 *  [jpowersdev](https://wordpress.org/support/users/jpowersdev/)
 * (@jpowersdev)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/php-snippet-3/#post-14962699)
 * Hi [@amanfredini](https://wordpress.org/support/users/amanfredini/),
 * Here is a docs page on editing the list of tabs by using a filter – [https://docs.woocommerce.com/document/editing-product-data-tabs/](https://docs.woocommerce.com/document/editing-product-data-tabs/).
 * The tabs created by our plugin have a “content” key which can be modified through
   this filter. Something like this should work:
 *     ```
       add_filter( 'woocommerce_product_tabs', 'yikes_edit_tab_content', 98 );
   
       function yikes_edit_tab_content( $tabs ) {
       	foreach( array_keys( $tabs ) as $key ) {
       		if ('tab-slug' === $key) {
       			$tabs[$key]['content'] = 'My new content';	
       		}
       	}
   
       	return $tabs;
       }
       ```
   
 * Let me know if that helps,
    Jon
 *  Thread Starter [amanfredini](https://wordpress.org/support/users/amanfredini/)
 * (@amanfredini)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/php-snippet-3/#post-14962731)
 * Ok, i can modify tabs of specific product id?
 *  [jpowersdev](https://wordpress.org/support/users/jpowersdev/)
 * (@jpowersdev)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/php-snippet-3/#post-14962743)
 * Hi [@amanfredini](https://wordpress.org/support/users/amanfredini/),
 * Yes. If the loop hasn’t been modified, `get_the_ID()` should work. Alternatively,
   you can use this:
 *     ```
       global $product;
       $id = $product->get_id();
       ```
   
 * Jon
 *  [jpowersdev](https://wordpress.org/support/users/jpowersdev/)
 * (@jpowersdev)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/php-snippet-3/#post-14962744)
 * To clarify – you would wrap the entire thing in an if statement checking if you
   are on the correct product.
 *  Thread Starter [amanfredini](https://wordpress.org/support/users/amanfredini/)
 * (@amanfredini)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/php-snippet-3/#post-14966754)
 * I mean, how can i add a tab to specific product in a foreach loop?
 * I need to call the add_Action every time?
 *  [jpowersdev](https://wordpress.org/support/users/jpowersdev/)
 * (@jpowersdev)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/php-snippet-3/#post-14966945)
 * Hi [@amanfredini](https://wordpress.org/support/users/amanfredini/),
 * So the add filter is going to run each time a product page loads. We just need
   to check (inside the filter function) if you are on the correct product.
 *     ```
       add_filter( 'woocommerce_product_tabs', 'yikes_edit_tab_content', 98 );
   
       function yikes_edit_tab_content( $tabs ) {
       	global $product;
       	$id = $product->get_id();
   
       	// Check desired product ID.
       	if ( $id === 0 ) {
       		foreach( array_keys( $tabs ) as $key ) {
       			if ('tab-slug' === $key) {
       				$tabs[$key]['content'] = 'My new content';	
       			}
       		}
       	}
   
       	return $tabs;
       }
       ```
   
 * If you need to do this with multiple products, you can create an associative 
   array above the function call that is keyed on the product ID with the content
   as the value. Something like this:
 *     ```
       $arr = array(
               10 => 'My content for product ID 10',
       );
       ```
   
 * Then you’ll pull the content value from the array above instead of setting it
   manually in your filter function.
 * That being said, the reason for using a plugin like ours is to avoid doing that
   sort of thing. If you need conditional content for a single product it can make
   sense to do this, but otherwise, you are probably better off defining a shortcode
   and adding that shortcode to a product tab using our plugin.
 * Let me know if that helps,
    Jon
 *  Thread Starter [amanfredini](https://wordpress.org/support/users/amanfredini/)
 * (@amanfredini)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/php-snippet-3/#post-14968135)
 * I mean, i need to batch create woocommerce product from a big csv.
 * In foreach loop i’ve called woocommerce API to create the product. I have stored
   the product id in a variable. How can i add tab data individually for each product
   when i will create them?
 *  [jpowersdev](https://wordpress.org/support/users/jpowersdev/)
 * (@jpowersdev)
 * [4 years, 7 months ago](https://wordpress.org/support/topic/php-snippet-3/#post-14970221)
 * Hi [@amanfredini](https://wordpress.org/support/users/amanfredini/),
 * Ah, I totally misunderstood what you were looking to do. You actually want to
   write data to the database.
 * The tabs are stored as post meta, so you can use the `update_post_meta` function.`
   yikes_woo_products_tabs` is the meta key. You’ll want to shape the data this:
 *     ```
       array(
       	array(
       		'title' => 'Tab Title',
       		'id' => 'tab-title',
       		'content' => '<p>Some html</p>'
       	)
       )
       ```
   
 * Let me know if that helps,
    Jon
 *  Thread Starter [amanfredini](https://wordpress.org/support/users/amanfredini/)
 * (@amanfredini)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/php-snippet-3/#post-14993015)
 * Yes, when i insert with update_meta_data all work fine.
 * When i update the product (for example the price or other attribute) and i click
   Save, custom tab disappear
 *  Thread Starter [amanfredini](https://wordpress.org/support/users/amanfredini/)
 * (@amanfredini)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/php-snippet-3/#post-14993074)
 * Sorry, if i would like to call two times update_post_meta for insert in two different
   part of the code, how can i do?
 * If i call two times update_post_meta, the last call owerwrite previous insert
 *  [jpowersdev](https://wordpress.org/support/users/jpowersdev/)
 * (@jpowersdev)
 * [4 years, 6 months ago](https://wordpress.org/support/topic/php-snippet-3/#post-14993394)
 * Hi [@amanfredini](https://wordpress.org/support/users/amanfredini/),
 * The disappearing-on-save bug could be a number of different things. Are you using
   any other plugins that might be modifying the edit product page?
 * Yes, every time you call update_post_meta it replaces the existing value with
   the new one. If you need to add data instead of overriding it, you should first
   fetch the current value and add the new information to that before saving.
 * Jon

Viewing 11 replies - 1 through 11 (of 11 total)

The topic ‘PHP Snippet’ is closed to new replies.

 * ![](https://ps.w.org/yikes-inc-easy-custom-woocommerce-product-tabs/assets/icon-
   256x256.png?rev=1558461)
 * [Custom Product Tabs for WooCommerce](https://wordpress.org/plugins/yikes-inc-easy-custom-woocommerce-product-tabs/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/yikes-inc-easy-custom-woocommerce-product-tabs/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/yikes-inc-easy-custom-woocommerce-product-tabs/)
 * [Active Topics](https://wordpress.org/support/plugin/yikes-inc-easy-custom-woocommerce-product-tabs/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/yikes-inc-easy-custom-woocommerce-product-tabs/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/yikes-inc-easy-custom-woocommerce-product-tabs/reviews/)

 * 11 replies
 * 2 participants
 * Last reply from: [jpowersdev](https://wordpress.org/support/users/jpowersdev/)
 * Last activity: [4 years, 6 months ago](https://wordpress.org/support/topic/php-snippet-3/#post-14993394)
 * Status: resolved