Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Pablo Borowicz <[email protected]>
Co-authored-by: Krystian Podemski <[email protected]>
  • Loading branch information
3 people authored Nov 2, 2022
1 parent 59bf678 commit 4482766
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
10 changes: 5 additions & 5 deletions modules/sample-modules/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ weight: 80

# Sample modules and how to guides

The PrestaShop Community made some example modules and how to guides to help you understand how to implement hooks, admin grids, manage entities... and a lot more.
The PrestaShop Community has made example modules and how-to guides to help you understand how to implement hooks, admin grids, manage entities... and a lot more.

{{% children %}}

## Our example modules repository
## Example modules repository

* [See all our example modules (GitHub)](https://github.com/PrestaShop/example-modules)

This repository hosts example modules built and maintained by PrestaShop.
These modules demonstrate useful usecases for developers willing to customize the software.
This repository hosts example modules built and maintained by project members and the community.
These modules demonstrate good use cases for developers willing to customize the software.

### Extending Symfony pages

- [Demo Extending a Symfony Form - 1](https://github.com/PrestaShop/example-modules/tree/master/demoextendsymfonyform1) - how to insert an input inside a Symfony form
- [Extending a Symfony form - how to insert a field inside an existing form](https://github.com/PrestaShop/example-modules/tree/master/demoextendsymfonyform1)
- [Demo Extending a Symfony Form - 2](https://github.com/PrestaShop/example-modules/tree/master/demoextendsymfonyform2) - how to insert an input inside a Symfony form - 2
- [Demo Extending a Symfony Form - 3](https://github.com/PrestaShop/example-modules/tree/master/demoextendsymfonyform3) - how to use CQRS in a module
- [Demo View Order Hooks](https://github.com/PrestaShop/example-modules/tree/master/demovieworderhooks)
Expand Down
8 changes: 4 additions & 4 deletions modules/sample-modules/example-hooks/_index.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
title: Hooks examples
title: Hook examples
weight: 2
---

# Hooks implementation examples
# Hook implementation examples

Hooks is one of the most powerful tools to extend or modify PrestaShop with modules.
Hooks are one of the most powerful tools to extend or modify PrestaShop using modules.

For a complete introduction about hooks, please read [Hooks]({{< relref "/8/modules/concepts/hooks/" >}})
For a complete introduction to hooks, please read [Hooks]({{< relref "/8/modules/concepts/hooks/" >}})

{{% children %}}
30 changes: 15 additions & 15 deletions modules/sample-modules/example-hooks/actionValidateOrder.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: "actionValidateOrder"
weight: 3
---

# Hook example : actionValidateOrder
# Hook example: actionValidateOrder

This hook is triggered when an `order` is validated.

Expand All @@ -26,9 +26,9 @@ actionValidateOrder
);
```

A classic use-case for this hook could be :
A classic use case for this hook could be:

> I want to reward my customers on their _n-th_ order
_I want to reward my customers on their n-th order_

```php
<?php
Expand All @@ -47,51 +47,51 @@ class MyModuleRewardCustomerWhenOrder extends Module
if ($hasValidParams && !$this->customerAlreadyRewarded((int) $customerObject->id)) {
$hasConfiguredState = in_array((int) $orderObject->getCurrentState(), $this->getConfuredOrdersStatesIds());
$hasCustomerRequiredNbrOfTheOrderToReward = $this->getCustomerValidOrdersNbr((int) $customerObject->id) == $this->getRequiredNbrOfTheOrderToReward();
if($hasConfiguredState && $hasCustomerRequiredNbrOfTheOrderToReward) {
if ($hasConfiguredState && $hasCustomerRequiredNbrOfTheOrderToReward) {
$customerReward = $this->createCustomerReward($customerObject, $orderObject);
if(Validate::isLoadedObject($customerReward)) {
$this->setAlreadyRewarded($customerObject);
$this->notifyCustomer($customerObject, $customerReward);
if (Validate::isLoadedObject($customerReward)) {
$this->setAlreadyRewarded($customerObject);
$this->notifyCustomer($customerObject, $customerReward);

//TODO : of course don't forget to log if something fails here :)
// of course don't forget to log if something fails here :)
}
}
}
}

protected function customerAlreadyRewarded(int $idCustomer): bool
{
//TODO : check if customer already rewarded
// check if customer already rewarded
}

protected setAlreadyRewarded(): void
{
//TODO: set customer was rewarded
// set customer was rewarded
}

protected function getConfuredOrdersStatesIds(): array
{
//TODO : return array with configured states ids in your module
// return array with configured states ids in your module
}

protected function getCustomerValidOrdersNbr(int $idCustomer): int
{
//TODO : return number of total order valid by customer
// return number of total order valid by customer
}

protected function getRequiredNbrOfTheOrderToReward(): int
{
//TODO : return configured number of orders required to reward the customer
// return configured number of orders required to reward the customer
}

protected function createCustomerReward(Customer $customer, Order $order): ?CartRule
{
//TODO: generate customer cart rule (according to the order amount for example)
// generate customer cart rule (according to the order amount for example)
}

protected function notifyCustomer(Customer $customer, CartRule $cartRule): bool
{
//TODO: notify the customer
// notify the customer
}
}
```

0 comments on commit 4482766

Please sign in to comment.