Skip to content

Coupons

This article contains all actions for retrieving and managing coupons.

Get coupon by ID#

query {
  getCoupon(id: "1") {
    id
    siteid
    label
    provider
    config
    datestart
    dateend
    status
    mtime
    ctime
    editor
  }
}
const body = JSON.stringify({'query':
`query {
  getCoupon(id: "1") {
    id
    siteid
    label
    provider
    config
    datestart
    dateend
    status
    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": {
    "getCoupon": {
      "id": "1",
      "siteid": "1.",
      "label": "demo-voucher",
      "provider": "Voucher",
      "config": "{\"voucher.productcode\":\"demo-rebate\"}",
      "datestart": null,
      "dateend": null,
      "status": 1,
      "mtime": "2022-12-01 11:59:03",
      "ctime": "2022-12-01 11:59:03",
      "editor": "core"
    }
  }
}

Search coupons#

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

query {
  searchCoupons(filter: "{\"~=\": {\"coupon.label\":\"demo\"}}") {
    id
    siteid
    label
    provider
    config
    datestart
    dateend
    status
    mtime
    ctime
    editor
  }
}
let filter = {
    "~=": {"coupon.label":"demo"}
};
const fstr = JSON.stringify(JSON.stringify(filter));
const body = JSON.stringify({'query':
`query {
  searchCoupons(filter: ` + fstr + `) {
    id
    siteid
    label
    provider
    config
    datestart
    dateend
    status
    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": {
    "searchCoupons": [
      {
        "id": "1",
        "siteid": "1.",
        "label": "demo-voucher",
        "provider": "Voucher",
        "config": "{\"voucher.productcode\":\"demo-rebate\"}",
        "datestart": null,
        "dateend": null,
        "status": 1,
        "mtime": "2022-12-01 11:59:03",
        "ctime": "2022-12-01 11:59:03",
        "editor": "core"
      },
      {
        "id": "2",
        "siteid": "1.",
        "label": "demo-percent",
        "provider": "PercentRebate",
        "config": "{\"percentrebate.productcode\":\"demo-rebate\",\"percentrebate.rebate\":\"10\"}",
        "datestart": null,
        "dateend": null,
        "status": 1,
        "mtime": "2022-12-01 11:59:03",
        "ctime": "2022-12-01 11:59:03",
        "editor": "core"
      }
    ]
  }
}

Fetch coupon configuration#

To retrieve the backend configuration for a coupon provider and the added decorators use:

query {
  getCouponConfig(provider: "Voucher,Basket") {
    code
    type
    label
  }
}
const body = JSON.stringify({'query':
`query {
  getCouponConfig(provider: "Voucher,Basket") {
    code
    type
    label
  }
}`});

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": {
    "getCouponConfig": [
      {
        "code":"voucher.productcode",
        "type":"string",
        "label":"Product code of the rebate product"
      },{
        "code":"basket.total-value-min",
        "type":"map",
        "label":"Minimum total value of the basket"
      },{
        "code":"basket.total-value-max",
        "type":"map",
        "label":"Maximum total value of the basket"
      }
    ]
  }
}

Save single coupon#

mutation {
  saveCoupon(input: {
    label: "Test coupon"
    provider: "PercentRebate"
    config: "{\"percentrebate.productcode\":\"demo-rebate\",\"percentrebate.rebate\":\"25\"}",
  }) {
    id
  }
}
const body = JSON.stringify({'query':
`mutation {
  saveCoupon(input: {
    label: "Test coupon"
    provider: "PercentRebate"
    config: "{\"percentrebate.productcode\":\"demo-rebate\",\"percentrebate.rebate\":\"25\"}",
  }) {
    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": {
    "saveCoupon": {
      "id": "4"
    }
  }
}

Save multiple coupons#

mutation {
  saveCoupons(input: [{
    label: "Test coupon 2"
    provider: "PercentRebate"
    config: "{\"percentrebate.productcode\":\"demo-rebate\",\"percentrebate.rebate\":\"7.5\"}",
  },{
    label: "Test coupon 3"
    provider: "FixedRebate,BasketValues"
    config: "{\"fixedrebate.productcode\":\"demo-rebate\",\"fixedrebate.rebate\":{\"EUR\":"10.00"},\"basket.total-value-min\":{\"EUR\":100}}",,
  }]) {
    id
  }
}
const body = JSON.stringify({'query':
`mutation {
  saveCoupons(input: [{
    label: "Test coupon 2"
    provider: "PercentRebate"
    config: "{\"percentrebate.productcode\":\"demo-rebate\",\"percentrebate.rebate\":\"7.5\"}",
  },{
    label: "Test coupon 3"
    provider: "FixedRebate,BasketValues"
    config: "{\"fixedrebate.productcode\":\"demo-rebate\",\"fixedrebate.rebate\":{\"EUR\":"10.00"},\"basket.total-value-min\":{\"EUR\":100}}",
  }]) {
    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": {
    "saveCoupons": [
      {
        "id": "5"
      },
      {
        "id": "6"
      }
    ]
  }
}

Delete single coupon#

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

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": {
    "deleteCoupon": "4"
  }
}

Delete multiple coupons#

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

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": {
    "deleteCoupons": [
      "5",
      "6"
    ]
  }
}

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.