Wishlist - How to create functions "Wedding List" with basic operation with wishlist plugin
The logic behind the code is as follows
1. a user visits a wish not his (not owner)
2. the code registers a cookie with the id of the wishlist
3. the user buys a product on the site
4. the id of the wish list is stored in the order information
5. the cookie is cleared
6. the order goes to completad
7. the system automatically retrieves the wishlist whose id is stored with the order, the flows in the case also find them purchased item and, in my wishlist, removes them from wishlist
Try to add the following code in the functions.php file of your theme, or child to enable such behavior:
if( defined( 'YITH_WCWL' ) ){
if( ! function_exists( 'yith_wcwl_set_visited_wishlist_cookie' ) ) {
function yith_wcwl_set_visited_wishlist_cookie() {
$wishlist = YITH_WCWL_Wishlist_Factory::get_current_wishlist();
if( ! $wishlist ){
return;
}
if( $wishlist->is_current_user_owner() ){
return;
}
wc_setcookie( 'yith_wcwl_visited_wishlist', $wishlist->get_id(), time() + apply_filters( 'yith_wcwl_cookie_expiration', 60 * 60 * 24 * 30 ), false );
}
add_action( 'template_redirect', 'yith_wcwl_set_visited_wishlist_cookie', 10 );
}
if( ! function_exists( 'yith_wcwl_set_visited_wishlist_order_meta' ) ){
function yith_wcwl_set_visited_wishlist_order_meta( $order_id ){
$visited_wishlist = $_COOKIE['yith_wcwl_visited_wishlist'];
update_post_meta( $order_id, '_yith_wcwl_visited_wishlist', $visited_wishlist );
wc_setcookie( 'yith_wcwl_visited_wishlist', '', time() - 3600 , false );
}
add_action( 'woocommerce_checkout_update_order_meta', 'yith_wcwl_set_visited_wishlist_order_meta', 10, 1 );
}
if( ! function_exists( 'yith_wcwl_delete_wishlist_item' ) ){
function yith_wcwl_delete_wishlist_item( $order_id ){
$visited_wishlist = get_post_meta( $order_id, '_yith_wcwl_visited_wishlist', true);
if( ! empty( $visited_wishlist ) ) {
$wishlist = yith_wcwl_get_wishlist( $visited_wishlist );
$order = wc_get_order( $order_id );
if( $wishlist && $order ){
$items = $order->get_items();
if( ! empty( $items ) ){
foreach ( $items as $item ){
$prod_id = $item['product_id'];
$wishlist->remove_product( $prod_id );
}
$wishlist->save();
}
}
}
delete_post_meta( $order_id, '_yith_wcwl_visited_wishlist' );
}
add_action( 'woocommerce_order_status_completed', 'yith_wcwl_delete_wishlist_item', 10, 1 );
}
}