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 new filter class
In fact, to create a new filter you can rely on higher level of abstraction and extend \WPDesk\ShopMagic\Workflow\Filter\FilterUsingComparisionTypes
. That abstract class holds many helpful functions and skeleton for integrating UI interface for filter creation on 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 public function get_name(): string
. Simply return the string (you may wrap it in __()
for i18n).
Creating business logic for filter
There are two the most important methods in filter making it do its work.
public function passed(): bool
protected function get_type(): \WPDesk\ShopMagic\Workflow\Filter\ComparisonType\ComparisonType
First one, passed()
holds the business logic of the filter. Here you are preparing data for filtering. To use some example, lets say we want to check if our customer has id higher than 270. (The number is totally irrelevant).
To check that initially we have to get customer currently passed to the event and simply retrieve his/hers 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 filter - call to get_type()
method. Inside this protected method you just have to instantiate a new comparison type object which handles UI selection for filter like picking a product that matches a name specified by 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 event 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 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 newly created filter into ShopMagic
After writing code for new filter you may notice that it is not shown in available filters on frontend yet. That's because you have to inform ShopMagic about 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 new filter by hooking into ShopMagic action you have to use associative array - simply pushing to the list won't work, because UI form will have no idea, where to save the filter in database. The key for the array has no greater meaning, yet those must remain unique, unless you want to overwrite some filters.