I’ve managed so as to add two customized meta fields to the backend of woocommerce, transport and dimension guides, utilizing the add_meta_boxes operate, as I do not wish to use the woocommerce customized fields part as these will likely be constant throughout most merchandise. I’ll then use these new fields to show knowledge in numerous factors on my web site, together with pop up buttons and customized tabs.
I’ve stored the instance quick and with the safety stuff lacking, however it’s best to get an concept.
add_action( 'add_meta_boxes', 'additional_product_tabs_metabox' );
operate additional_product_tabs_metabox()
{
add_meta_box(
'add_product_metabox_additional_tabs',
__( 'Further product Tabs', 'woocommerce' ),
'additional_product_tabs_metabox_content',
'product',
'regular',
'excessive'
);
}
// Add customized metabox content material
operate additional_product_tabs_metabox_content( $put up )
{
echo '<h4>' . __( 'Transport', 'woocommerce' ) . '</h4>';
$worth = get_post_meta( $post->ID, '_shipping_details', true );
wp_editor( $worth, '_shipping_details', array( 'editor_height' => 100 ) );
echo '<h4>' . __( 'Dimension Information', 'woocommerce' ) . '</h4>';
$worth = get_post_meta( $post->ID, '_side_guide', true );
wp_editor( $worth, '_side_guide', array( 'editor_height' => 100 ) );
// Sanitize person enter and save the put up meta fields values.
if( isset($_POST[ '_shipping_details' ]) )
update_post_meta( $post_id, '_shipping_details', wp_kses_post($_POST[ '_shipping_details' ]) );
if( isset($_POST[ '_side_guide' ]) )
update_post_meta( $post_id, '_side_guide', wp_kses_post($_POST[ '_side_guide' ]) );
What I am combating is getting the ultimate outcomes to show ONLY if I even have knowledge or textual content within the fields on the product web page backend.
For the customized tabs, that is what I’ve (and it really works), however I questioned if there was a greater method that does not contain utilizing the endif.
// Provides the Transport Particulars Tab
if( get_post_meta( $post->ID, '_shipping_details', true )):
$tabs['shipping_details_tab'] = array(
'title' => __( 'Transport Particulars', 'woocommerce' ),
'precedence' => 15,
'callback' => 'woo_shipping_details_tab_content'
);endif;
// Provides the Dimension Information Particulars Tab
if( get_post_meta( $post->ID, '_size_guide', true )):
$tabs['size_guide_tab'] = array(
'title' => __( 'Dimension Information', 'woocommerce' ),
'precedence' => 15,
'callback' => 'woo_size_guide_tab_content'
);endif;
For different events, I would like it to look as a button, which opens in a lightbox in numerous elements of my product web page. However solely when knowledge is current within the product backend.
The one resolution I’ve discovered was:
add_action( 'woocommerce_before_single_variation','size_guide_example', 12);
operate size_guide_example(){
$post_id = get_post_meta( get_the_ID(), '_size_guide', true );
if ($post_id == '') {
echo '';
} else {
echo /*my content material */);
}
Nonetheless, as you may see that resolution is not perfect both. I’ve tried a whooooole bunch of various code reminiscent of:
$post_id = get_the_ID();
if ( metadata_exists( 'put up', $post_id, '_size_guide' ) ) {
if( ! in_array( '_size_guide', get_post_custom_keys($post_id) ) ) {}
if ( metadata_exists( 'put up', $post_id, '_size_guide' ) ) {
$meta_value = get_post_meta( $post_id, '_size_guide', true );
}
And extra, however none of them work!
Does anybody have some higher options for me?
Brief model: I would like my dimension information and transport data, as proven within the picture under, to show ONLY if there may be textual content or code within the precise textual content space. I would like the knowledge to NOT present if it is clean. This may apply to hiding/displaying further tabs, and hiding/displaying buttons that may open the knowledge in a lightbox.
THANKYOU