ショートコードを生成した最近閲覧したスクリプトを作成しました。これを私のホームページに挿入しました。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の場合にCookieを設定する必要があります。 –