PHP code example of tmaic / tmaic-sku
1. Go to this page and download the library: Download tmaic/tmaic-sku library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tmaic / tmaic-sku example snippets
use Illuminate\Database\Eloquent\Model;
use Tmaic\TmaicSku\Traits\HasSku;
class Product extends Model
{
use HasSku;
}
use Tmaic\TmaicSku\Models\Option;
Option::create(['name' => '尺寸']);
$option->delete();
$poduct->attrs()->get();
$poduct->attrs;
$product->addAttrValues($option, ['S', 'M', 'L']);
$product->addAttrValues('套餐', ['套餐一', '套餐二', '套餐三']);
$product->syncAttrValues($option, ['红色', '白色']);
$product->removeAttrValues($option);
addAttrValues($option, ...$value)
syncAttrValues($option, ...$value)
removeAttrValues($option)
$product->syncSkuWithAttrs([$attr1, $attr2, $attr3], ['amount' => 5000, 'stock' => 100]);
use Tmaic\TmaicSku\Models\Sku;
// 通过属性值组合获取sku
$sku = Sku::findByPosition($attr1, $attr2);
// 获取产品sku实例
$product->skus()->get();
$sku->delete();
$product->skus()->delete();
use Tmaic\TmaicSku\Models\Sku;
Sku::findByPosition([$attr1, $attr2, $attr3])
// 增加属性值组合
$sku->assignAttrs([$attr1, $attr2])
// 同步属性值组合
$sku->syncAttrs([$attr1, $attr2])
// 移除属性值组合
$sku->removeAttrs([$attr1, $attr2])
// 创建产品
$product = Goods::create(['GoodsName' => 'phone 11 Pro Max']);
// 准备作为sku属性
$colorAttrs = $product->addAttrValues('选择外观', ['深空灰', '银色']);
$capacity = $product->addAttrValues('存储容量', ['128GB', '256GB']);
// 获取属性值实例
$black = $colorAttrs->firstWhere('value', '黑色');
$white = $colorAttrs->firstWhere('value', '白色');
$siz128GB = $capacity->firstWhere('value', '128GB');
$siz256GB = $capacity->firstWhere('value', '256GB');
// 组合属性值,建立sku
$product->syncSkuWithAttrs([$black, $siz128GB], ['amount' => 12699, 'stock' => 100]);
$product->syncSkuWithAttrs([$black, $siz256GB], ['amount' => 12699, 'stock' => 100]);
$product->syncSkuWithAttrs([$white, $siz128GB], ['amount' => 12699, 'stock' => 100]);
$product->syncSkuWithAttrs([$white, $siz256GB], ['amount' => 12699, 'stock' => 100]);
// 获取商品及商品SKU数据
$product = $product->load('skus.attrs.option');
bash
php artisan vendor:publish --tag=tmaic-sku-migrations
bash
php artisan migrate
bash
php artisan vendor:publish --tag=tmaic-sku-config