Php 扩展不带插件的愿望列表以使用WooCommerce产品
我已经尝试了很多插件,但没有一个是适合我的。我终于找到了一个解决方案,如何在没有插件的情况下将帖子添加到收藏夹,它适用于帖子类型“post”,但我也需要woocommerce产品 我尝试将$args从'post\u type'=>'post'更改为'post\u type'=>'post,product'或'post\u type'=>'product',但它不会将产品添加到收藏夹页面。仍然只有新增的帖子 这是一个来源,但有些单词是西里尔语,所以我会翻译 1)向functions.php添加3个函数Php 扩展不带插件的愿望列表以使用WooCommerce产品,php,ajax,wordpress,woocommerce,custom-wordpress-pages,Php,Ajax,Wordpress,Woocommerce,Custom Wordpress Pages,我已经尝试了很多插件,但没有一个是适合我的。我终于找到了一个解决方案,如何在没有插件的情况下将帖子添加到收藏夹,它适用于帖子类型“post”,但我也需要woocommerce产品 我尝试将$args从'post\u type'=>'post'更改为'post\u type'=>'post,product'或'post\u type'=>'product',但它不会将产品添加到收藏夹页面。仍然只有新增的帖子 这是一个来源,但有些单词是西里尔语,所以我会翻译 1)向functions.php添加3
//favorite posts array
function favorite_id_array() {
if (!empty( $_COOKIE['favorite_post_ids'])) {
return explode(',', $_COOKIE['favorite_post_ids']);
}
else {
return array();
}
}
//add to favorite function
function add_favorite() {
$post_id = (int)$_POST['post_id'];
if (!empty($post_id)) {
$new_post_id = array(
$post_id
);
$post_ids = array_merge($new_post_id, favorite_id_array());
$post_ids = array_diff($post_ids, array(
''
));
$post_ids = array_unique($post_ids);
setcookie('favorite_post_ids', implode(',', $post_ids) , time() + 3600 * 24 * 365, '/');
echo count($post_ids);
}
die();
}
add_action('wp_ajax_favorite', 'add_favorite');
add_action('wp_ajax_nopriv_favorite', 'add_favorite');
//delete from favorite function
function delete_favorite() {
$post_id = (int)$_POST['post_id'];
if (!empty($post_id)) {
$favorite_id_array = favorite_id_array();
if (($delete_post_id = array_search($post_id, $favorite_id_array)) !== false) {
unset($favorite_id_array[$delete_post_id]);
}
setcookie('favorite_post_ids', implode(',', $favorite_id_array) , time() + 3600 * 24 * 30, '/');
echo count($favorite_id_array);
}
die();
}
add_action('wp_ajax_delfavorite', 'delete_favorite');
add_action('wp_ajax_nopriv_delfavorite', 'delete_favorite');
<?php if(in_array($post->ID, favorite_id_array())){ ?>
<div class="fv_<?php echo $post->ID; ?>" title="Already in favorite" ><img src="http://yoursite.com/path-to-your-icon.svg" ><a href="http://yoursite.com/favorite/">In favorite</a></div>
<?php } else { ?>
<div class="fv_<?php echo $post->ID; ?>" >
<div class="add-favorite" title="Add to favorite" data-post_id="<?php echo $post->ID; ?>">
<img src="http://yoursite.com/path-to-your-icon.svg">Add to favorite
</div>
</div>
<?php } ?>
2)将“添加到收藏夹”链接添加到模板。对于帖子,它是single.php
//favorite posts array
function favorite_id_array() {
if (!empty( $_COOKIE['favorite_post_ids'])) {
return explode(',', $_COOKIE['favorite_post_ids']);
}
else {
return array();
}
}
//add to favorite function
function add_favorite() {
$post_id = (int)$_POST['post_id'];
if (!empty($post_id)) {
$new_post_id = array(
$post_id
);
$post_ids = array_merge($new_post_id, favorite_id_array());
$post_ids = array_diff($post_ids, array(
''
));
$post_ids = array_unique($post_ids);
setcookie('favorite_post_ids', implode(',', $post_ids) , time() + 3600 * 24 * 365, '/');
echo count($post_ids);
}
die();
}
add_action('wp_ajax_favorite', 'add_favorite');
add_action('wp_ajax_nopriv_favorite', 'add_favorite');
//delete from favorite function
function delete_favorite() {
$post_id = (int)$_POST['post_id'];
if (!empty($post_id)) {
$favorite_id_array = favorite_id_array();
if (($delete_post_id = array_search($post_id, $favorite_id_array)) !== false) {
unset($favorite_id_array[$delete_post_id]);
}
setcookie('favorite_post_ids', implode(',', $favorite_id_array) , time() + 3600 * 24 * 30, '/');
echo count($favorite_id_array);
}
die();
}
add_action('wp_ajax_delfavorite', 'delete_favorite');
add_action('wp_ajax_nopriv_delfavorite', 'delete_favorite');
<?php if(in_array($post->ID, favorite_id_array())){ ?>
<div class="fv_<?php echo $post->ID; ?>" title="Already in favorite" ><img src="http://yoursite.com/path-to-your-icon.svg" ><a href="http://yoursite.com/favorite/">In favorite</a></div>
<?php } else { ?>
<div class="fv_<?php echo $post->ID; ?>" >
<div class="add-favorite" title="Add to favorite" data-post_id="<?php echo $post->ID; ?>">
<img src="http://yoursite.com/path-to-your-icon.svg">Add to favorite
</div>
</div>
<?php } ?>
<?php
/*
Template Name: favorite
*/
?>
<?php
$favorite_id_array = favorite_id_array();
global $wp_query;
$save_wpq = $wp_query;
$args = array(
'paged' => get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1,
'post_type' => 'post',
'post__in' => !empty($favorite_id_array) ? $favorite_id_array : array(0),
);
$wp_query = new WP_Query( $args );
?>
<?php if ($wp_query->have_posts()) : ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<div class="fv_<?php echo $post->ID; ?>">
<div class="delete-favorite" data-post_id="<?php echo $post->ID; ?>" title="Delete from favorite">
<img src="http://yoursite.com/link-to-your-icon.svg" >Delete
</div>
</div>
<a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
<?php endwhile; ?>
<?php else : ?>
<p>No posts in your favorite</p>
<?php endif; ?>
<?php wp_reset_postdata(); ?>
your pagination code there.