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';
}
}
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';
}
}
Share
Improve this question
edited Jun 8, 2018 at 13:03
fuxia♦
107k39 gold badges255 silver badges459 bronze badges
asked Jun 8, 2018 at 12:19
Patrick BatemannPatrick Batemann
11 bronze badge
1 Answer
Reset to default 1The error
Is coming from the file:
/wp-content/plugins/woocommerce-display-various/display_various.php
Line 180.
Disable the plugin 'woocommerce-display-various' and check if the problem still exists.
Your code
Why are you using output buffering (ob_start()
)? Furthermore you're not using it correctly.
There's alot to improve on your code...
See my changes:
add_action('init','add_custom_price_shortcodes');
function add_custom_price_shortcodes() {
if ( class_exists( 'woocommerce' ) ) {
add_shortcode('display_price', 'display_price_shortcode');
add_shortcode('display_price_no_style', 'display_price_no_style_shortcode');
}
}
function display_price_shortcode() {
global $post;
if(get_post_type($post->ID) == 'product'){
return price_shortcode_display_price($post->ID, 'with_style');
} else {
return "";
}
}
function display_price_no_style_shortcode() {
global $post;
if(get_post_type($post->ID) == 'product'){
return price_shortcode_display_price($post->ID, 'no_style');
} else {
return "";
}
}
function price_shortcode_display_price($post_id, $type = 'no_style') {
$product = wc_get_product($post_id);
if($product) {
$derpreis = $product->get_price();
if($derpreis > 0 ) {
if( $type == 'no_style') {
return $product->get_price_html();
}
if( $type == 'with_style') {
return '<span class="displayprice">'. $product->get_price_html() . '</span>';
}
} else {
return 'Kein Preis verfügbar';
}
} else {
return 'Kein Preis verfügbar';
}
}
Regards, Bjorn