0

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';
    }
}
0

The 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

  • unfortunately my comment was to long as a follow-up question so I had to write an "answer". Could you also take a look at this? :) – Patrick Batemann Jun 22 '18 at 13:26
  • @PatrickBatemann. Sorry, you can't use Answers to ask follow-up questions. You said the price now works as expected and then you say 'it doesn't resolve the problem'.. What problem exactly? Are you seeing errors? I recommend you close this question. Create a new one with a good problem description, error logs if you have them and include all the code. Tag me inside the question and i'll take a look. Regards, Bjorn – Bjorn Jun 23 '18 at 4:08
0

thank you so much for your amazing help. The price thing now works as it should. Unfortunately the code is really old and I am no coder as one might have noticed.

I tried to adapt your style to the other parts of the plugin (that gets title, price, sku, categories, ...) but unfortunately it doesn't resolve the problem and I don't knwo where I am stuck.

These are the other codes. Is it possible for you to take a look there? :)

add_action('init','add_custom_info_shortcodes');

function add_custom_info_shortcodes() {
if ( class_exists( 'woocommerce' ) ) {
    add_shortcode('display_info', 'display_info_shortcode');
 }
}

function display_info_shortcode($atts) {
global $post;

extract(shortcode_atts(array(
    'id' => $post->ID,
    'link' => 'true',
), $atts));

if(get_post_type($id)=='product'){
    ob_start();
    display_info($id);
    return ob_get_clean();
}else{
    return "";
 }

}

function display_info($id=""){
global $wpdb;
global $product;

if(get_post_type( $id )=='product'){
    $produkttitel = $wpdb->get_var( "
        SELECT COUNT(post_title) FROM $wpdb->posts
        WHERE post_type = 'product'
        AND ID = $id
        ");
}

if($produkttitel != 0 ) {
    echo '<h1><span itemprop="name" class="product_title entry-title">'. $product->get_title() . '</span></h1>';
    echo '<span class="displayprice">'. $product->get_price_html() . '</span>';
} else {        
    echo 'Kein Titel verfügbar';
}
}

and the second one is

if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ){
add_shortcode('display_sku_cat', 'display_sku_cat_shortcode');
}

function display_sku_cat_shortcode($atts) {
global $post;

extract(shortcode_atts(array(
    'id' => $post->ID,
    'link' => 'true',
), $atts));

if($link==='true'|$link==='false') {
    $link = ($link === 'true');
}

if(get_post_type($id)=='product'){
    ob_start();
    display_sku_cat($id);
    return ob_get_clean();
}else{
    return "";
}

}

function display_sku_cat($id="", $permalink=false ){
global $wpdb;
global $post;
global $product;

if(empty($id)){
    $id=$post->post_id;
}

if(is_bool($permalink)) {
    if($permalink){
        $link = get_permalink( $id );
    }
}else{
    $link = $permalink;
    $permalink = true;
}

$target = "";        
if($newwindow == 'true'){
    $target="target='_blank' ";
}

if(get_post_type( $id )=='product'){
    $artikelnummer = $wpdb->get_var( "
    SELECT COUNT(meta value) FROM $wpdb->postmeta
    WHERE meta_key = '_sku'
    AND post_id = $id
    ");

}

if ( wc_product_sku_enabled() && ( $product->get_sku() || $product->is_type( 'variable' ) ) ) : ?>

    <span style="font-size: 11px;">
    <span class="sku_wrapper"><?php _e( 'SKU:', 'woocommerce' ); ?> <span class="sku" itemprop="sku"><?php echo ( $sku = $product->get_sku() ) ? $sku : __( 'N/A', 'woocommerce' ); ?></span></span></br >

    <?php endif; ?>

    <?php echo $product->get_categories( ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', $cat_count, 'woocommerce' ) . ' ', '</span>' ); ?>

    </span>
<?php

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.