PHP code example of hon / hon-sku

1. Go to this page and download the library: Download hon/hon-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/ */

    

hon / hon-sku example snippets


use Illuminate\Database\Eloquent\Model;
use Tmaic\TmaicSku\Traits\HasSku;

class ProductM extends Model
{
    use HasSku;
}

use Hon\HonSku\Models\Option;
Option::create(['name' => '尺寸']);

$option->delete();

$poduct->attrs()->get();
$poduct->attrs;

$product->addAttrValues($option, ['S', 'M', 'L']);
$product->addAttrValues('包装', ['一包(2.5kg)', '一件(25kg)', '一车(250kg)']);

$product->syncAttrValues($option, ['红色', '白色']);

$product->removeAttrValues($option);

addAttrValues($option, ...$value)
syncAttrValues($option, ...$value)
removeAttrValues($option)

$product->syncSkuWithAttrs([$attr1, $attr2, $attr3], ['amount' => 5000, 'stock' => 100]);

use Hon\HonSku\Models\Sku;
// 通过属性值组合获取sku
$sku = Sku::findByPosition($attr1, $attr2);
// 获取产品sku实例
$product->skus()->get();

$sku->delete();
$product->skus()->delete();

use Hon\HonSku\Models\Sku;
Sku::findByPosition([$attr1, $attr2, $attr3])

// 增加属性值组合
$sku->assignAttrs([$attr1, $attr2])
// 同步属性值组合
$sku->syncAttrs([$attr1, $attr2])
// 移除属性值组合
$sku->removeAttrs([$attr1, $attr2])

// 创建产品
$product = Proudcts::create(['productName' => '75-85云皮腿肉']);

// 准备作为sku属性
$unit = $product->addAttrValues('单位', ['包', '件']);
$weight  = $product->addAttrValues('重量', ['250g', '40包']);

// 获取属性值实例
$bag = $unit->firstWhere('value', '包');
$boxful = $unit->firstWhere('value', '箱');
$bagWeight = $weight->firstWhere('value', '250g');
$boxfulWeight = $weight->firstWhere('value', '40包');

// 组合属性值,建立sku
$product->syncSkuWithAttrs([$unit, $bag], ['amount' => 12699, 'stock' => 100]);
$product->syncSkuWithAttrs([$boxful, $boxfulWeight], ['amount' => 12699, 'stock' => 100]);

// 获取商品及商品SKU数据
$product = $product->load('skus.attrs.option');
bash
php artisan vendor:publish --tag=hon-sku-migrations
bash
php artisan migrate
bash
php artisan vendor:publish --tag=hon-sku-config