Created
May 5, 2020 12:53
-
-
Save cagartner/b8778b456685a9ab4ac9ad7f0fbc33b0 to your computer and use it in GitHub Desktop.
Creating product programatically on Bagisto Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// File: app/Http/Controllers/CreateProductController.php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Webkul\Attribute\Models\Attribute; | |
use Webkul\Attribute\Models\AttributeOption; | |
use Webkul\Attribute\Repositories\AttributeFamilyRepository; | |
use Webkul\Product\Repositories\ProductRepository; | |
class CreateProductController extends Controller | |
{ | |
/** | |
* Product Repository instance | |
* | |
* @var ProductRepository | |
*/ | |
protected $productRepository; | |
/** | |
* AttributeFamily Repository instance | |
* | |
* @var \Webkul\Product\Repositories\AttributeFamilyRepository | |
*/ | |
protected $attributeFamilyRepository; | |
/** | |
* Product Attribute Types | |
* | |
* @var array | |
*/ | |
protected $types; | |
/** | |
* Create a new helper instance. | |
* | |
* @param ProductRepository $productRepository | |
* @param AttributeFamilyRepository $attributeFamilyRepository | |
*/ | |
public function __construct( | |
ProductRepository $productRepository, | |
AttributeFamilyRepository $attributeFamilyRepository | |
) | |
{ | |
$this->productRepository = $productRepository; | |
$this->attributeFamilyRepository = $attributeFamilyRepository; | |
$this->types = [ | |
'text', | |
'textarea', | |
'boolean', | |
'select', | |
'multiselect', | |
'datetime', | |
'date', | |
'price', | |
'image', | |
'file', | |
'checkbox', | |
]; | |
} | |
public function create() | |
{ | |
$sku = 'teste-' . time(); | |
$data = [ | |
'type' => 'simple', | |
'attribute_family_id' => 1, // Id do grupo de atributos, | |
'sku' => $sku | |
]; | |
$product = $this->productRepository->create($data); | |
$data['name'] = 'Product Example ' . time(); | |
$data['url_key'] = $sku; | |
$data['width'] = 10; | |
$data['height'] = 10; | |
$data['depth'] = 10; | |
$data['weight'] = 1; | |
$data['price'] = 120; | |
// Get core Channel | |
$channel = core()->getCurrentChannel(); | |
$data['locale'] = core()->getCurrentLocale()->code; | |
if ($brand = Attribute::where(['code' => 'brand'])->first()) { | |
// Adiciona uma marca se tiver | |
$data['brand'] = AttributeOption::where(['attribute_id' => $brand->id])->first()->id ?? ''; | |
} | |
$data['channel'] = $channel->code; | |
$data['channels'] = [ | |
0 => $channel->id, | |
]; | |
// Pega estoques | |
$inventorySource = $channel->inventory_sources[0]; | |
$data['inventories'] = [ | |
$inventorySource->id => 10, | |
]; | |
// Pega categorias | |
$data['categories'] = [ | |
0 => $channel->root_category->id, | |
]; | |
$updated = $this->productRepository->update($data, $product->id); | |
return $updated; | |
} | |
/** | |
* @param $code | |
* @return \Illuminate\Support\Collection | |
*/ | |
protected function getFamilyAttributes($code) | |
{ | |
$attributeFamily = $this->attributeFamilyRepository->findWhere([ | |
'code' => $code, | |
]); | |
$attributes = collect(); | |
if ($attributeFamily->count()) { | |
$attributeGroups = $attributeFamily->first()->attribute_groups; | |
foreach ($attributeGroups as $attributeGroup) { | |
foreach ($attributeGroup->custom_attributes as $customAttribute) { | |
$attributes->push($customAttribute); | |
} | |
} | |
} | |
return $attributes; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// File: routes/web.php | |
/* | |
|-------------------------------------------------------------------------- | |
| Web Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register web routes for your application. These | |
| routes are loaded by the RouteServiceProvider within a group which | |
| contains the "web" middleware group. Now create something great! | |
| | |
*/ | |
Route::get('createProductTest', 'CreateProductController@create'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment