I am having major problems for 3-4 weeks now. A plugin I have written (I'm no coder, I've just copied and altered from another plugin) outputs a white screen of death after saving the product. On the frontend everything's good and displays the function perfectly.
The plugin should get the price from the database and output it via a shortcode [display_price] for normal theme style or [display_price_no_style] without any styling.
See the code of the plugin attached.
Your help is greatly appreciated :) :)
(btw: I also have the problem with other functions such as "get_sku" or "get_title")
Thank you so much, Patrick
So this should create a shortcode that executes the function to get the price and outputs properly. When saving a product that includes the shortcode the following error occurs:
Fatal error: Uncaught Error: Call to a member function get_title() on null in /home/.sites/123/site5586569/web/wordpress/wp-content/plugins/woocommerce-display-various/display_various.php:180 Stack trace: #0 /home/.sites/123/site5586569/web/wordpress/wp-content/plugins/woocommerce-display-various/display_various.php(140): display_info(5449) #1 /home/.sites/123/site5586569/web/wordpress/wp-includes/shortcodes.php(319): display_info_shortcode('', '', 'display_info') #2 [internal function]: do_shortcode_tag(Array) #3 /home/.sites/123/site5586569/web/wordpress/wp-includes/shortcodes.php(197): preg_replace_callback('/\[(\[?)(displa...', 'do_shortcode_ta...', '[display_info]\n') #4 /home/.sites/123/site5586569/web/wordpress/wp-content/themes/enfold/config-templatebuilder/avia-template-builder/php/shortcode-helper.class.php(485): do_shortcode('[display_info]\n') #5 /home/.sites/123/site5586569/web/wordpress/wp-content/themes/enfold/config-templatebuilder/avia-shortcodes/textblock.php(253): ShortcodeHelper::avia_apply_autop('
Plugin Code
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ){
add_shortcode('display_price', 'display_price_shortcode');
}
function display_price_shortcode($atts) {
global $post;
extract(shortcode_atts(array(
'id' => $post->ID,
), $atts));
if(get_post_type($id)=='product'){
ob_start();
display_price_no_style_only($id);
return ob_get_clean();
}else{
return "";
}
}
function display_price_no_style_only($id="" ){
global $wpdb;
global $post;
global $product;
if(empty($id)){
$id=$post->post_id;
}
if(get_post_type( $id )=='product'){
$derpreis = $wpdb->get_var( "
SELECT COUNT(meta_value) FROM $wpdb->postmeta
WHERE meta_key = '_price'
AND post_id = $id
");
}
if($derpreis > 0 ) {
echo '<span class="displayprice">'. $product->get_price_html() . '</span>';
}
if($derpreis = 0 ) {
echo 'Preis nicht verfügbar';
}
}
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ){
add_shortcode('display_price_no_style', 'display_price_no_style_shortcode');
}
function display_price_no_style_shortcode($atts) {
global $post;
extract(shortcode_atts(array(
'id' => $post->ID,
), $atts));
if(get_post_type($id)=='product'){
ob_start();
display_price_only($id);
return ob_get_clean();
}else{
return "";
}
}
function display_price_only($id="" ){
global $wpdb;
global $post;
global $product;
if(empty($id)){
$id=$post->post_id;
}
if(get_post_type( $id )=='product'){
$derpreis = $wpdb->get_var( "
SELECT COUNT(meta_value) FROM $wpdb->postmeta
WHERE meta_key = '_price'
AND post_id = $id
");
}
if($derpreis > 0 ) {
echo $product->get_price_html();
}
if($derpreis = 0 ) {
echo 'Kein Preis verfügbar';
}
}