0
votes

I am trying show recently viewed products of Woocommerce using Elementor's posts widget.

I am trying to create a elementor custom query from https://developers.elementor.com/custom-query-filter/ and https://www.wpexplorer.com/woocommerce-recently-viewed-products-shortcode/.

add_action( 'elementor/query/customquery', function( $query ) {

// Get WooCommerce Global
global $woocommerce;

// Get recently viewed product cookies data
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array();
$viewed_products = array_filter( array_map( 'absint', $viewed_products ) );

// If no data, quit
if ( empty( $viewed_products ) )
    return __( 'You have not viewed any product yaml_emit(data)!', 'rc_wc_rvp' );

// Create query arguments array
$query_args = array(
    'post_type'      => 'product',
    'post__in'       => $viewed_products, 
    'orderby'        => 'rand'
);

// Add meta_query to query args
$query_args['meta_query'] = array();

$query->set( 'meta_query', $query_args );
});

But so far no luck. Can anyone tell me if this will be possible to do and if possible, then what I am doing wrong?

Thanks in advance Julash

1
Have you logged inside the function to be sure its even called?Mr. Jo

1 Answers

0
votes

You need to set individual params to $query->set. check the below code.

add_action( 'elementor/query/customquery', function( $query ) {

    // Get WooCommerce Global
    global $woocommerce;

    // Get recently viewed product cookies data
    $viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array();
    $viewed_products = array_filter( array_map( 'absint', $viewed_products ) );

    // If no data, quit
    if ( empty( $viewed_products ) )
        return __( 'You have not viewed any product yaml_emit(data)!', 'rc_wc_rvp' );

    $query->set( 'post_type', 'product' );
    $query->set( 'post__in', $viewed_products );
    $query->set( 'orderby', 'rand' );

});