How to create custom filters in ShopMagic

To create a new filter in ShopMagic you are required to:

  1. Create a class that implements interface \WPDesk\ShopMagic\Filter\Filter
  2. 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\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\Filter\OrderFilter: when you want to use filters based on WooCommerce order
  • \WPDesk\ShopMagic\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(). 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.

  1. public function passed()
  2. protected function get_type()

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->get_customer(); // We inherit it from FilterUsingComparisonTypes class

    return $this->get_type()->passed(
        $this->fields_data->get( \WPDesk\ShopMagic\Filter\ComparisonTypes\FloatType::VALUE_KEY ),
        $this->fields_data->get( \WPDesk\ShopMagic\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\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.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.

Still need help? Contact Us Contact Us