2020-01-17 13:42:47 +00:00
|
|
|
<?php
|
2020-01-28 14:38:07 +00:00
|
|
|
|
2020-01-17 13:42:47 +00:00
|
|
|
namespace Webkul\Core\Helpers;
|
|
|
|
|
|
|
|
|
|
// here you can define custom actions
|
|
|
|
|
// all public methods declared in helper class will be available in $I
|
|
|
|
|
|
|
|
|
|
use Codeception\Module\Laravel5;
|
|
|
|
|
use Illuminate\Support\Facades\Event;
|
|
|
|
|
use Webkul\Product\Models\Product;
|
|
|
|
|
use Webkul\Product\Models\ProductInventory;
|
2020-01-28 14:38:07 +00:00
|
|
|
use Webkul\Product\Models\ProductAttributeValue;
|
|
|
|
|
use Webkul\Product\Models\ProductDownloadableLink;
|
|
|
|
|
use Webkul\Product\Models\ProductDownloadableLinkTranslation;
|
2020-01-17 13:42:47 +00:00
|
|
|
|
|
|
|
|
class Laravel5Helper extends Laravel5
|
|
|
|
|
{
|
2020-01-28 14:38:07 +00:00
|
|
|
public const SIMPLE_PRODUCT = 1;
|
|
|
|
|
public const VIRTUAL_PRODUCT = 2;
|
|
|
|
|
public const DOWNLOADABLE_PRODUCT = 3;
|
|
|
|
|
|
2020-01-17 13:42:47 +00:00
|
|
|
/**
|
|
|
|
|
* Returns field name of given attribute.
|
|
|
|
|
*
|
|
|
|
|
* @param string $attribute
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
* @part ORM
|
|
|
|
|
*/
|
|
|
|
|
public static function getAttributeFieldName(string $attribute): ?string
|
|
|
|
|
{
|
|
|
|
|
$attributes = [
|
|
|
|
|
'product_id' => 'integer_value',
|
|
|
|
|
'sku' => 'text_value',
|
|
|
|
|
'name' => 'text_value',
|
|
|
|
|
'url_key' => 'text_value',
|
|
|
|
|
'tax_category_id' => 'integer_value',
|
|
|
|
|
'new' => 'boolean_value',
|
|
|
|
|
'featured' => 'boolean_value',
|
|
|
|
|
'visible_individually' => 'boolean_value',
|
|
|
|
|
'status' => 'boolean_value',
|
|
|
|
|
'short_description' => 'text_value',
|
|
|
|
|
'description' => 'text_value',
|
|
|
|
|
'price' => 'float_value',
|
|
|
|
|
'cost' => 'float_value',
|
|
|
|
|
'special_price' => 'float_value',
|
|
|
|
|
'special_price_from' => 'date_value',
|
|
|
|
|
'special_price_to' => 'date_value',
|
|
|
|
|
'meta_title' => 'text_value',
|
|
|
|
|
'meta_keywords' => 'text_value',
|
|
|
|
|
'meta_description' => 'text_value',
|
|
|
|
|
'width' => 'integer_value',
|
|
|
|
|
'height' => 'integer_value',
|
|
|
|
|
'depth' => 'integer_value',
|
|
|
|
|
'weight' => 'integer_value',
|
|
|
|
|
'color' => 'integer_value',
|
|
|
|
|
'size' => 'integer_value',
|
|
|
|
|
'brand' => 'text_value',
|
|
|
|
|
'guest_checkout' => 'boolean_value',
|
|
|
|
|
];
|
|
|
|
|
if (!array_key_exists($attribute, $attributes)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return $attributes[$attribute];
|
|
|
|
|
}
|
2020-01-28 14:38:07 +00:00
|
|
|
|
2020-01-17 13:42:47 +00:00
|
|
|
/**
|
2020-01-28 14:38:07 +00:00
|
|
|
* Helper function to generate products for testing
|
|
|
|
|
*
|
|
|
|
|
* @param int $productType
|
|
|
|
|
* @param array $configs
|
|
|
|
|
* @param array $productStates
|
2020-01-17 13:42:47 +00:00
|
|
|
*
|
|
|
|
|
* @return \Webkul\Product\Models\Product
|
2020-02-05 10:18:21 +00:00
|
|
|
* @part ORM
|
2020-01-17 13:42:47 +00:00
|
|
|
*/
|
2020-01-28 14:38:07 +00:00
|
|
|
public function haveProduct(int $productType, array $configs = [], array $productStates = []): Product
|
|
|
|
|
{
|
2020-01-17 13:42:47 +00:00
|
|
|
$I = $this;
|
2020-01-28 14:38:07 +00:00
|
|
|
|
|
|
|
|
switch ($productType) {
|
|
|
|
|
case self::DOWNLOADABLE_PRODUCT:
|
|
|
|
|
$product = $I->haveDownloadableProduct($configs, $productStates);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case self::VIRTUAL_PRODUCT:
|
|
|
|
|
$product = $I->haveVirtualProduct($configs, $productStates);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case self::SIMPLE_PRODUCT:
|
|
|
|
|
default:
|
|
|
|
|
$product = $I->haveSimpleProduct($configs, $productStates);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($product !== null) {
|
|
|
|
|
Event::dispatch('catalog.product.create.after', $product);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $product;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $configs
|
|
|
|
|
* @param array $productStates
|
|
|
|
|
*
|
|
|
|
|
* @return \Webkul\Product\Models\Product
|
|
|
|
|
*/
|
|
|
|
|
private function haveSimpleProduct(array $configs = [], array $productStates = []): Product
|
|
|
|
|
{
|
|
|
|
|
$I = $this;
|
|
|
|
|
if (!in_array('simple', $productStates)) {
|
|
|
|
|
$productStates = array_merge($productStates, ['simple']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var Product $product */
|
|
|
|
|
$product = $I->createProduct($configs['productAttributes'] ?? [], $productStates);
|
|
|
|
|
|
|
|
|
|
$I->createAttributeValues($product->id, $configs['attributeValues'] ?? []);
|
|
|
|
|
|
|
|
|
|
$I->createInventory($product->id, $configs['productInventory'] ?? []);
|
|
|
|
|
|
|
|
|
|
return $product->refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $configs
|
|
|
|
|
* @param array $productStates
|
|
|
|
|
*
|
|
|
|
|
* @return \Webkul\Product\Models\Product
|
|
|
|
|
*/
|
|
|
|
|
private function haveVirtualProduct(array $configs = [], array $productStates = []): Product
|
|
|
|
|
{
|
|
|
|
|
$I = $this;
|
2020-01-28 15:49:26 +00:00
|
|
|
if (!in_array('virtual', $productStates)) {
|
2020-01-28 14:38:07 +00:00
|
|
|
$productStates = array_merge($productStates, ['virtual']);
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-17 13:42:47 +00:00
|
|
|
/** @var Product $product */
|
2020-01-28 14:38:07 +00:00
|
|
|
$product = $I->createProduct($configs['productAttributes'] ?? [], $productStates);
|
|
|
|
|
|
|
|
|
|
$I->createAttributeValues($product->id, $configs['attributeValues'] ?? []);
|
|
|
|
|
|
|
|
|
|
$I->createInventory($product->id, $configs['productInventory'] ?? []);
|
|
|
|
|
|
|
|
|
|
return $product->refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $configs
|
|
|
|
|
* @param array $productStates
|
|
|
|
|
*
|
|
|
|
|
* @return \Webkul\Product\Models\Product
|
|
|
|
|
*/
|
|
|
|
|
private function haveDownloadableProduct(array $configs = [], array $productStates = []): Product
|
|
|
|
|
{
|
|
|
|
|
$I = $this;
|
|
|
|
|
if (!in_array('downloadable', $productStates)) {
|
|
|
|
|
$productStates = array_merge($productStates, ['downloadable']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** @var Product $product */
|
|
|
|
|
$product = $I->createProduct($configs['productAttributes'] ?? [], $productStates);
|
|
|
|
|
|
|
|
|
|
$I->createAttributeValues($product->id, $configs['attributeValues'] ?? []);
|
|
|
|
|
|
|
|
|
|
$I->createDownloadableLink($product->id);
|
|
|
|
|
|
|
|
|
|
return $product->refresh();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $attributes
|
|
|
|
|
* @param array $states
|
|
|
|
|
*
|
|
|
|
|
* @return \Webkul\Product\Models\Product
|
|
|
|
|
*/
|
|
|
|
|
private function createProduct(array $attributes = [], array $states = []): Product
|
|
|
|
|
{
|
|
|
|
|
return factory(Product::class)->states($states)->create($attributes);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $productId
|
|
|
|
|
* @param array $inventoryConfig
|
|
|
|
|
*/
|
|
|
|
|
private function createInventory(int $productId, array $inventoryConfig = []): void
|
|
|
|
|
{
|
|
|
|
|
$I = $this;
|
|
|
|
|
$I->have(ProductInventory::class, array_merge($inventoryConfig, [
|
|
|
|
|
'product_id' => $productId,
|
2020-01-17 13:42:47 +00:00
|
|
|
'inventory_source_id' => 1,
|
|
|
|
|
]));
|
|
|
|
|
}
|
2020-01-28 14:38:07 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $productId
|
|
|
|
|
*/
|
|
|
|
|
private function createDownloadableLink(int $productId): void
|
|
|
|
|
{
|
|
|
|
|
$I = $this;
|
|
|
|
|
$link = $I->have(ProductDownloadableLink::class, [
|
|
|
|
|
'product_id' => $productId,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$I->have(ProductDownloadableLinkTranslation::class, [
|
|
|
|
|
'product_downloadable_link_id' => $link->id,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $productId
|
|
|
|
|
* @param array $attributeValues
|
|
|
|
|
*/
|
|
|
|
|
private function createAttributeValues(int $productId, array $attributeValues = []): void
|
2020-01-17 13:42:47 +00:00
|
|
|
{
|
|
|
|
|
$I = $this;
|
|
|
|
|
$productAttributeValues = [
|
|
|
|
|
'sku',
|
|
|
|
|
'url_key',
|
|
|
|
|
'tax_category_id',
|
|
|
|
|
'price',
|
|
|
|
|
'cost',
|
|
|
|
|
'name',
|
|
|
|
|
'new',
|
|
|
|
|
'visible_individually',
|
|
|
|
|
'featured',
|
|
|
|
|
'status',
|
|
|
|
|
'guest_checkout',
|
|
|
|
|
'short_description',
|
|
|
|
|
'description',
|
|
|
|
|
'meta_title',
|
|
|
|
|
'meta_keywords',
|
|
|
|
|
'meta_description',
|
|
|
|
|
'weight',
|
|
|
|
|
];
|
|
|
|
|
foreach ($productAttributeValues as $attribute) {
|
2020-01-28 14:38:07 +00:00
|
|
|
$data = ['product_id' => $productId];
|
2020-01-17 13:42:47 +00:00
|
|
|
if (array_key_exists($attribute, $attributeValues)) {
|
|
|
|
|
$fieldName = self::getAttributeFieldName($attribute);
|
2020-01-28 14:38:07 +00:00
|
|
|
if (!array_key_exists($fieldName, $data)) {
|
2020-01-17 13:42:47 +00:00
|
|
|
$data[$fieldName] = $attributeValues[$attribute];
|
|
|
|
|
} else {
|
|
|
|
|
$data = [$fieldName => $attributeValues[$attribute]];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$I->have(ProductAttributeValue::class, $data, $attribute);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|