How to create custom filters in ShopMagic
To create a new filter in ShopMagic you are required to:
- Create a class that extends
\WPDesk\ShopMagic\Workflow\Filter\Filter
abstract class. - Hook that into already created ShopMagic filters.
Creating a new filter class
In fact, to create a new filter you can rely on a higher level of abstraction and extend \WPDesk\ShopMagic\Workflow\Filter\FilterUsingComparisionTypes
. That abstract class holds many helpful functions and a framework for integrating the UI interface for filter creation on the front end.
Additionally, you can choose to use even higher levels of abstraction from provided specified abstract classes:
\WPDesk\ShopMagic\Workflow\Filter\OrderFilter
: when you want to use filters based on WooCommerce order\WPDesk\ShopMagic\Workflow\Filter\CustomerFilter
: when you want to use filters based on Customer (user or guest)
Naming your filter
Each filter has to have its name set by the public function get_name(): string
. Simply return the string (you may wrap it in __()
for i18n).
Creating business logic for filter
There are two most important methods in filter making it do its work.
public function passed(): bool
protected function get_type(): \WPDesk\ShopMagic\Workflow\Filter\ComparisonType\ComparisonType
The first one, passed()
holds the business logic of the filter. Here you are preparing data for filtering. To use some example, let's say we want to check if our customer has an id higher than 270. (The number is totally irrelevant).
To check that initially we have to get the customer currently passed to the event and simply retrieve his/her ID.
public function passed() {
$customer = $this->resources->get(Customer::class); // We inherit resources from FilterUsingComparisonTypes class
return $this->get_type()->passed(
$this->fields_data->get( \WPDesk\ShopMagic\Workflow\Filter\ComparisonTypes\FloatType::VALUE_KEY ),
$this->fields_data->get( \WPDesk\ShopMagic\Workflow\Filter\ComparisonTypes\FloatType::CONDITION_KEY ),
$customer->get_id()
);
}
In the snippet above you can also see the other important component of the filter - call to get_type()
method. Inside this protected method you just have to instantiate a new comparison type object that handles UI selection for filters like picking a product that matches a name specified by the user.
There are many built-in comparison types which you can find in src/Filter/ComparisonType
(looking up from the project root folder). You can even write your own comparison type by extending abstract class \WPDesk\ShopMagic\Workflow\Filter\ComparitionType\AbstractType
.
In our example, we have used FloatType
class which simply compares the passed value to the expected one, set in WordPress automation form.
You can find code example for writing a custom filter on our GitHub repository.
Hook the newly created filter into ShopMagic
After writing the code for the new filter you may notice that it is not shown in available filters on the frontend yet. That's because you have to inform ShopMagic about the new filter and hook it into the list of existing filters.
To do that, you will need to attach action to shopmagic/core/filters
hook. In fact, you just need to append your class to the array of filters.
add_filter('shopmagic/core/filters', function ( array $filters ) {
$filters['order_id'] = new NewOrderIdFilter();
return $filters;
});
Now you are good to go and test your new filter!
Worth noticing!
When adding a new filter by hooking into ShopMagic action you have to use an associative array - simply pushing to the list won't work, because the UI form will have no idea, where to save the filter in the database. The key for the array has no greater meaning, yet those must remain unique unless you want to overwrite some filters.