Skip to content

Coupon codes

This article contains all actions for retrieving and managing coupon codes.

Get coupon by ID#

query {
  getCouponCode(id: "1") {
    id
    siteid
    parentid
    code
    count
    dateend
    datestart
    ref
    mtime
    ctime
    editor
  }
}
const body = JSON.stringify({'query':
`query {
  getCouponCode(id: "1") {
    id
    siteid
    parentid
    code
    count
    dateend
    datestart
    ref
    mtime
    ctime
    editor
  }
}`});

fetch($('.aimeos').data('graphql'), {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "getCouponCode": {
      "id": "32",
      "siteid": "1.",
      "parentid": 38,
      "code": "fixed",
      "count": "1000",
      "dateend": null,
      "datestart": null,
      "ref": "",
      "mtime": "2022-12-01 11:59:03",
      "ctime": "2022-12-01 11:59:03",
      "editor": "core"
    }
  }
}

Search coupon codes#

The filter parameter is explained in the filter section of the GraphQL basics article.

query {
  searchCouponCodes(filter: "{\"==\": {\"coupon.code.count\":1000}}") {
    id
    siteid
    parentid
    code
    count
    dateend
    datestart
    ref
    mtime
    ctime
    editor
  }
}
let filter = {
    "==": {"coupon.code.count":1000}
};
const fstr = JSON.stringify(JSON.stringify(filter));
const body = JSON.stringify({'query':
`query {
  searchCouponCodes(filter: ` + fstr + `) {
    id
    siteid
    parentid
    code
    count
    dateend
    datestart
    ref
    mtime
    ctime
    editor
  }
}`});

fetch($('.aimeos').data('graphql'), {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "searchCouponCodes": [
      {
        "id": "32",
        "siteid": "1.",
        "parentid": 38,
        "code": "fixed",
        "count": "1000",
        "dateend": null,
        "datestart": null,
        "ref": "",
        "mtime": "2022-12-01 11:59:03",
        "ctime": "2022-12-01 11:59:03",
        "editor": "core"
      },
      {
        "id": "33",
        "siteid": "1.",
        "parentid": 39,
        "code": "percent",
        "count": "1000",
        "dateend": null,
        "datestart": null,
        "ref": "",
        "mtime": "2022-12-01 11:59:03",
        "ctime": "2022-12-01 11:59:03",
        "editor": "core"
      }
    ]
  }
}

Save single coupon#

mutation {
  saveCouponCode(input: {
    parentid: 1
    code: "TEST"
    count: 1
  }) {
    id
  }
}
const body = JSON.stringify({'query':
`mutation {
  saveCouponCode(input: {
    parentid: 1
    code: "TEST"
    count: 1
  }) {
    id
  }
}`});

fetch($('.aimeos').data('graphql'), {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "saveCouponCode": {
      "id": "3"
    }
  }
}

Save multiple coupon codes#

mutation {
  saveCouponCodes(input: [{
    parentid: 2
    code: "TEST2"
    count: 10
  },{
    parentid: 12
    code: "TEST3"
    count: null
  }]) {
    id
  }
}
const body = JSON.stringify({'query':
`mutation {
  saveCouponCodes(input: [{
    parentid: 2
    code: "TEST2"
    count: 10
  },{
    parentid: 12
    code: "TEST3"
    count: null
  }]) {
    id
  }
}`});

fetch($('.aimeos').data('graphql'), {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "saveCouponCodes": [
      {
        "id": "4"
      },
      {
        "id": "5"
      }
    ]
  }
}

Delete single coupon#

mutation {
  deleteCouponCode(id: "3")
}
const body = JSON.stringify({'query':
`mutation {
  deleteCouponCode(id: "3")
}`});

fetch($('.aimeos').data('graphql'), {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "deleteCouponCode": "3"
  }
}

Delete multiple coupon codes#

mutation {
  deleteCouponCodes(id: ["4", "5"])
}
const body = JSON.stringify({'query':
`mutation {
  deleteCouponCodes(id: ["4", "5"])
}`});

fetch($('.aimeos').data('graphql'), {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "deleteCouponCodes": [
      "4",
      "5"
    ]
  }
}

Comments

Become an Aimeos Partner

Aimeos partners are first-class specialists in creating or hosting your Aimeos e-commerce project. They have proven their expertise by building top level e-commerce applications using Aimeos.