• Resolved devsaredead

    (@devsaredead)


    I’m trying to nail down the issues that prevent my PHP version to upgrade to 8.0, and among others I got an error related to CodeSnippet:

    code-snippets/php/snippet-ops.php(582) : eval()’d code on line 11

    I have no clue. Can anyone help me fix the error? Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hi @devsaredead,

    This error is coming from one of the snippets you’ve added.

    Are you able to post the full error message? We’re looking for a snippet that’s at least 11 lines long.

    Thread Starter devsaredead

    (@devsaredead)

    yes, the full error is this:

    Fatal error: Uncaught Error: Call to undefined function create_function() in /…/wp-content/plugins/code-snippets/php/snippet-ops.php(582) : eval()’d code:11 Stack trace: #0 /…/wp-content/plugins/code-snippets/php/snippet-ops.php(582): eval() #1 /…/wp-content/plugins/code-snippets/php/snippet-ops.php(663): Code_Snippets\execute_snippet(‘//**** SHOP PAG…’, 17) #2 /…/wp-includes/class-wp-hook.php(324): Code_Snippets\execute_active_snippets(”) #3 /…/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array) #4 /…/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #5 /…/wp-settings.php(550): do_action(‘plugins_loaded’) #6 /…/wp-config.php(98): require_once(‘/home/agenda/ba…’) #7 /…/wp-load.php(50): require_once(‘/…’) #8 /…/wp-blog-header.php(13): require_once(‘/…/’) #9 /…/index.php(17): require(‘/…’) #10 {main} thrown in /…/wp-content/plugins/code-snippets/php/snippet-ops.php(582) : eval()’d code on line 11

    I have noticed the //**** SHOP PAG tag, so I went to disable the single snippet and it fixes the error. The problem is that it’s a snippet with several functions related to Woocommerce, so it’s hard to spot which one in particular

    Plugin Author Shea Bunge

    (@bungeshea)

    create_function is very old PHP syntax. If you know which snippet is causing the problem, share the code here and I might be able to help you fix it up.

    Thread Starter devsaredead

    (@devsaredead)

    ….would you be really so kind to review this long snippets? (some are commented, i.e. not currently in use)

    //**** SHOP PAGE ****// 
    
    // 0) MAIN SHOP DESCRIPTION
    function avs_shop_description() {
    $description = '<p class="storemotto">SEGUITE IL VOSTRO ISTINTO... COMPRATE DA NOI</p>';
    echo $description;
    }
    add_action('woocommerce_archive_description', 'avs_shop_description');
    
    // 1) CHANGE NUMBER OF PRODUCTS DISPLAYED PER PAGE
    add_filter( 'loop_shop_per_page', create_function( '$products', 'return 9;' ), 30 );
    
    // 2) HIDE CATEGORY PRODUCT COUNT IN PRODUCT ARCHIVES
    add_filter( 'woocommerce_subcategory_count_html', '__return_false' );
    
    // 3) SHOW CATEGORY BELONGING TO EACH PRODUCT
    add_action( 'woocommerce_shop_loop_item_title', 'avs_show_all_subcats', 2 );
    function avs_show_all_subcats() {
       global $product;
       $cats = get_the_terms( $product->get_id(), 'product_cat' );
       if ( empty( $cats ) ) return;
       echo join( ', ', wp_list_pluck( $cats, 'name' ) );
    }
    
    // 4a) PRICE DISPLAY "Free" IF ZERO OR "non disponibile" IF EMPTY
    add_filter( 'woocommerce_get_price_html', 'avs_price_free_zero_empty', 9999, 2 );
    function avs_price_free_zero_empty( $price, $product ){
        if ( 0 == $product->get_price() ) { $price = '<span class="woocommerce-Price-amount amount">omaggio</span>'; }
        if ( '' === $product->get_price() ) { $price = '<span class="woocommerce-Price-amount amount">prezzo n/d</span>'; }  
        return $price;
    }
    
    // 4b) CATALOGUE DISPLAY "non disponibile" IF OUT OF STOCK
    add_filter( 'woocommerce_product_add_to_cart_text', 'avs_archive_custom_cart_button_text' );
    function avs_archive_custom_cart_button_text( $text ) {
       global $product;       
       if ( $product && ! $product->is_in_stock() ) {           
          return 'non disponibile';
       } 
       return $text;
    }
    
    // 5a) ALLOW/DISALLOW GUEST CHECKOUT OF THE PRODUCT (ADDS A METABOX IN THE PRODUCT) 
    // source: https://gist.github.com/contemplate/2adc7be2c72d585a07ac6f90b1f1e1b4 (https://gist.github.com/mikejolley/5fadb94dd9eff11ab512)
    // Display Guest Checkout Field
    add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields' );
    function woo_add_custom_general_fields() {
      global $woocommerce, $post;
      echo '<div class="options_group">';
      // Checkbox
      woocommerce_wp_checkbox( 
      array( 
    	'id'            => '_allow_guest_checkout', 
    	'label'         => __('Checkout', 'woocommerce' ), 
    	'description'   => __('Permetti acquisto senza registrarsi (Guest Checkout)', 'woocommerce' ) 
    	)
       );
      echo '</div>';
    }
    // Save Guest Checkout Field
    add_action( 'woocommerce_process_product_meta', 'woo_add_custom_general_fields_save' );
    function woo_add_custom_general_fields_save( $post_id ){
    	$woocommerce_checkbox = isset( $_POST['_allow_guest_checkout'] ) ? 'yes' : 'no';
    	update_post_meta( $post_id, '_allow_guest_checkout', $woocommerce_checkbox );
    }
    // Enable Guest Checkout on Certain products
    add_filter( 'pre_option_woocommerce_enable_guest_checkout', 'enable_guest_checkout_based_on_product' );
    function enable_guest_checkout_based_on_product( $value ) {
      if ( WC()->cart ) {
        $cart = WC()->cart->get_cart();
        foreach ( $cart as $item ) {
          if ( get_post_meta( $item['product_id'], '_allow_guest_checkout', true ) == 'yes' ) {
            $value = "yes";
          } else {
            $value = "no";
            break;
          }
        }
      }
      return $value;
    }
    
    // 5b) FREE DOWNLOADS (PRODUCTS) WITHOUT CHECKOUT
    //function direct_free_downloads_button($button)
    //{
    //    global $product;
    //    if( $product->is_downloadable() AND $product->get_price() == 0 )
    //    {
    //        $files = $product->get_files();
    //        $files = array_keys($files);
    //        $download_url = home_url('?download_file='.$product->id.'&key='.$files[0].'&free=1' );
    //        $button = sprintf( '<a href="%s" rel="nofollow" data-product_id="%s" data-product_sku="%s" data-quantity="%s" class="button %s product_type_%s">%s</a>',
    //            esc_url( $download_url  ),
    //            esc_attr( $product->id ),
    //            esc_attr( $product->get_sku() ),
    //           esc_attr( isset( $quantity ) ? $quantity : 1 ),
    //            $product->is_purchasable() && $product->is_in_stock() ? '' : '',
    //           esc_attr( $product->product_type ),
    //            esc_html( 'omaggio' )
    //        );
    //    }
    //    return $button;
    //}
    //add_filter('woocommerce_loop_add_to_cart_link', 'direct_free_downloads_button', 100);
    //
    /**  Handles downloading of free Downloadable products * @return [type] [description]  */
    //function download_free_product_file()
    //{
    //    $product_id    = absint( $_GET['download_file'] );
    //    $_product      = wc_get_product( $product_id );
    //
    //    if( $_product->get_price() == 0 ) {
    //        WC_Download_Handler::download( $_product->get_file_download_path( filter_var($_GET['key'], FILTER_SANITIZE_STRING)  ), $product_id );
    //    }
    //}
    //if ( isset( $_GET['download_file'] ) && isset( $_GET['key'] ) && $_GET['free'] ) {
    //    add_action( 'init', 'download_free_product_file' );
    //}
    
    
    
    //** PRODUCT PAGE **// 
    
    // 6a) ADD Back-to-store BUTTON IN PRODUCT PAGE
    add_action('woocommerce_single_product_summary', 'avs_back_to_store', 45);
    // 6b) ADD Back-to-store BUTTON IN CHECKOUT PAGE
    add_action( 'woocommerce_cart_actions', 'avs_back_to_store' );
    function avs_back_to_store() { ?>
    <a class="button wc-backward" href="<?php echo get_permalink( wc_get_page_id( 'shop' ) ); ?>"><?php _e( 'tornare al Duty Free', 'woocommerce' ) ?></a>
    <?php
    }
    
    // 7) SHOW CATEGORY TITLE IN PRODUCT PAGE
    add_filter( 'woocommerce_show_page_title', 'not_a_shop_page' );
    function not_a_shop_page() {
        return boolval(!is_shop());
    }
    
    // 8) SHOW DEFAULT DESCRIPTION WHEN SHORT ONE IS EMPTY
    add_action( 'woocommerce_single_product_summary', 'avs_echo_short_desc_if_empty', 21 );
    function avs_echo_short_desc_if_empty() {
       global $post;
       if ( empty ( $post->post_excerpt  ) ) {
          $post_excerpt = '<p>';
          $post_excerpt .= 'Questo articolo è pubblicato dalla Redazione a seguito di una ricerca di mercato indipendente.<br>Il produttore/distributore può gestirne la promozione e le vendite <a href="/membership-account/membership-levels/">registrando la propria azienda.</a>';
          $post_excerpt .= '</p>';
          echo $post_excerpt;
       }
    }
    
    // 9) CHANGE Add-to-Cart BUTTON LABEL IF PRODUCT IS ALREADY IN CART
    // Edit Single Product Page Add to Cart
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'avs_custom_add_cart_button_single_product' );
    function avs_custom_add_cart_button_single_product( $label ) {
       foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
          $product = $values['data'];
          if( get_the_ID() == $product->get_id() ) {
             $label = __('aggiungere ?', 'woocommerce');
          }
       }
       return $label;
    }
    // Edit Loop Pages Add to Cart
    add_filter( 'woocommerce_product_add_to_cart_text', 'avs_custom_add_cart_button_loop', 99, 2 );
    function avs_custom_add_cart_button_loop( $label, $product ) {
       if ( $product->get_type() == 'simple' && $product->is_purchasable() && $product->is_in_stock() ) {
          foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
             $_product = $values['data'];
             if( get_the_ID() == $_product->get_id() ) {
                $label = __('Aggiungere ?', 'woocommerce');
             }
          }
       }
       return $label;
    }
    
    // 10) MOVE RELATED PRODUCTS INTO A NEW DESCRIPTION TAB
    // remove related products from their original position
    remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20);
    // add a new tab
    add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
    function woo_new_product_tab( $tabs ) {
    $tabs['related_products'] = array(
       'title'    => __( 'PRODOTTI ANALOGHI', 'woocommerce' ),
       'priority'    => 50,
       'callback'    => 'woo_new_product_tab_content'
    );
       return $tabs;
    }
    // put the related products inside tab
    function woo_new_product_tab_content() {
    woocommerce_output_related_products();
    }
    
    
    
    //** CART PAGE **// 
    
    // 11) SHOW TOTAL WEIGHT IN CART
    add_action( 'woocommerce_before_cart', 'avs_print_cart_weight' );
    add_action( 'woocommerce_before_checkout_form', 'avs_print_cart_weight' );
    function avs_print_cart_weight() {
       $notice = 'Il Vostro bagaglio pesa: ' . WC()->cart->get_cart_contents_weight() . get_option( 'woocommerce_weight_unit' );
       if ( is_cart() ) {
          wc_print_notice( $notice, 'notice' );
       } else {
          wc_add_notice( $notice, 'notice' );
       }
    }
    
    // 12) DISPLAY Regular/Sale SUBTOTAL IN CART TABLE
    add_filter( 'woocommerce_cart_item_price', 'avs_change_cart_table_price_display', 30, 3 );
    function avs_change_cart_table_price_display( $price, $values, $cart_item_key ) {
       $slashed_price = $values['data']->get_price_html();
       $is_on_sale = $values['data']->is_on_sale();
       if ( $is_on_sale ) {
          $price = $slashed_price;
       }
       return $price;
    }
    
    // 13) DISPLAY CATEGORIES UNDER PRODUCTS NAMES IN CART TABLE
    add_filter( 'woocommerce_cart_item_name', 'avs_cart_item_category', 9999, 3 );
    function avs_cart_item_category( $name, $cart_item, $cart_item_key ) {
       $product = $cart_item['data'];
       if ( $product->is_type( 'variation' ) ) {
          $product = wc_get_product( $product->get_parent_id() );
       }
       $cat_ids = $product->get_category_ids();
       if ( $cat_ids ) $name .= '<br>' . wc_get_product_category_list( $product->get_id(), ', ', '<span class="posted_in">' . _n( 'Category:', 'Categories:', count( $cat_ids ), 'woocommerce' ) . ' ', '</span>' );
       return $name;
    }
    
    
    
    //** CHECKOUT PAGE **// 
    
    // 14) MESSAGE OF REMAINING COST TO ENABLE FREE SHIPPING
    function avs_avviso_sped_free() {
       $min_free = 50; // importo minimo per spedizione gratuita impostato su WooCommerce
       $cart_now = WC()->cart->subtotal;
       if ( $cart_now < $min_free ) {
          $added_text = 'Attenzione: aggiungendo ulteriori <strong>' . wc_price( $min_free - $cart_now ) . '</strong> al bagaglio, la spedizione sarà gratuita.';
          $notice = sprintf( $added_text );
          wc_print_notice( $notice, 'notice' );
       } 
    }
    add_action( 'woocommerce_before_checkout_form', 'avs_avviso_sped_free' );
    
    // 15) ADD Back-to-cart BUTTON IN CHECKOUT PAGE
    add_action( 'woocommerce_before_checkout_form', 'avs_return_to_cart_notice_button' );
    function avs_return_to_cart_notice_button(){
        $message = __('Avete altro da dichiarare?', 'woocommerce');
        $button_text = __('mostrate nuovamente il babaglio', 'woocommerce');
        $cart_link = WC()->cart->get_cart_url();
        wc_add_notice( '<a href="' . $cart_link . '" class="button wc-forward">' . $button_text . '</a>' . $message, 'notice' );
    }
    add_filter('ywctm_modify_woocommerce_after_shop_loop_item', '__return_false');
    
    // 16a) HIDE BILLING DETAILS FROM CHECKOUT IF ONLY VIRTUAL PRODUCTS 
    add_filter( 'woocommerce_checkout_fields' , 'avs_simplify_checkout_virtual' );
    function avs_simplify_checkout_virtual( $fields ) {
       $only_virtual = true;
       foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
          // Check if there are non-virtual products
          if ( ! $cart_item['data']->is_virtual() ) $only_virtual = false;   
       }
        if( $only_virtual ) {
           unset($fields['billing']['billing_company']);
           unset($fields['billing']['billing_address_1']);
           unset($fields['billing']['billing_address_2']);
           unset($fields['billing']['billing_city']);
           unset($fields['billing']['billing_postcode']);
           unset($fields['billing']['billing_country']);
           unset($fields['billing']['billing_state']);
           unset($fields['billing']['billing_phone']);
           add_filter( 'woocommerce_enable_order_notes_field', '__return_false' );
         }
         return $fields;
    }
    
    // 16b) HIDE BILLING DETAILS FROM CHECKOUT IF SHIPPING METHOD IS LOCALPICKUP
    add_filter('woocommerce_checkout_fields', 'avs_remove_billing_checkout_fields');
    function avs_remove_billing_checkout_fields($fields) {
    	$shipping_method ='local_pickup:2'; // Value of the applicable shipping method.
    	global $woocommerce;
    		$chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    		$chosen_shipping = $chosen_methods[0];
    	if ($chosen_shipping == $shipping_method) {
    		unset($fields['billing']['billing_address_1']); // Value of field name to be hidden
    		unset($fields['billing']['billing_address_2']);
    	    unset($fields['billing']['billing_city']);
            unset($fields['billing']['billing_postcode']);
            unset($fields['billing']['billing_country']);
            unset($fields['billing']['billing_state']);
            unset($fields['billing']['billing_phone']);
    	}
    	return $fields;
    }
    
    // 17) DISPLAY original/coupon-discounted SUBTOTAL IN CHECKOUT TABLE
    add_filter( 'woocommerce_cart_subtotal', 'avs_slash_cart_subtotal_if_discount', 9999, 3 );
    function avs_slash_cart_subtotal_if_discount( $cart_subtotal, $compound, $cart ){
       if ( $cart->get_cart_discount_total() > 0 ) {
          return wc_format_sale_price( $cart->get_subtotal(), $cart->get_subtotal() - $cart->get_cart_discount_total() );
       }
       return $cart_subtotal;
    }
    
    // 18) PRODUCT IMAGES AT CHECKOUT and ORDER PAGE
    //add_filter( 'woocommerce_cart_item_name', 'avs_product_image_review_order_checkout', 9999, 3 );
    //function avs_product_image_review_order_checkout( $name, $cart_item, $cart_item_key ) {
    //    if ( ! is_checkout() ) return $name;
    //    $product = $cart_item['data'];
    //    $thumbnail = $product->get_image( array( '50', '50' ), array( 'class' => 'alignleft' ) );
    //    return $thumbnail . $name;
    //}
    
    add_filter( 'woocommerce_cart_item_name', 'avs_product_image_on_checkout', 10, 3 );
    function avs_product_image_on_checkout( $name, $cart_item, $cart_item_key ) {  
    /* Return if not checkout page */
    if ( ! is_checkout() ) {return $name;}
    /* Get product object */
    $_product = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
    /* Get product thumbnail */
    $thumbnail = $_product->get_image();
    /* Add wrapper to image and add some css */
    $image = '<div style="width:52px;padding:0 7px 7px 0;display:inline-block;vertical-align:top;">' . $thumbnail . '</div>';
    /* Prepend image to name and return it */
    return $image . $name;
    }
    
    add_filter( 'woocommerce_order_item_name', 'avs_product_image_on_order_pay', 10, 3 );
    function avs_product_image_on_order_pay( $name, $item, $extra ) {
    /* Return if not checkout page */
    if ( ! is_checkout() ) {return $name;}
    $product_id = $item->get_product_id();
    /* Get product object */
    $_product = wc_get_product( $product_id );
    /* Get product thumbnail */
    $thumbnail = $_product->get_image();
    /* Add wrapper to image and add some css */
    $image = '<div style="width:52px;padding:0 7px 7px 0;display:inline-block;vertical-align:top;">' . $thumbnail . '</div>';
    /* Prepend image to name and return it */
    return $image . $name;
    }
    
     add_filter( 'woocommerce_checkout_product_title', 'my_add_order_review_product_image', 10, 2 );
    // XX) PLACEHOLDERS IN CHECKOUT FORMS
    //add_filter( 'woocommerce_checkout_fields', 'avs_labels_inside_checkout_fields', 9999 );
    //function avs_labels_inside_checkout_fields( $fields ) {
    //   foreach ( $fields as $section => $section_fields ) {
    //      foreach ( $section_fields as $section_field => $section_field_settings ) {
    //         $fields[$section][$section_field]['placeholder'] = $fields[$section][$section_field]['label'];
    //         $fields[$section][$section_field]['label'] = '';
    //      }
    //   }
    //   return $fields;
    //}
    
    
    
    //** USER MY-ACCOUNT PAGE **// 
    
    // 19) PRINT CUSTOMER AVATAR IN MY-ACCOUNT PAGE
     function avs_myaccount_customer_avatar() {
         $current_user = wp_get_current_user();
         echo '<div class="myaccount_avatar">' . get_avatar( $current_user->user_email, 72, '', $current_user->display_name ) . '</div>';
     }
     add_action( 'woocommerce_account_content', 'avs_myaccount_customer_avatar', 5 );
    
    // 20) DISPLAY ALL PRODUCTS PURCHASED BY USER INTO HIS MY-ACCOUNT PAGE
    add_shortcode( 'my_purchased_products', 'avs_products_bought_by_curr_user' );
    function avs_products_bought_by_curr_user() {
        // get current user
        $current_user = wp_get_current_user();
        if ( 0 == $current_user->ID ) return;
        // get user orders (completed + processing)
        $customer_orders = get_posts( array(
            'numberposts' => -1,
            'meta_key'    => '_customer_user',
            'meta_value'  => $current_user->ID,
            'post_type'   => wc_get_order_types(),
            'post_status' => array_keys( wc_get_is_paid_statuses() ),
        ) );
        // loop through orders and get products IDs
        if ( ! $customer_orders ) return;
        $product_ids = array();
        foreach ( $customer_orders as $customer_order ) {
            $order = wc_get_order( $customer_order->ID );
            $items = $order->get_items();
            foreach ( $items as $item ) {
                $product_id = $item->get_product_id();
                $product_ids[] = $product_id;
            }
        }
        $product_ids = array_unique( $product_ids );
        $product_ids_str = implode( ",", $product_ids );
        // pass product IDs to products shortcode
        return do_shortcode("[products ids='$product_ids_str']");
    }
    
    // 21) DISPLAY USER'S WISHLIST INTO HIS MY-ACCOUNT PAGE
    // 1. Register new endpoint (URL) for My Account page  (Note: Re-save Permalinks or it will give 404 error, the flush rule makes unnecessary to save permalinks)
    //function avs_add_wishlist_endpoint() {
    //    add_rewrite_endpoint( 'wishlist', EP_ROOT | EP_PAGES );
    //	flush_rewrite_rules();
    //}
    //add_action( 'init', 'avs_add_wishlist_endpoint' );
    // 2. Add new query var  
    //function avs_wishlist_query_vars( $vars ) {
    //    $vars[] = 'wishlist';
    //    return $vars;
    //}
    //add_filter( 'query_vars', 'avs_wishlist_query_vars', 0 );
    // 3. Insert the new endpoint into the My Account menu
    //function avs_add_wishlist_link_my_account( $items ) {
    //    $items['wishlist'] = 'DESIDERATI';
    //    return $items;
    //}
    //add_filter( 'woocommerce_account_menu_items', 'avs_add_wishlist_link_my_account' );
    // 4. Add content to the new tab  (Note: add_action must follow 'woocommerce_account_{your-endpoint-slug}_endpoint' format)
    //function avs_wishlist_content() {
    //   echo '<p>Questa è la lista degli acquisti che desiderate fare in un secondo momento... ma attenti a non far scappare le offerte!</p>';
    //   echo do_shortcode( ' [woosw_list] ' );
    //}
    //add_action( 'woocommerce_account_wishlist_endpoint', 'avs_wishlist_content' );
    Plugin Author Shea Bunge

    (@bungeshea)

    Hi @devsaredead,

    The issue is with this line here:

    add_filter( 'loop_shop_per_page', create_function( '$products', 'return 9;' ), 30 );

    Replacing it with this instead should solve the issue:

    add_filter( 'loop_shop_per_page', function ( $products ) { return 9; }, 30 );
    Thread Starter devsaredead

    (@devsaredead)

    Thank you, I have now ruled out the error related to this plugin. Now I’ll try to fix the other ones. ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘error when upgrading PHP 8.0’ is closed to new replies.