0

I hope you will be able to assist me with this.

On our woocommerce store we have this custom functions.php code to display recently viewed products on single product page, but the Apache Error Log shows multiple errors.

Functions.php code:

add_shortcode( 'recently_viewed_products', 'bbloomer_recently_viewed_shortcode' );
 
function bbloomer_recently_viewed_shortcode() {
 
   $viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', wp_unslash( $_COOKIE['woocommerce_recently_viewed'] ) ) : array();
   $viewed_products = array_slice($viewed_products, 0, 6);

 
   if ( empty( $viewed_products ) ) return;
    
   $title = '<h5 class="product-section-title container-width product-section-title-related pt-half pb-half uppercase">RECENTLY VIEWED</h5>';
   $product_ids = implode( ",", $viewed_products );
 
   return $title . do_shortcode("[products ids='$product_ids']");
   
}

// adds notice at single product page above add to cart
add_action( 'woocommerce_after_single_product', 'recviproducts', 31 );
function recviproducts() {
    echo do_shortcode ('[recently_viewed_products]');
}

function custom_track_product_view() {
    if ( ! is_singular( 'product' ) ) {
        return;
    }

    global $post;

    if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) )
        $viewed_products = array_slice($viewed_products, 0, 6);
    else
        $viewed_products = (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] );

    if ( ! in_array( $post->ID, $viewed_products ) ) {
        $viewed_products[] = $post->ID;
    }

    if ( sizeof( $viewed_products ) > 6 ) {
        array_shift( $viewed_products );
    }

    // Store for session only
    wc_setcookie( 'woocommerce_recently_viewed', implode( '|', $viewed_products ) );
}

Apache Error Log:

[proxy_fcgi:error] [pid 21983:tid 139686479484672] [client 54.236.1.13:0] AH01071: Got error 'PHP message: PHP Warning: array_slice() expects parameter 1 to be array, null given in /home/662214.cloudwaysapps.com/chy............mizer-child-theme/functions.php on line 428PHP message: PHP Warning: in_array() expects parameter 2 to be array, null given in /home/662214.cloudwaysapps.com/chyneruzzn/public_html/wp-content/themes/shoptimizer-child-theme/functions.php on line 432'

I think I need to add something like '->toArray();' but I am not sure as I saw it on another post.

Line 428 is:

$viewed_products = array_slice($viewed_products, 0, 6);

Line 432 is:

if ( ! in_array( $post->ID, $viewed_products ) ) {

Thank you so much in advance!

I believe I should change the functions.php code so that we prevent the error. Possibly with ->toArray(); but I don't know how to reference and type it.

4
  • Please edit the question so the Apace log is plain text instead of a code block... it's uncomfortable trying to horizontallyscroll throug that long message. It seems to say the error is on line 428 of functions.php but the code you have given us doesn't seem to be that long. Can you please highlight where line 428 is? Commented Mar 5, 2023 at 14:08
  • 1
    In your function custom_track_product_view(), $viewed_products is not declared. Commented Mar 5, 2023 at 14:18
  • 1
    get a good IDE it would have given you an error hint itself. Commented Mar 5, 2023 at 14:21
  • Hi @AndyPreston Thanks! I edited it, apologies if it wasn't clear enough. Line 428 is: $viewed_products = array_slice($viewed_products, 0, 6); Line 432 is: if ( ! in_array( $post->ID, $viewed_products ) ) { I hope this helps better, thank you in advance! Commented Mar 5, 2023 at 14:22

2 Answers 2

2

Considering how this piece of code is working, in your function custom_track_product_view(), you should replace

if ( empty( $_COOKIE['woocommerce_recently_viewed'] ) )
    $viewed_products = array_slice($viewed_products, 0, 6);
else
    $viewed_products = (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] );

by this:

$viewed_products = !empty($_COOKIE['woocommerce_recently_viewed']) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed']) : [];
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much! I will test it right away and leave a feedback as soon as possible.
There is no benefit in explicitly casting the return value from explode() as an (array) -- that is the only type of data that explode() returns.
Absolutly, @mickmackusa, I just used the available cocde of from example.
It is okay for questions to contain mistakes; answers should endeavor to have no mistakes.
It depends on what you consider to be a mistake.
1

In your function custom_track_product_view() you are using array_slice($viewed_products, 0, 6); but at that point $viewed_products hasn't yet been assigned any value at all. You can't take a slice of an array that doesn't exist.

So if at any time this function is called and the cookie woocommerce_recently_viewed hasn't been set, the code needs some other source to obtain the list of viewed products from, or you could simply abandon any attempt as using them and setting $viewed_products to an empty array.

2 Comments

Now it makes perfect sense! Yes, I do not want to set other source to obtain so it is best to abandon the first attempt (first product page visit). Could you give me an idea how to change the code for this? I am not sure if the above code change from Lagaart is doing that. Thank you in advance for all your help.
It does look as though @Lagaart and I have given you two parts of the same answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.