ThriveThemes / thrive-automator-docs

7 stars 2 forks source link

Cannot read properties of undefined (reading 'filters') #2

Open ajgagnon opened 2 years ago

ajgagnon commented 2 years ago

Thanks for the awesome library and docs! I'm trying to create a custom trigger. However, once it's selected I'm getting this error:

Cannot read properties of undefined (reading 'filters')

The data field is showing up in the dropdown, but when I select my Data_Field, I'm getting the error. In my Data_Field, I'm returning:

public static function get_supported_filters() {
    return [ 'string_contains', 'string_equals' ]; 
}

So I should think the filters are defined. But perhaps there is a misconfiguration. Is there anything I should be checking?

Matei7 commented 2 years ago

@ajgagnon Could you give me some more info about your field? I would like to know the field ID and also if you could provide us the Data Object that you are using, at least its ID.

Also as a heads-up at the moment we only support 1 type of filter for each so only the first element in the array will work as the field filter, but we already have some combined filters like string_ec which is string equals, contains, empty/not empty that you can use

ajgagnon commented 2 years ago

Thanks for your reply @Matei7

Here's my data field:

class ProductNameField extends Data_Field {
    public static function get_id() {
        return 'surecart/product_name';
    }

    public static function get_name() {
        return __( 'Product Name', 'surecart' );
    }

    public static function get_description() {
        return __( 'A specific product name.', 'surecart' );
    }

    public static function get_supported_filters() {
        return [ 'string_ec' ];
    }

    public static function get_validators() {
        return [ 'required' ];
    }

    public static function get_placeholder() {
        return '';
    }

    public static function get_field_value_type() {
        return static::TYPE_STRING;
    }

    public static function get_dummy_value() {
        return 'Product Name';
    }

    public static function primary_key(): array {
        return [ ProductData::get_id() ];
    }
}

And Data_Object:

class ProductData extends Data_Object {
    /**
     * Get the data-object identifier
     *
     * @return string
     */
    public static function get_id() {
        return 'surecart/product_data';
    }

    /**
     * Nice name for the data object.
     *
     * @return string
     */
    public static function get_nice_name() {
        return __( 'SureCart product', 'surecart' );
    }

    /**
     * Array of field object keys that are contained by this data-object
     *
     * @return array
     */
    public static function get_fields() {
        return [
            ProductNameField::get_id(),
            'archived',
            'shipping_enabled',
            'tax_category',
            'tax_enabled',
            'metadata',
            'file_upload_ids',
            'prices',
            'product_group',
        ];
    }

    /**
     * Create the object from the given product.
     *
     * @param string|\SureCart\Models\Product $param Product model or id.
     *
     * @throws \Exception If no parameter is provided.
     *
     * @return array
     */
    public static function create_object( $param ) {
        if ( empty( $param ) ) {
            throw new \Exception( 'No parameter provided for SureCart ProductData object' );
        }

        $product = null;
        if ( is_a( $param, Product::class ) ) {
            $product = $param;
        } else {
            $product = Product::find( $param );
        }

        if ( $product ) {
            return [
                'surecart_product_id' => $product->id,
                'name'                => $product->name,
                'archived'            => $product->archived,
                'shipping_enabled'    => $product->shipping_enabled,
                'tax_category'        => $product->tax_category,
                'tax_enabled'         => $product->tax_enabled,
                'metadata'            => $product->metadata,
                'file_upload_ids'     => $product->file_upload_ids,
                'prices'              => $product->prices,
                'product_group'       => $product->product_group,
            ];
        }

        return $product;
    }

    /**
     * Get the options.
     *
     * @return array
     */
    public static function get_data_object_options() {
        $options = [];

        foreach ( Product::get() as $product ) {
            $name           = $product->name;
            $id             = $product->id;
            $options[ $id ] = array(
                'id'    => $id,
                'label' => $name,
            );
        }

        return $options;
    }
}
Matei7 commented 2 years ago

@ajgagnon thank you for your help.

I've managed to replicate the issue based on your files and indeed there was an issue in our code related to slashes added inside the item ID, but we will release a fix for this next week.

ajgagnon commented 2 years ago

I see this was closed out. Did this get in any update? I'm still having this issue.

Matei7 commented 2 years ago

@ajgagnon yes we released a fix for the error you encountered at the end of June