나는 내 홈 페이지에 삽입 한 단축 코드를 생성 한 최근 본 스크립트를 만들었습니다.Woocommerce 최근에 본 상품
이 스크립트는 내 웹 사이트를 방문하고 떠난 사람들이 다시 방문하면 즉시 마지막으로 방문한 제품을 볼 수 있도록 설계되었습니다.
나는 단축 코드 [woocommerce_recently_viewed_products]
을 배치 한 다음과 같은 스크립트를 사용하여 단축 코드를 생성 한 :
function rc_woocommerce_recently_viewed_products($atts, $content = null) {
// Get shortcode parameters
extract(shortcode_atts(array(
"per_page" => '5'
), $atts));
// 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 yet!', 'rc_wc_rvp');
// Create the object
ob_start();
wc_setcookie('woocommerce_recently_viewed', implode('|', $viewed_products));
}
// Get products per page
if(!isset($per_page) ? $number = 4 : $number = $per_page)
// Create query arguments array
$query_args = array(
'posts_per_page' => $number,
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'post__in' => $viewed_products,
'orderby' => 'rand'
);
// Add meta_query to query args
$query_args['meta_query'] = array();
// Check products stock status
$query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();
// Create a new query
$r = new WP_Query($query_args);
// If query return results
if ($r->have_posts()) {
$content = '<ul class="rc_wc_rvp_product_list_widget">';
// Start the loop
while ($r->have_posts()) {
$r->the_post();
global $product;
$content .= '<li>
<a href="' . get_permalink() . '">
' . (has_post_thumbnail() ? get_the_post_thumbnail($r->post->ID, 'shop_thumbnail') : woocommerce_placeholder_img('shop_thumbnail')) . ' ' . get_the_title() . '
</a> ' . $product->get_price_html() . '
</li>';
}
$content .= '</ul>';
}
// Get clean object
$content .= ob_get_clean();
// Return whole content
return $content;
}
// Register the shortcode
add_shortcode("woocommerce_recently_viewed_products",
"rc_woocommerce_recently_viewed_products");
모든 등록 된 것으로 보인다. 그러나, 나 자신을 테스트 할 때. 몇 가지 제품을보고 단축 코드가 등록 된 홈페이지로 돌아가서 텍스트를 봅니다
아직 제품을 보지 않았습니다!
내가 또는 잠재 고객이 보았을 수도있는 제품을 등록하고 표시하려면 무엇이 누락되었는지 알 수 없습니다.
쿠키를 설정 한 코드가 누락되었습니다 ... 질문을 편집하여 추가 할 수 있습니까? – LoicTheAztec
@LoicTheAztec 나는 세트 쿠키를 놓친다. 추가를 시도했지만 동일한 결과를 얻었습니다. 질문이 업데이트되었습니다. – PaulMcF87
is_product() 조건이 true 일 때 쿠키를 설정해야합니다. –