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.
functions.phpbut the code you have given us doesn't seem to be that long. Can you please highlight where line 428 is?