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() {
$action_params = get_query_var( 'wishlist-action', false );
$action_params = explode( '/', apply_filters( 'yith_wcwl_current_wishlist_view_params', $action_params ) );
$action = ( isset( $action_params[0] ) ) ? $action_params[0] : 'view';
$available_views = apply_filters( 'yith_wcwl_available_wishlist_views', array( 'view', 'user' ) );

if(
empty( $action ) ||
( ! empty( $action ) && ( $action == 'view' || $action == 'user' ) ) ||
( ! empty( $action ) && ( $action == 'manage' || $action == 'create' ) && get_option( 'yith_wcwl_multi_wishlist_enable', false ) != 'yes' ) ||
( ! empty( $action ) && ! in_array( $action, $available_views ) ) ||
! empty( $user_id )
) {
if ( ! empty( $action ) && $action == 'user' ) {
$user_id = isset( $action_params[1] ) ? $action_params[1] : false;
$user_id = ( ! $user_id ) ? get_query_var( $user_id, false ) : $user_id;
$user_id = ( ! $user_id ) ? get_current_user_id() : $user_id;

$wishlists = YITH_WCWL()->get_wishlists( array( 'user_id' => $user_id, 'is_default' => 1 ) );

if ( ! empty( $wishlists ) && isset( $wishlists[0] ) ) {
$wishlist_id = $wishlists[0]['wishlist_token'];
} else {
$wishlist_id = false;
}
} else {
$wishlist_id = isset( $action_params[1] ) ? $action_params[1] : false;
$wishlist_id = ( ! $wishlist_id ) ? get_query_var( 'wishlist_id', false ) : $wishlist_id;
}

$is_user_owner = false;

if( ! empty( $user_id ) ){
if( get_current_user_id() == $user_id ){
$is_user_owner = true;
}
}
elseif( ! is_user_logged_in() ){
if( empty( $wishlist_id ) ){
$is_user_owner = true;
}
else{
$is_user_owner = false;
}
}
else{
if( empty( $wishlist_id ) ){
$is_user_owner = true;
}
else{
$wishlist = YITH_WCWL()->get_wishlist_detail_by_token( $wishlist_id );
$is_user_owner = $wishlist['user_id'] == get_current_user_id();
}
}

if( ! $is_user_owner ) {
wc_setcookie( 'yith_wcwl_visited_wishlist', $wishlist_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_detail_by_token( $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'];

YITH_WCWL()->details['remove_from_wishlist'] = $prod_id;
YITH_WCWL()->details['wishlist_id'] = $visited_wishlist;
YITH_WCWL()->details['user_id'] = $wishlist['user_id'];

YITH_WCWL()->remove();
}
}
}
}

delete_post_meta( $order_id, '_yith_wcwl_visited_wishlist' );
}
add_action( 'woocommerce_order_status_completed', 'yith_wcwl_delete_wishlist_item', 10, 1 );
}
}
Was this article helpful?
1 out of 1 found this helpful

Back to Help Center

Powered by Zendesk