PHP Snippet
-
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?
-
Hi @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/.
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,
JonOk, i can modify tabs of specific product id?
Hi @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
To clarify – you would wrap the entire thing in an if statement checking if you are on the correct product.
I mean, how can i add a tab to specific product in a foreach loop?
I need to call the add_Action every time?
Hi @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,
JonI 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?
Hi @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_metafunction.yikes_woo_products_tabsis 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,
JonYes, 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
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
Hi @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
The topic ‘PHP Snippet’ is closed to new replies.