0

I hope someone can help me as I'm fairly new to this.

I created a very simple plugin for my own site, but I've had a few people ask me for it because they like the functionality of it. However, they want to customise the main headings (h2 and h3). I added an options form to the admin area, but I don't know how to get the results to show on the front end. Any help would be greatly appreciated and fantastic.

<?php
/*
Plugin Name: Related Products Out of Stock Display 
Plugin URI: http://example.com/
Description: Displays related products above gallery when items are out of 
stock. Relies on categories and tags for best results 
Author: John Cook 
Version: 1.0
Author URI: http://www.example.com
License: GPL2
*/
add_action( 'woocommerce_before_single_product_summary', 
'my_related_out_of_stock', 15);
function my_related_out_of_stock () {
global $product;
$availability = $product->get_availability();

/*check if availability in the array = string 'Out of Stock'
**if so display on page.
*/
if ( $availability['availability'] == 'Out of stock') {
echo '<h2>';
echo WOOS_Related_Products_text_field_0;
echo '</h2>';
echo '<h3>';
echo WOOS_Related_Products_text_field_1;
echo '</h3>';
$args = array( 
'posts_per_page' => 4,  
'columns' => 4,  
'orderby' => 'rand' 
); 
woocommerce_related_products( apply_filters( 'my_related_out_of_stock_args', $args ) ); 
}
}
/*
**
**
**options page
**
**
*/
add_action( 'admin_menu', 'WOOS_Related_Products_add_admin_menu' );
add_action( 'admin_init', 'WOOS_Related_Products_settings_init' );


function WOOS_Related_Products_add_admin_menu() { 

add_options_page( 'Wordpress out of stock related products', 'Wordpress out of stock related products', 'manage_options', 'wordpress_out_of_stock_related_products', 'WOOS_Related_Products_options_page' );

}


function WOOS_Related_Products_settings_init() { 

register_setting( 'pluginPage', 'WOOS_Related_Products_settings' );

add_settings_section(
    'WOOS_Related_Products_pluginPage_section', 
    __( 'Create your custom out of stock message to display before related products', 'wordpress' ), 
    'WOOS_Related_Products_settings_section_callback', 
    'pluginPage'
);

add_settings_field( 
    'WOOS_Related_Products_text_field_0', 
    __( 'Main heading', 'wordpress' ), 
    'WOOS_Related_Products_text_field_0_render', 
    'pluginPage', 
    'WOOS_Related_Products_pluginPage_section' 
);

add_settings_field( 
    'WOOS_Related_Products_text_field_1', 
    __( 'Sub heading', 'wordpress' ), 
    'WOOS_Related_Products_text_field_1_render', 
    'pluginPage', 
    'WOOS_Related_Products_pluginPage_section' 
);


}


function WOOS_Related_Products_text_field_0_render(  ) { 

$options = get_option( 'WOOS_Related_Products_settings' );
?>
<input type='text' name='WOOS_Related_Products_settings[WOOS_Related_Products_text_field_0]' value='<?php echo $options['WOOS_Related_Products_text_field_0']; ?>'>
<?php

}


function WOOS_Related_Products_text_field_1_render(  ) { 

$options = get_option( 'WOOS_Related_Products_settings' );
?>
<input type='text' name='WOOS_Related_Products_settings[WOOS_Related_Products_text_field_1]' value='<?php echo $options['WOOS_Related_Products_text_field_1']; ?>'>
<?php

}


function WOOS_Related_Products_settings_section_callback(  ) { 

echo __( 'Create a custom message for your shoppers when they encounter an item that is out of stock. This custom message will appear before the related products', 'wordpress' );

}


function WOOS_Related_Products_options_page(  ) { 

?>
<form action='options.php' method='post'>

    <h1>Wordpress out of stock related products</h1>

    <?php
    settings_fields( 'pluginPage' );
    do_settings_sections( 'pluginPage' );
    submit_button();
    ?>

</form>
<?php

}

?>
5
  • you can read the values in the same way that in backend with $options = get_option( 'WOOS_Related_Products_settings' );.
    – mmm
    Apr 28, 2018 at 12:18
  • I'm not entirely sure if I follow what you mean. You might have to ELI5 to me. What I'm trying to show is the results from the form in the h2 and h3 on the frontend. I feel like I'm close, but yet so far
    – John Cook
    Apr 28, 2018 at 12:56
  • yes it's that, look what is in $options.
    – mmm
    Apr 28, 2018 at 17:17
  • where you save WOOS_Related_Products_text_field_0 and WOOS_Related_Products_text_field_1?
    – Manyang
    Apr 29, 2018 at 0:47
  • Hi Manyang. Yes, I want to display the entered text from the form fields in the admin area in those fields on the frontend where the h2 and h3 are
    – John Cook
    Apr 29, 2018 at 2:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.