Skip to content

Rules

This article contains all actions for retrieving and managing rules.

Get rule by ID#

query {
  getRule(id: "1") {
    id
    siteid
    type
    label
    provider
    datestart
    dateend
    config
    position
    status
    mtime
    ctime
    editor
  }
}
const body = JSON.stringify({'query':
`query {
  getRule(id: "1") {
    id
    siteid
    type
    label
    provider
    datestart
    dateend
    config
    position
    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": {
    "getRule": {
      "id": "1",
      "siteid": "1.",
      "type": "catalog",
      "label": "+10% Test",
      "provider": "Percent",
      "datestart": null,
      "dateend": null,
      "config": "{\"last-rule\":0,\"percent\":10}",
      "position": 0,
      "status": 1,
      "mtime": "2022-12-22 09:51:38",
      "ctime": "2022-06-21 13:36:28",
      "editor": "aimeos@aimeos.org"
    }
  }
}

Search rules#

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

query {
  searchRules(filter: "{\"~=\": {\"rule.label\":\"Test\"}}") {
    id
    siteid
    type
    label
    provider
    datestart
    dateend
    config
    position
    status
    mtime
    ctime
    editor
  }
}
let filter = {
    "~=": {"rule.label":"Test"}
};
const fstr = JSON.stringify(JSON.stringify(filter));
const body = JSON.stringify({'query':
`query {
  searchRules(filter: ` + fstr + `) {
    id
    siteid
    type
    label
    provider
    datestart
    dateend
    config
    position
    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": {
    "searchRules": [
      {
        "id": "1",
        "siteid": "1.",
        "type": "catalog",
        "label": "+10% Test",
        "provider": "Percent",
        "datestart": null,
        "dateend": null,
        "config": "{\"last-rule\":0,\"percent\":10}",
        "position": 0,
        "status": 1,
        "mtime": "2022-12-22 09:51:38",
        "ctime": "2022-06-21 13:36:28",
        "editor": "aimeos@aimeos.org"
      }
    ]
  }
}

Fetch rule configuration#

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

query {
  getRuleConfig(provider: "Percent,Category", type: "catalog") {
    code
    type
    label
  }
}
const body = JSON.stringify({'query':
`query {
  getRuleConfig(provider: "Percent,Category", type: "catalog") {
    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": {
    "getRuleConfig": [
      {
        "code":"last-rule",
        "type":"boolean",
        "label":"Don't execute subsequent rules"
      },{
        "code":"percent",
        "type":"number",
        "label":"Percentage to add or subtract"
      },{
        "code":"category.code",
        "type":"string",
        "label":"Category codes"
      }
    ]
  }
}

Save single rule#

mutation {
  saveRule(input: {
    type: "catalog"
    label: "Test rule"
    provider: "Percent"
    config: "{\"percent\": 10}"
  }) {
    id
  }
}
const body = JSON.stringify({'query':
`mutation {
  saveRule(input: {
    type: "catalog"
    label: "Test rule"
    provider: "Percent"
    config: "{\"percent\": 10}"
  }) {
    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": {
    "saveRule": {
      "id": "2"
    }
  }
}

Save multiple rules#

mutation {
  saveRules(input: [{
    type: "catalog"
    label: "Test rule 2"
    provider: "Percent"
    config: "{\"percent\": 10}"
  },{
    type: "catalog"
    label: "Test rule 3"
    provider: "Percent,Category"
    config: "{\"percent\":20,\"category.code\":\"demo-best\"}"
  }]) {
    id
  }
}
const body = JSON.stringify({'query':
`mutation {
  saveRules(input: [{
    type: "catalog"
    label: "Test rule 2"
    provider: "Percent"
    config: "{\"percent\": 10}"
  },{
    type: "catalog"
    label: "Test rule 3"
    provider: "Percent,Category"
    config: "{\"percent\":20,\"category.code\":\"demo-best\"}"
  }]) {
    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": {
    "saveRules": [
      {
        "id": "3"
      },
      {
        "id": "4"
      }
    ]
  }
}

Delete single rule#

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

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": {
    "deleteRule": "2"
  }
}

Delete multiple rules#

mutation {
  deleteRules(id: ["3", "4"])
}
const body = JSON.stringify({'query':
`mutation {
  deleteRules(id: ["3", "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": {
    "deleteRules": [
      "3",
      "4"
    ]
  }
}

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.