skip to Main Content

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.

2

Answers


  1. 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']) : [];
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search