0

I need to share wishlist using Graphql in Magento2.4.

Anyone could assist me in this?

1 Answer 1

0
  1. Create module SharingWishlist sequence Magento_WishlistGraphQl
  2. Define schema in etc/schema.graphqls
type Query {
    sharingWishlist(sharing_code: String!) : Wishlist! @resolver(class: "Vendor\\SharingWishlist\\Model\\Resolver\\SharingWishlist")
}
  1. Create resolver Vendor\SharingWishlist\Model\Resolver\SharingWishlist
<?php

/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Vendor\SharingWishlist\Model\Resolver;

use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Wishlist\Model\WishlistFactory;

/**
* Resolver for Item
*/
class SharingWishlist implements ResolverInterface
{
   /**
    * @var WishlistFactory
    */
   protected WishlistFactory $wishlistFactory;

   /**
    * SharingWishlist constructor.
    * @param WishlistFactory $wishlistFactory
    */
   public function __construct(
       WishlistFactory $wishlistFactory
   )
   {
       $this->wishlistFactory = $wishlistFactory;
   }

   /**
    * @inheritdoc
    */
   public function resolve(
       Field $field,
       $context,
       ResolveInfo $info,
       array $value = null,
       array $args = null
   )
   {
       if (!isset($args['sharing_code'])) {
           throw new LocalizedException(__('Missing key "code" in Wishlist value data'));
       }

       $code = $args['sharing_code'];

       $wishlistFactoryCreate = $this->wishlistFactory->create();

       $wishlist = $wishlistFactoryCreate->loadByCode($code);

       $wishlist->setId($wishlist->getOrigData('wishlist_id'));

       return [
           'id' => (string)$wishlist->getId(),
           'sharing_code' => $wishlist->getSharingCode(),
           'updated_at' => $wishlist->getUpdatedAt(),
           'items_count' => $wishlist->getItemsCount(),
           'model' => $wishlist
       ];
   }
}
  • That's all you can get wishlist by graphql
  query sharingWishlist($sharing_code: String = "FrywWTpFihhWMHgO9u4dmUezfMStIFMm"){
    sharingWishlist(sharing_code: $sharing_code) {
      items_count
      sharing_code
      updated_at
      items_v2 {
        items {
          id
          quantity
          description
          added_at
          product {
          __typename
          uid
          sku
          name
          ... on SimpleProduct {
            image {
              url
              label
            }
            image_hover
          }
          special_from_date
          special_to_date
          created_at
          rating_summary
          stock_status
          categories{
            uid
          }
          only_x_left_in_stock
          thumbnail {
            url
            position
            disabled
            label
          }
          url_key
          url_rewrites {
            url
          }
          price_range {
            maximum_price {
              final_price {
                currency
                value
              }
              regular_price {
                currency
                value
              }
            }
            minimum_price {
              final_price {
                currency
                value
              }
              regular_price {
                currency
                value
              }
            }
          }
          ... on ConfigurableProduct {
            variants {
              product {
                color
                size
                image_hover
                image {
                  url
                  label
                }
                uid
                sku
              }
            }
            configurable_options {
              id
              uid
              attribute_id
              attribute_id_v2
              attribute_uid
              attribute_code
              label
              position
              use_default
              values {
                value_index
                uid
                label
                default_label
                store_label
                use_default_value
                swatch_data {
                  value
                }
              }
              product_id
            }
          }
          ... on GroupedProduct {
            items {
              product {
                sku
              }
            }
          }
        }
          ... on ConfigurableWishlistItem {
            configurable_options {
              configurable_product_option_uid
              configurable_product_option_value_uid
            }
          }
        }
        page_info {
          page_size
          current_page
        }
      }
    }
  }

For frontent I was create a route like /customer/:sharing_code Then I get sharing_code param and send graphQl request In case vuestorefront

        const graphqlQuery = {
          "operationName": "loadShareWishlist",
          "query": loadShareWishlist,
          "variables": {
            ...route.value.params
          }
        };

        const sharingWishlistData = (await $vsf.$magento.client.post($vsf.$magento.config.magentoApiEndpoint, graphqlQuery)).data;

        const {sharingWishlist} = sharingWishlistData.data;

        wishlist.value = sharingWishlist as Wishlist | Object;

So you need just make a button to copy link like customer/: sharing_code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.