0

I'm working on plugin that make extension for Ideas Shop. My problem is that I've had added some new fields to Product model in my extension plugin but this fields is not working with operations like update or creating some new product because the structure is that data from products controller are sending to Ideas\Shop\Facades\Product method saveProductData() and then they are saving in Ideas\Shop\Models\Products model. My question is how can I override the saveProductData() in my plugin extension.

namespace Ideas\Shop\Controllers;
//class Products extends IdeasShopController

/**
 * Override create_onSave()
 */
public function create_onSave()
{
    $post = post();
    $rs = ProductFacades::saveProduct($post);
    if ($rs['rs'] != IdeasShop::SUCCESS) {
        Flash::error($rs['msg'][0]);//save flash in next refresh
    } else {
        $url = $this->handleSaveResult($rs, $post, 'create');
        return redirect($url);
    }
}

/**
 * Override update_onSave()
 */
public function update_onSave()
{
    $post = post();
    $rs = ProductFacades::saveProduct($post);
    if ($rs['rs'] != IdeasShop::SUCCESS) {
        Flash::error($rs['msg'][0]);//save flash in next refresh
    } else {
        $url = $this->handleSaveResult($rs, $post, 'update');
        return redirect($url);
    }
}

In this model is method that I want override in my plugin extensions...

namespace Ideas\Shop\Facades;
//class Product extends Model

public static function saveProductData($post)
{
    $id = $post['id'];
    $model = new Products();
    if ($id != 0) {//create
        $model = Products::find($id);
    }
    $product = $post['Products'];
    $model->name = $product['name'];
    $model->slug = $product['slug'];
    $model->sku = $product['sku'];
    $model->price = $product['price'];
    $model->price_promotion = $product['price_promotion'];
    $model->qty = $product['qty'];
    if ($id == 0) {
        $model->qty_order = 0;
    }
    $model->featured_image = $product['featured_image'];
    $model->product_order = $product['product_order'];
    if ($id == 0) {//create
        $model->product_type = $product['product_type'];
        $model->attribute_group_id = $product['attribute_group_id'];
    }
    $model->tax_class_id = $product['tax_class_id'];
    $model->weight = $product['weight'];
    $model->weight_id = $product['weight_id'];
    $model->status = $product['status'];
    $model->is_virtual_product = $product['is_virtual_product'];
    $model->save();
    return $model;
}

And this is model that is used for saving data from product to database

namespace Ideas\Shop\Models;    
class Products extends Model
    {
      ...
    }

1 Answers1

2

Hmm, you can not override its method but you can listen for its before save event and then assign extra fields which you added

\Ideas\Shop\Models\Products::extend(function($model) {

    $model->bindEvent('model.beforeSave', function() use ($model) {

        // you can receive data from post() may be and assign
        $model->my_new_field = 'some data';
    });
});

It will set field data before save so when model saved it will persist that data in to the database.

if you need to extend more things you can use this reference : https://octobercms.com/docs/database/model#extending-models

if any doubt please comment.

Hardik Satasiya
  • 9,547
  • 3
  • 22
  • 40