承接 monurakkaya/opencart-eloquent 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

monurakkaya/opencart-eloquent

Composer 安装命令:

composer create-project monurakkaya/opencart-eloquent

包简介

Eloquent support for OpenCart models

README 文档

README

Yet another OpenCart database package for lazy laravel developers. I wrote this package to use with my OpenCart extensions. It does not replace whole opencart models with eloquent models. It just gives you the opportunity to use eloquent ORM flexibility while querying

INSTALLATION

  1. First, install the package via composer:
composer require monurakkaya/opencart-eloquent
  1. Then copy ocmod file to your system directory
cp storage/vendor/monurakkaya/opencart-eloquent/src/*.xml upload/system
  1. Go to your admin panel and refresh modifications.

EXAMPLES

To get categories count which have products in it, just type:

use App\Models\Catalog\Category\Category;

$categories_count = Category::whereHas('products')->count();

To get categories with products count, just type:

use App\Models\Catalog\Category\Category;

$categories = Category::withCount('products')->get();

echo $categories->first()->products_count // 5
echo $categories->first()->description->name // Electronics

Options and values

use App\Models\Catalog\Option\Option;

$options = Option::with('description', 'values.description')->get();

foreach ($options as $option) {
    echo $option->description->name; // Color
    foreach ($option->values as $value) {
        $value->description->name; // Dark
    }
}

What about product options and option values?

use App\Models\Catalog\Product\Product;

$product = Product::with('options.values')->find(5);

foreach($product->options as $option) {
    echo $option->option->description->name; // Color
    foreach ($option->values as $value) {
        $value->value->description->name; // Grey
    }
}

// Be careful about (N+1) which opencart doesn't care at all...

Pagination

use App\Models\Catalog\Manufacturer\Manufacturer;

$manufacturers = Manufacturer::with('description')
    ->paginate(
        $this->config->get('config_limit_admin'), 
        ['*'], 
        'page', 
        $this->request->get['page']
     );
 
//below lines belongs to opencart logic.  
$pagination = new \Pagination();
$pagination->total = $manufacturers->total();
$pagination->page = $manufacturers->currentPage();
$pagination->limit = $this->config->get('config_limit_admin');
$pagination->url = $this->url->link('extension/module/some_of_my_extensions/manufacturer', 'user_token=' . $this->session->data['user_token']. '&page={page}', true);
$pagination = $pagination->render();

$this->response->setOutput($this->load->view('extension/module/some_of_my_extensions', compact('manufacturers', 'pagination')))
  

Easy right? :)

This project will be maintained as long as I continue to use OpenCart.

WRITING YOUR EXTENSION MODELS

just put your models in your upload/app directory. Recommended way is follow opencart directory structure like:
upload/admin/controller/extension/module/product_video.php upload/admin/view/template/extension/module/product_video.twig upload/app/Models/Extension/Module/ProductVideo.php

<?php
namespace App\Models\Extension\Module

use App\Models\Model;
use App\Models\Catalog\Product\Product;

class ProductVideo extends Model {
    private $table = DB_PREFIX.'product_video';
    private $primaryKey = 'product_video_id';
    
    public function product()
    {
        return $this->belongsTo(Product::class, 'product_id') 
    }
}

then in your module controller just call:

use App\Models\Extension\Module\ProductVideo;

$video = ProductVideo::with('product')->first();
$video->product // Product object

OCMOD SUPPORT

You can easily modify original model files without touching any vendor file.

Each Model has comment lines //trait at the beginning and //ocmod at the end. just search and add after whatever you want.

with below code you can use name property as mutator so instead $product->description->name you can use $product->name:

<file path="app/Models/Catalog/Product/Product.php">
    <operation>
        <search><![CDATA[//ocmod]]></search>
        <add position="after">
            <![CDATA[
            public function getNameAttribute() {
                return $this->description->name;
            }
            ]]>
        </add>
    </operation>
</file>

RESULT

$product->description->name // Ibanez RG35EX Black
$product->name              // Ibanez RG35EX Black

with below code you can do your modification with a trait.

<?php
namespace App\Traits\Extension\Payment\MyModule\Traits\HasPrice;

trait HasPrice {
    public function getDiscountedPrice($percent = 20) {
        return $this->price * (100 - $percent) / 100;
    }
}
<file path="app/Models/Catalog/Product/Product.php">
    <operation>
        <search><![CDATA[//trait]]></search>
        <add position="after">
            <![CDATA[
            use \App\Traits\Extension\Payment\MyModule\Traits\HasPrice;
            ]]>
        </add>
    </operation>
</file>

RESULT

$product->price // 50
$product->getDiscountedPrice() // 40
$product->getDiscountedPrice(10) // 45

with below code you can create relation between opencart product model and your extension's model

<file path="app/Models/Catalog/Product/Product.php">
    <operation>
        <search><![CDATA[//ocmod]]></search>
        <add position="after">
            <![CDATA[
            public function videos() {
                return $this->hasMany(\App\Models\Extension\Module\ProductVideo::class, 'product_id', 'product_id');
            }
            ]]>
        </add>
    </operation>
</file>

RESULT

$product = Product::with('videos')->first();
$product->videos->first(); // Video model

modification file will be stored to storage/modification/app/Models/Product/Product.php

monurakkaya/opencart-eloquent 适用场景与选型建议

monurakkaya/opencart-eloquent 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 232 次下载、GitHub Stars 达 6, 最近一次更新时间为 2021 年 08 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel opencart」 「laravel-opencart」 「eloquent-opencart」 「eloquent opencart」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 monurakkaya/opencart-eloquent 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 monurakkaya/opencart-eloquent 我们能提供哪些服务?
定制开发 / 二次开发

基于 monurakkaya/opencart-eloquent 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 232
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 6
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-08-06