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
  }
}
Aimeos.query(`query {
  getRule(id: "1") {
    id
    siteid
    type
    label
    provider
    datestart
    dateend
    config
    position
    status
    mtime
    ctime
    editor
  }
}`).then(data => {
  console.log(data)
})
const body = JSON.stringify({'query':
`query {
  getRule(id: "1") {
    id
    siteid
    type
    label
    provider
    datestart
    dateend
    config
    position
    status
    mtime
    ctime
    editor
  }
}`});

fetch('<GraphQL URL>', {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': '<CSRF token>'
    },
    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\\"}}") {
    items {
      id
      siteid
      type
      label
      provider
      datestart
      dateend
      config
      position
      status
      mtime
      ctime
      editor
    }
    total
  }
}
Aimeos.query(`query {
  searchRules(filter: "{\\"~=\\": {\\"rule.label\\":\\"Test\\"}}") {
    items {
      id
      siteid
      type
      label
      provider
      datestart
      dateend
      config
      position
      status
      mtime
      ctime
      editor
    }
    total
  }
}`).then(data => {
  console.log(data)
})
let filter = {
    "~=": {"rule.label":"Test"}
};
const fstr = JSON.stringify(JSON.stringify(filter));
const body = JSON.stringify({'query':
`query {
  searchRules(filter: ` + fstr + `) {
    items {
      id
      siteid
      type
      label
      provider
      datestart
      dateend
      config
      position
      status
      mtime
      ctime
      editor
    }
    total
  }
}`});

fetch('<GraphQL URL>', {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': '<CSRF token>'
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "searchRules": {
      "items": [
        {
          "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"
        }
      ],
      "total": 1
    }
  }
}

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
  }
}
Aimeos.query(`query {
  getRuleConfig(provider: "Percent,Category", type: "catalog") {
    code
    type
    label
  }
}`).then(data => {
  console.log(data)
})
const body = JSON.stringify({'query':
`query {
  getRuleConfig(provider: "Percent,Category", type: "catalog") {
    code
    type
    label
  }
}`});

fetch('<GraphQL URL>', {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': '<CSRF token>'
    },
    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
  }
}
Aimeos.query(`query {
  saveRule(input: {
    type: "catalog"
    label: "Test rule"
    provider: "Percent"
    config: "{\\"percent\\": 10}"
  }) {
    id
  }
}`).then(data => {
  console.log(data)
})
const body = JSON.stringify({'query':
`mutation {
  saveRule(input: {
    type: "catalog"
    label: "Test rule"
    provider: "Percent"
    config: "{\\"percent\\": 10}"
  }) {
    id
  }
}`});

fetch('<GraphQL URL>', {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': '<CSRF token>'
    },
    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
  }
}
Aimeos.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
  }
}`).then(data => {
  console.log(data)
})
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('<GraphQL URL>', {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': '<CSRF token>'
    },
    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")
}
Aimeos.query(`mutation {
  deleteRule(id: "2")
}`).then(data => {
  console.log(data)
})
const body = JSON.stringify({'query':
`mutation {
  deleteRule(id: "2")
}`});

fetch('<GraphQL URL>', {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': '<CSRF token>'
    },
    body: body
}).then(response => {
    return response.json();
}).then(data => {
    console.log(data);
});

Response:

{
  "data": {
    "deleteRule": "2"
  }
}

Delete multiple rules#

mutation {
  deleteRules(id: ["3", "4"])
}
Aimeos.query(`mutation {
  deleteRules(id: ["3", "4"])
}`).then(data => {
  console.log(data)
})
const body = JSON.stringify({'query':
`mutation {
  deleteRules(id: ["3", "4"])
}`});

fetch('<GraphQL URL>', {
    method: 'POST',
    credentials: 'same-origin',
    headers: { // Laravel only
        'X-CSRF-TOKEN': '<CSRF token>'
    },
    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.