This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

Pat Och
Pat Och

hi Bros,

In the plugin that I develop, I need to dynamically extend the model of another plugin with a static method. I have no idea how to do it.

In my plugin, I have a course session object that inherits from Jiri/JKShop/Models/product

In the jiri_jkshop_products table, I added an is_course field.

Before creating a product object, I would like to know if it is a course product or not in the following way:

        `if (\Jiri\JKShop\Models\Product::isCourse($productJson["product_id"])) {
            $product = \PluginAuthor\PluginName\Models\Session::find($productJson["product_id"]);
        } else {
            $product = \Jiri\JKShop\Models\Product::find($productJson["product_id"]);
        }`

How to dynamically add the is_course static method in the plugin.php file of my plugin?

I hope this idea corresponds to elegant code, if you have better, I take

thank you in advance & regards, Patrick

Last updated

MarvinDurot
MarvinDurot

You can perform this by extending your model with addDynamicMethod() :

Product::extend(function($model) {
  $model->addDynamicMethod('isCourse', function() use ($model) {
    return (bool) $model->is_course;
  });
});

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.