def get_category_by_name(self, category_name): """get_product_by_sku. Parameters ---------- category_name : str category name to search for. Returns ------- id : ID! ID of the category with the matching name. """ variables = {"search": category_name} query = """ query categories($search: String!) { categories(first: 100, filter: {search: $search}) { edges { node { id name } } } } """ response = graphql_request(query, variables, self.headers, self.endpoint_url) return self.get_category_by_name_helper(response["data"]["categories"], category_name)
def get_product_by_sku(self, product_sku): """get_product_by_sku. Parameters ---------- product_sku : str product sku to search for. Returns ------- id : ID! ID of the product with the matching sku. """ variables = {"search": product_sku} query = """ query products($search: String!) { products(first: 100, filter: {search: $search}) { edges { node { id variants { sku } } } } } """ response = graphql_request(query, variables, self.headers, self.endpoint_url) return self.get_matching_sku_helper(response["data"]["products"], product_sku)
def update_product(self, product_id, product): """update_product. Parameters ---------- product_id : str product id required to query the product. product : Product product with fields to update to Returns ------- product : dict updates the product object. """ # define updated project obj from product to update from data updated_product = { "category": product["category"], "chargeTaxes": product["chargeTaxes"], "descriptionJson": product["descriptionJson"], "isPublished": product["isPublished"], "name": product["name"], "basePrice": product["basePrice"], "taxCode": "", "seo": { "title": product["seo"]["title"], "description": product["seo"]["description"] } } variables = {"id": product_id, "input": updated_product} # * Definition: product(id: ID, input: Product): Product query = """ mutation productUpdate($id: ID!, $input: ProductInput!) { productUpdate(id: $id, input: $input) { product { id name } productErrors { field message code } } } """ response = graphql_request(query, variables, self.headers, self.endpoint_url) errors = response["data"]["productUpdate"]["productErrors"] handle_errors(errors) return response["data"]["productUpdate"]["product"][ "name"] + " was updated."
def query_all_categories(self): """create a category Parameters ---------- None Returns ------- list : list list of all the categories in the database. Raises ------ Exception when productErrors is not an empty list. """ variables = {} query = """ query categories { categories(first: 100) { edges { node { name id children(first: 100){ edges { node { name id } } } ancestors(first: 100){ edges { node { name id } } } } } } } """ response = graphql_request(query, variables, self.headers, self.endpoint_url) # Return edges of all categories return self.get_parent_categories( response["data"]["categories"]["edges"])
def category_create(self, name, parent_id=None): """create a category Parameters ---------- **kwargs : dict, optional overrides the default value set to create the category refer to the productTypeCreateInput graphQL type to know what can be overriden. Returns ------- id : str the id of the productType created. Raises ------ Exception when productErrors is not an empty list. """ category = {"name": name} variables = { "input": category, } if parent_id is not None: variables["parent"] = parent_id query = """ mutation createCategory($input: CategoryInput!, $parent: ID) { categoryCreate(input: $input, parent: $parent) { category { id } productErrors { field message code } } } """ response = graphql_request(query, variables, self.headers, self.endpoint_url) errors = response["data"]["categoryCreate"]["productErrors"] handle_errors(errors) return response["data"]["categoryCreate"]["category"]["id"]
def get_category_children(self, name): """get_product_by_sku. Parameters ---------- category_name : str category name to search for. Returns ------- list : List! List of child categories of category. """ variables = {"search": name} query = """ query categories($search: String!) { categories(first: 100, filter: {search: $search}) { edges { node { id name children(first: 100) { edges { node { name id children(first: 100) { edges { node { name id } } } } } } } } } } """ response = graphql_request(query, variables, self.headers, self.endpoint_url) return self.get_category_children_helper( response["data"]["categories"], name)
def get_all_product_ids(self): variables = {} query = """ query products { products(first: 100) { edges { node { id } } } } """ response = graphql_request(query, variables, self.headers, self.endpoint_url) return response["data"]["products"]["edges"]
def purge_products(self): edges = self.get_all_product_ids() ids = [] for node in edges: ids.append(node["node"]["id"]) variables = {"ids": ids} query = """ mutation productBulkDelete($ids: [ID]!) { productBulkDelete(ids: $ids) { count } } """ response = graphql_request(query, variables, self.headers, self.endpoint_url) return None
def get_product(self, product_id): """get_product. Parameters ---------- product_id : str product id required to query the product. Returns ------- product : dict the product object. """ variables = {"id": product_id} # * Definition: product(id: ID, slug: String): Product query = """ fragment TaxedMoneyFields on TaxedMoney { currency gross { amount localized } net { amount localized } tax { amount localized } } fragment TaxedMoneyRangeFields on TaxedMoneyRange { start { ...TaxedMoneyFields } stop { ...TaxedMoneyFields } } fragment ProductPricingFields on ProductPricingInfo { onSale discount { ...TaxedMoneyFields } discountLocalCurrency { ...TaxedMoneyFields } priceRange { ...TaxedMoneyRangeFields } priceRangeUndiscounted { ...TaxedMoneyRangeFields } priceRangeLocalCurrency { ...TaxedMoneyRangeFields } } fragment ProductVariantFields on ProductVariant { id sku name stockQuantity isAvailable pricing { discountLocalCurrency { ...TaxedMoneyFields } price { currency gross { amount localized } } priceUndiscounted { currency gross { amount localized } } priceLocalCurrency { currency gross { amount localized } } } attributes { attribute { id name } values { id name value: name } } } query get_product($id: ID!) { product(id: $id) { id seoTitle seoDescription name description descriptionJson publicationDate isPublished productType { id name } slug category { id name } updatedAt chargeTaxes weight { unit value } thumbnail { url alt } pricing { ...ProductPricingFields } isAvailable basePrice { currency amount } taxType { description taxCode } variants { ...ProductVariantFields } images { id url } } } """ response = graphql_request(query, variables, self.headers, self.endpoint_url) return response["data"]["product"]