create('eav_entity_type', function (Blueprint $table) { $table->integer('entity_type_id'); $table->string('entity_type_code'); }); $schema->create('eav_attribute', function (Blueprint $table) { $table->integer('attribute_id'); $table->integer('entity_type_id'); $table->string('attribute_code'); $table->string('backend_type'); $table->string('frontend_label')->nullable(); $table->string('frontend_input')->nullable(); $table->integer('is_required')->default(0); $table->integer('is_user_defined')->default(0); }); $schema->create('customer_entity', function (Blueprint $table) { $table->integer('entity_id'); $table->string('email')->nullable(); $table->integer('is_active')->default(1); $table->integer('group_id')->default(1); $table->string('created_at')->nullable(); $table->string('updated_at')->nullable(); }); foreach (['varchar', 'int', 'datetime', 'text'] as $type) { $schema->create('customer_entity_'.$type, function (Blueprint $table) { $table->increments('value_id'); $table->integer('entity_id'); $table->integer('attribute_id'); $table->text('value')->nullable(); }); } $schema->create('customer_address_entity', function (Blueprint $table) { $table->integer('entity_id'); $table->integer('parent_id'); $table->integer('is_active')->default(1); $table->string('created_at')->nullable(); }); foreach (['varchar', 'int', 'text'] as $type) { $schema->create('customer_address_entity_'.$type, function (Blueprint $table) { $table->increments('value_id'); $table->integer('entity_id'); $table->integer('attribute_id'); $table->text('value')->nullable(); }); } $db = DB::connection($connection); $db->table('eav_entity_type')->insert([ ['entity_type_id' => self::CUSTOMER_TYPE_ID, 'entity_type_code' => 'customer'], ['entity_type_id' => self::ADDRESS_TYPE_ID, 'entity_type_code' => 'customer_address'], ]); $db->table('eav_attribute')->insert([ ['attribute_id' => 5, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'firstname', 'backend_type' => 'varchar'], ['attribute_id' => 7, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'lastname', 'backend_type' => 'varchar'], ['attribute_id' => 11, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'dob', 'backend_type' => 'datetime'], ['attribute_id' => 12, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'password_hash', 'backend_type' => 'varchar'], ['attribute_id' => 13, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'default_billing', 'backend_type' => 'int'], ['attribute_id' => 14, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'default_shipping', 'backend_type' => 'int'], ['attribute_id' => 18, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'gender', 'backend_type' => 'int'], ['attribute_id' => 88, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'telephone', 'backend_type' => 'varchar'], ['attribute_id' => 89, 'entity_type_id' => self::CUSTOMER_TYPE_ID, 'attribute_code' => 'source', 'backend_type' => 'int'], ['attribute_id' => 20, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'firstname', 'backend_type' => 'varchar'], ['attribute_id' => 21, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'lastname', 'backend_type' => 'varchar'], ['attribute_id' => 22, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'company', 'backend_type' => 'varchar'], ['attribute_id' => 23, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'street', 'backend_type' => 'text'], ['attribute_id' => 24, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'city', 'backend_type' => 'varchar'], ['attribute_id' => 25, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'region', 'backend_type' => 'varchar'], ['attribute_id' => 26, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'postcode', 'backend_type' => 'varchar'], ['attribute_id' => 27, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'country_id', 'backend_type' => 'varchar'], ['attribute_id' => 28, 'entity_type_id' => self::ADDRESS_TYPE_ID, 'attribute_code' => 'telephone', 'backend_type' => 'varchar'], ]); $schema->create('newsletter_subscriber', function (Blueprint $table) { $table->integer('subscriber_id'); $table->integer('store_id')->default(0); $table->string('change_status_at')->nullable(); $table->integer('customer_id')->default(0); $table->string('subscriber_email')->nullable(); $table->integer('subscriber_status')->default(0); $table->string('subscriber_confirm_code')->nullable(); }); $schema->create('sales_flat_order', function (Blueprint $table) { $table->integer('entity_id'); $table->string('increment_id')->nullable(); $table->integer('customer_id')->nullable(); $table->string('customer_email')->nullable(); $table->string('customer_firstname')->nullable(); $table->string('customer_lastname')->nullable(); $table->integer('customer_is_guest')->default(0); $table->string('status')->nullable(); $table->string('state')->nullable(); $table->integer('store_id')->nullable(); $table->string('base_currency_code')->nullable(); $table->string('order_currency_code')->nullable(); $table->string('store_currency_code')->nullable(); $table->decimal('grand_total', 12, 4)->default(0); $table->decimal('base_grand_total', 12, 4)->default(0); $table->decimal('subtotal', 12, 4)->default(0); $table->decimal('base_subtotal', 12, 4)->default(0); $table->decimal('subtotal_incl_tax', 12, 4)->nullable(); $table->decimal('base_subtotal_incl_tax', 12, 4)->nullable(); $table->decimal('tax_amount', 12, 4)->default(0); $table->decimal('base_tax_amount', 12, 4)->default(0); $table->decimal('discount_amount', 12, 4)->default(0); $table->decimal('base_discount_amount', 12, 4)->default(0); $table->decimal('shipping_amount', 12, 4)->default(0); $table->decimal('base_shipping_amount', 12, 4)->default(0); $table->decimal('shipping_incl_tax', 12, 4)->nullable(); $table->decimal('base_shipping_incl_tax', 12, 4)->nullable(); $table->decimal('shipping_tax_amount', 12, 4)->default(0); $table->decimal('base_shipping_tax_amount', 12, 4)->default(0); $table->string('shipping_method')->nullable(); $table->string('shipping_description')->nullable(); $table->string('coupon_code')->nullable(); $table->integer('mw_rewardpoint')->default(0); $table->decimal('mw_rewardpoint_discount', 12, 4)->default(0); $table->decimal('mw_rewardpoint_discount_show', 12, 4)->default(0); $table->decimal('amcheckoutfees_amount', 12, 4)->default(0); $table->decimal('base_amcheckoutfees_amount', 12, 4)->default(0); $table->integer('total_item_count')->default(0); $table->decimal('total_qty_ordered', 12, 4)->default(0); $table->decimal('total_invoiced', 12, 4)->default(0); $table->decimal('base_total_invoiced', 12, 4)->default(0); $table->string('created_at')->nullable(); $table->string('updated_at')->nullable(); }); $schema->create('sales_flat_order_item', function (Blueprint $table) { $table->integer('item_id'); $table->integer('order_id'); $table->integer('parent_item_id')->nullable(); $table->integer('product_id')->nullable(); $table->string('product_type')->nullable(); $table->string('sku')->nullable(); $table->string('name')->nullable(); $table->decimal('qty_ordered', 12, 4)->default(0); $table->decimal('qty_shipped', 12, 4)->default(0); $table->decimal('qty_invoiced', 12, 4)->default(0); $table->decimal('qty_canceled', 12, 4)->default(0); $table->decimal('qty_refunded', 12, 4)->default(0); $table->decimal('price', 12, 4)->default(0); $table->decimal('base_price', 12, 4)->default(0); $table->decimal('price_incl_tax', 12, 4)->nullable(); $table->decimal('base_price_incl_tax', 12, 4)->nullable(); $table->decimal('row_total', 12, 4)->default(0); $table->decimal('base_row_total', 12, 4)->default(0); $table->decimal('row_total_incl_tax', 12, 4)->nullable(); $table->decimal('base_row_total_incl_tax', 12, 4)->nullable(); $table->decimal('tax_amount', 12, 4)->default(0); $table->decimal('base_tax_amount', 12, 4)->default(0); $table->decimal('tax_percent', 12, 4)->default(0); $table->decimal('discount_amount', 12, 4)->default(0); $table->decimal('base_discount_amount', 12, 4)->default(0); $table->decimal('discount_percent', 12, 4)->default(0); $table->decimal('weight', 12, 4)->default(0); $table->decimal('row_weight', 12, 4)->default(0); $table->string('created_at')->nullable(); }); $schema->create('sales_flat_order_address', function (Blueprint $table) { $table->integer('entity_id'); $table->integer('parent_id'); $table->string('address_type')->nullable(); $table->string('firstname')->nullable(); $table->string('lastname')->nullable(); $table->string('company')->nullable(); $table->text('street')->nullable(); $table->string('city')->nullable(); $table->string('region')->nullable(); $table->string('postcode')->nullable(); $table->string('country_id')->nullable(); $table->string('telephone')->nullable(); $table->string('email')->nullable(); }); $schema->create('sales_flat_order_payment', function (Blueprint $table) { $table->integer('entity_id'); $table->integer('parent_id'); $table->string('method')->nullable(); $table->string('last_trans_id')->nullable(); $table->string('cc_type')->nullable(); $table->string('cc_last4')->nullable(); $table->decimal('amount_ordered', 12, 4)->nullable(); $table->decimal('base_amount_ordered', 12, 4)->nullable(); }); $schema->create('mw_reward_point_order', function (Blueprint $table) { $table->integer('order_id'); $table->integer('reward_point')->default(0); $table->integer('rewardpoint_sell_product')->default(0); $table->integer('earn_rewardpoint')->default(0); $table->float('money')->default(0); $table->string('reward_point_money_rate')->nullable(); }); self::createProductCatalog($connection); } /** * @param array $data */ public static function seedCustomer(string $connection, array $data): void { $entityId = (int) $data['entity_id']; DB::connection($connection)->table('customer_entity')->insert([ 'entity_id' => $entityId, 'email' => $data['email'], 'is_active' => $data['is_active'] ?? 1, 'group_id' => $data['group_id'] ?? 99, 'created_at' => $data['created_at'] ?? '2020-01-01 00:00:00', 'updated_at' => $data['updated_at'] ?? '2020-01-01 00:00:00', ]); $varchar = [ 5 => $data['firstname'] ?? null, 7 => $data['lastname'] ?? null, 12 => $data['password_hash'] ?? null, 88 => $data['telephone'] ?? null, ]; foreach ($varchar as $attributeId => $value) { if ($value === null || $value === '') { continue; } DB::connection($connection)->table('customer_entity_varchar')->insert([ 'entity_id' => $entityId, 'attribute_id' => $attributeId, 'value' => $value, ]); } if (isset($data['gender'])) { DB::connection($connection)->table('customer_entity_int')->insert([ 'entity_id' => $entityId, 'attribute_id' => 18, 'value' => $data['gender'], ]); } if (isset($data['source']) || isset($data['customer_source'])) { DB::connection($connection)->table('customer_entity_int')->insert([ 'entity_id' => $entityId, 'attribute_id' => 89, 'value' => $data['source'] ?? $data['customer_source'], ]); } if (! empty($data['default_billing'])) { DB::connection($connection)->table('customer_entity_int')->insert([ 'entity_id' => $entityId, 'attribute_id' => 13, 'value' => $data['default_billing'], ]); } if (! empty($data['default_shipping'])) { DB::connection($connection)->table('customer_entity_int')->insert([ 'entity_id' => $entityId, 'attribute_id' => 14, 'value' => $data['default_shipping'], ]); } if (! empty($data['dob'])) { DB::connection($connection)->table('customer_entity_datetime')->insert([ 'entity_id' => $entityId, 'attribute_id' => 11, 'value' => $data['dob'], ]); } } /** * @param array $data */ public static function seedAddress(string $connection, array $data): void { $entityId = (int) $data['entity_id']; DB::connection($connection)->table('customer_address_entity')->insert([ 'entity_id' => $entityId, 'parent_id' => $data['parent_id'], 'is_active' => 1, 'created_at' => '2020-01-01 00:00:00', ]); $map = [ 20 => ['table' => 'customer_address_entity_varchar', 'value' => $data['firstname'] ?? null], 21 => ['table' => 'customer_address_entity_varchar', 'value' => $data['lastname'] ?? null], 22 => ['table' => 'customer_address_entity_varchar', 'value' => $data['company'] ?? null], 23 => ['table' => 'customer_address_entity_text', 'value' => $data['street'] ?? null], 24 => ['table' => 'customer_address_entity_varchar', 'value' => $data['city'] ?? null], 25 => ['table' => 'customer_address_entity_varchar', 'value' => $data['region'] ?? null], 26 => ['table' => 'customer_address_entity_varchar', 'value' => $data['postcode'] ?? null], 27 => ['table' => 'customer_address_entity_varchar', 'value' => $data['country_id'] ?? null], 28 => ['table' => 'customer_address_entity_varchar', 'value' => $data['telephone'] ?? null], ]; foreach ($map as $attributeId => $item) { if ($item['value'] === null || $item['value'] === '') { continue; } DB::connection($connection)->table($item['table'])->insert([ 'entity_id' => $entityId, 'attribute_id' => $attributeId, 'value' => $item['value'], ]); } } /** * @param array $data */ public static function seedSubscriber(string $connection, array $data): void { DB::connection($connection)->table('newsletter_subscriber')->insert([ 'subscriber_id' => $data['subscriber_id'], 'store_id' => $data['store_id'] ?? 1, 'change_status_at' => $data['change_status_at'] ?? '2020-01-01 00:00:00', 'customer_id' => $data['customer_id'] ?? 0, 'subscriber_email' => $data['subscriber_email'], 'subscriber_status' => $data['subscriber_status'] ?? 1, 'subscriber_confirm_code' => $data['subscriber_confirm_code'] ?? 'confirmcode', ]); } /** * @param array $data */ public static function seedOrder(string $connection, array $data): void { $entityId = (int) $data['entity_id']; DB::connection($connection)->table('sales_flat_order')->insert([ 'entity_id' => $entityId, 'increment_id' => $data['increment_id'] ?? (string) (100000000 + $entityId), 'customer_id' => $data['customer_id'] ?? null, 'customer_email' => $data['customer_email'] ?? 'guest@example.com', 'customer_firstname' => $data['customer_firstname'] ?? 'Jane', 'customer_lastname' => $data['customer_lastname'] ?? 'Doe', 'customer_is_guest' => $data['customer_is_guest'] ?? 0, 'status' => $data['status'] ?? 'complete', 'state' => $data['state'] ?? 'complete', 'store_id' => $data['store_id'] ?? 1, 'base_currency_code' => $data['base_currency_code'] ?? 'USD', 'order_currency_code' => $data['order_currency_code'] ?? 'USD', 'store_currency_code' => $data['store_currency_code'] ?? 'USD', 'grand_total' => $data['grand_total'] ?? 110, 'base_grand_total' => $data['base_grand_total'] ?? 110, 'subtotal' => $data['subtotal'] ?? 100, 'base_subtotal' => $data['base_subtotal'] ?? 100, 'subtotal_incl_tax' => $data['subtotal_incl_tax'] ?? 100, 'base_subtotal_incl_tax' => $data['base_subtotal_incl_tax'] ?? 100, 'tax_amount' => $data['tax_amount'] ?? 0, 'base_tax_amount' => $data['base_tax_amount'] ?? 0, 'discount_amount' => $data['discount_amount'] ?? 0, 'base_discount_amount' => $data['base_discount_amount'] ?? 0, 'shipping_amount' => $data['shipping_amount'] ?? 10, 'base_shipping_amount' => $data['base_shipping_amount'] ?? 10, 'shipping_incl_tax' => $data['shipping_incl_tax'] ?? 10, 'base_shipping_incl_tax' => $data['base_shipping_incl_tax'] ?? 10, 'shipping_tax_amount' => $data['shipping_tax_amount'] ?? 0, 'base_shipping_tax_amount'=> $data['base_shipping_tax_amount'] ?? 0, 'shipping_method' => $data['shipping_method'] ?? 'flatrate_flatrate', 'shipping_description' => $data['shipping_description'] ?? 'Flat Rate - Fixed', 'coupon_code' => $data['coupon_code'] ?? null, 'mw_rewardpoint' => $data['mw_rewardpoint'] ?? 0, 'mw_rewardpoint_discount' => $data['mw_rewardpoint_discount'] ?? 0, 'mw_rewardpoint_discount_show' => $data['mw_rewardpoint_discount_show'] ?? 0, 'amcheckoutfees_amount' => $data['amcheckoutfees_amount'] ?? 0, 'base_amcheckoutfees_amount' => $data['base_amcheckoutfees_amount'] ?? 0, 'total_item_count' => $data['total_item_count'] ?? 1, 'total_qty_ordered' => $data['total_qty_ordered'] ?? 1, 'total_invoiced' => $data['total_invoiced'] ?? 110, 'base_total_invoiced' => $data['base_total_invoiced'] ?? 110, 'created_at' => $data['created_at'] ?? '2021-06-01 12:00:00', 'updated_at' => $data['updated_at'] ?? '2021-06-01 12:00:00', ]); foreach ($data['items'] ?? [] as $item) { self::seedOrderItem($connection, array_merge(['order_id' => $entityId], $item)); } foreach ($data['addresses'] ?? [] as $address) { self::seedOrderAddress($connection, array_merge(['parent_id' => $entityId], $address)); } if (! empty($data['payment'])) { self::seedOrderPayment($connection, array_merge(['parent_id' => $entityId], $data['payment'])); } if (! empty($data['reward_point_order'])) { self::seedRewardPointOrder($connection, array_merge(['order_id' => $entityId], $data['reward_point_order'])); } } /** * @param array $data */ public static function seedRewardPointOrder(string $connection, array $data): void { DB::connection($connection)->table('mw_reward_point_order')->insert([ 'order_id' => $data['order_id'], 'reward_point' => $data['reward_point'] ?? 0, 'rewardpoint_sell_product' => $data['rewardpoint_sell_product'] ?? 0, 'earn_rewardpoint' => $data['earn_rewardpoint'] ?? 0, 'money' => $data['money'] ?? 0, 'reward_point_money_rate' => $data['reward_point_money_rate'] ?? '100/1', ]); } /** * @param array $data */ public static function seedOrderItem(string $connection, array $data): void { DB::connection($connection)->table('sales_flat_order_item')->insert([ 'item_id' => $data['item_id'], 'order_id' => $data['order_id'], 'parent_item_id' => $data['parent_item_id'] ?? null, 'product_id' => $data['product_id'] ?? null, 'product_type' => $data['product_type'] ?? 'simple', 'sku' => $data['sku'] ?? 'SKU-1', 'name' => $data['name'] ?? 'Test Product', 'qty_ordered' => $data['qty_ordered'] ?? 1, 'qty_shipped' => $data['qty_shipped'] ?? 1, 'qty_invoiced' => $data['qty_invoiced'] ?? 1, 'qty_canceled' => $data['qty_canceled'] ?? 0, 'qty_refunded' => $data['qty_refunded'] ?? 0, 'price' => $data['price'] ?? 100, 'base_price' => $data['base_price'] ?? 100, 'price_incl_tax' => $data['price_incl_tax'] ?? 100, 'base_price_incl_tax' => $data['base_price_incl_tax'] ?? 100, 'row_total' => $data['row_total'] ?? 100, 'base_row_total' => $data['base_row_total'] ?? 100, 'row_total_incl_tax' => $data['row_total_incl_tax'] ?? 100, 'base_row_total_incl_tax' => $data['base_row_total_incl_tax'] ?? 100, 'tax_amount' => $data['tax_amount'] ?? 0, 'base_tax_amount' => $data['base_tax_amount'] ?? 0, 'tax_percent' => $data['tax_percent'] ?? 0, 'discount_amount' => $data['discount_amount'] ?? 0, 'base_discount_amount' => $data['base_discount_amount'] ?? 0, 'discount_percent' => $data['discount_percent'] ?? 0, 'weight' => $data['weight'] ?? 1, 'row_weight' => $data['row_weight'] ?? 1, 'created_at' => $data['created_at'] ?? '2021-06-01 12:00:00', ]); } /** * @param array $data */ public static function seedOrderAddress(string $connection, array $data): void { DB::connection($connection)->table('sales_flat_order_address')->insert([ 'entity_id' => $data['entity_id'], 'parent_id' => $data['parent_id'], 'address_type' => $data['address_type'] ?? 'billing', 'firstname' => $data['firstname'] ?? 'Jane', 'lastname' => $data['lastname'] ?? 'Doe', 'company' => $data['company'] ?? null, 'street' => $data['street'] ?? '123 Main St', 'city' => $data['city'] ?? 'Austin', 'region' => $data['region'] ?? 'TX', 'postcode' => $data['postcode'] ?? '78701', 'country_id' => $data['country_id'] ?? 'US', 'telephone' => $data['telephone'] ?? '5551112222', 'email' => $data['email'] ?? null, ]); } /** * @param array $data */ public static function seedOrderPayment(string $connection, array $data): void { DB::connection($connection)->table('sales_flat_order_payment')->insert([ 'entity_id' => $data['entity_id'], 'parent_id' => $data['parent_id'], 'method' => $data['method'] ?? 'paypal_express', 'last_trans_id' => $data['last_trans_id'] ?? null, 'cc_type' => $data['cc_type'] ?? null, 'cc_last4' => $data['cc_last4'] ?? null, 'amount_ordered' => $data['amount_ordered'] ?? 110, 'base_amount_ordered' => $data['base_amount_ordered'] ?? 110, ]); } public static function createProductCatalog(string $connection): void { $schema = Schema::connection($connection); $db = DB::connection($connection); $db->table('eav_entity_type')->insert([ ['entity_type_id' => self::CATEGORY_TYPE_ID, 'entity_type_code' => 'catalog_category'], ['entity_type_id' => self::PRODUCT_TYPE_ID, 'entity_type_code' => 'catalog_product'], ]); $schema->create('catalog_eav_attribute', function (Blueprint $table) { $table->integer('attribute_id'); $table->integer('is_searchable')->default(0); $table->integer('is_filterable')->default(0); $table->integer('is_comparable')->default(0); $table->integer('is_configurable')->default(0); $table->integer('position')->default(0); }); $schema->create('eav_attribute_option', function (Blueprint $table) { $table->integer('option_id'); $table->integer('attribute_id'); $table->integer('sort_order')->default(0); }); $schema->create('eav_attribute_option_value', function (Blueprint $table) { $table->increments('value_id'); $table->integer('option_id'); $table->integer('store_id')->default(0); $table->string('value')->nullable(); }); $schema->create('catalog_product_entity', function (Blueprint $table) { $table->integer('entity_id'); $table->string('sku')->nullable(); $table->string('type_id')->default('simple'); $table->integer('attribute_set_id')->default(4); $table->string('created_at')->nullable(); $table->string('updated_at')->nullable(); }); foreach (['varchar', 'int', 'decimal', 'text', 'datetime'] as $type) { $schema->create('catalog_product_entity_'.$type, function (Blueprint $table) { $table->increments('value_id'); $table->integer('entity_id'); $table->integer('attribute_id'); $table->integer('store_id')->default(0); $table->text('value')->nullable(); }); } $schema->create('catalog_product_entity_media_gallery', function (Blueprint $table) { $table->increments('value_id'); $table->integer('entity_id'); $table->string('value')->nullable(); }); $schema->create('catalog_product_entity_media_gallery_value', function (Blueprint $table) { $table->integer('value_id'); $table->integer('store_id')->default(0); $table->string('label')->nullable(); $table->integer('position')->default(0); $table->integer('disabled')->default(0); }); $schema->create('cataloginventory_stock_item', function (Blueprint $table) { $table->increments('item_id'); $table->integer('product_id'); $table->integer('stock_id')->default(1); $table->decimal('qty', 12, 4)->default(0); $table->integer('is_in_stock')->default(0); }); $schema->create('catalog_category_entity', function (Blueprint $table) { $table->integer('entity_id'); $table->string('path')->nullable(); }); $schema->create('catalog_category_entity_varchar', function (Blueprint $table) { $table->increments('value_id'); $table->integer('entity_id'); $table->integer('attribute_id'); $table->integer('store_id')->default(0); $table->string('value')->nullable(); }); $schema->create('catalog_category_product', function (Blueprint $table) { $table->integer('category_id'); $table->integer('product_id'); $table->integer('position')->default(0); }); $schema->create('catalog_product_option', function (Blueprint $table) { $table->increments('option_id'); $table->integer('product_id'); $table->string('type')->nullable(); $table->integer('is_require')->default(0); $table->integer('sort_order')->default(0); }); $schema->create('catalog_product_option_title', function (Blueprint $table) { $table->increments('option_title_id'); $table->integer('option_id'); $table->integer('store_id')->default(0); $table->string('title')->nullable(); }); $schema->create('catalog_product_option_type_value', function (Blueprint $table) { $table->increments('option_type_id'); $table->integer('option_id'); $table->string('sku')->nullable(); $table->integer('sort_order')->default(0); }); $schema->create('catalog_product_option_type_title', function (Blueprint $table) { $table->increments('option_type_title_id'); $table->integer('option_type_id'); $table->integer('store_id')->default(0); $table->string('title')->nullable(); }); $schema->create('catalog_product_option_type_price', function (Blueprint $table) { $table->increments('option_type_price_id'); $table->integer('option_type_id'); $table->integer('store_id')->default(0); $table->decimal('price', 12, 4)->default(0); $table->string('price_type')->default('fixed'); }); $db->table('eav_attribute')->insert([ ['attribute_id' => self::ATTR_PRODUCT_NAME, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'name', 'backend_type' => 'varchar', 'frontend_label' => 'Name', 'frontend_input' => 'text', 'is_required' => 1, 'is_user_defined' => 0], ['attribute_id' => self::ATTR_PRODUCT_DESCRIPTION, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'description', 'backend_type' => 'text', 'frontend_label' => 'Description', 'frontend_input' => 'textarea', 'is_required' => 0, 'is_user_defined' => 0], ['attribute_id' => self::ATTR_PRODUCT_SHORT_DESCRIPTION, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'short_description', 'backend_type' => 'text', 'frontend_label' => 'Short Description', 'frontend_input' => 'textarea', 'is_required' => 0, 'is_user_defined' => 0], ['attribute_id' => self::ATTR_PRODUCT_PRICE, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'price', 'backend_type' => 'decimal', 'frontend_label' => 'Price', 'frontend_input' => 'price', 'is_required' => 1, 'is_user_defined' => 0], ['attribute_id' => self::ATTR_PRODUCT_SPECIAL_PRICE, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'special_price', 'backend_type' => 'decimal', 'frontend_label' => 'Special Price', 'frontend_input' => 'price', 'is_required' => 0, 'is_user_defined' => 0], ['attribute_id' => self::ATTR_PRODUCT_COST, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'cost', 'backend_type' => 'decimal', 'frontend_label' => 'Cost', 'frontend_input' => 'price', 'is_required' => 0, 'is_user_defined' => 0], ['attribute_id' => self::ATTR_PRODUCT_WEIGHT, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'weight', 'backend_type' => 'decimal', 'frontend_label' => 'Weight', 'frontend_input' => 'weight', 'is_required' => 0, 'is_user_defined' => 0], ['attribute_id' => self::ATTR_PRODUCT_STATUS, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'status', 'backend_type' => 'int', 'frontend_label' => 'Status', 'frontend_input' => 'select', 'is_required' => 1, 'is_user_defined' => 0], ['attribute_id' => self::ATTR_PRODUCT_META_TITLE, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'meta_title', 'backend_type' => 'varchar', 'frontend_label' => 'Meta Title', 'frontend_input' => 'text', 'is_required' => 0, 'is_user_defined' => 0], ['attribute_id' => self::ATTR_PRODUCT_META_KEYWORD, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'meta_keyword', 'backend_type' => 'text', 'frontend_label' => 'Meta Keywords', 'frontend_input' => 'textarea', 'is_required' => 0, 'is_user_defined' => 0], ['attribute_id' => self::ATTR_PRODUCT_META_DESCRIPTION, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'meta_description', 'backend_type' => 'varchar', 'frontend_label' => 'Meta Description', 'frontend_input' => 'textarea', 'is_required' => 0, 'is_user_defined' => 0], ['attribute_id' => self::ATTR_PRODUCT_URL_KEY, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'url_key', 'backend_type' => 'varchar', 'frontend_label' => 'URL Key', 'frontend_input' => 'text', 'is_required' => 0, 'is_user_defined' => 0], ['attribute_id' => self::ATTR_PRODUCT_HAIR_COLORR, 'entity_type_id' => self::PRODUCT_TYPE_ID, 'attribute_code' => 'hair_colorr', 'backend_type' => 'int', 'frontend_label' => 'Hair Color', 'frontend_input' => 'select', 'is_required' => 0, 'is_user_defined' => 1], ['attribute_id' => self::ATTR_CATEGORY_NAME, 'entity_type_id' => self::CATEGORY_TYPE_ID, 'attribute_code' => 'name', 'backend_type' => 'varchar', 'frontend_label' => 'Name', 'frontend_input' => 'text', 'is_required' => 1, 'is_user_defined' => 0], ]); $db->table('catalog_eav_attribute')->insert([ ['attribute_id' => self::ATTR_PRODUCT_HAIR_COLORR, 'is_searchable' => 1, 'is_filterable' => 1, 'is_comparable' => 0, 'is_configurable' => 0, 'position' => 10], ]); $db->table('eav_attribute_option')->insert([ ['option_id' => self::HAIR_COLORR_OPTION_ID, 'attribute_id' => self::ATTR_PRODUCT_HAIR_COLORR, 'sort_order' => 1], ]); $db->table('eav_attribute_option_value')->insert([ ['option_id' => self::HAIR_COLORR_OPTION_ID, 'store_id' => 0, 'value' => 'Natural Black'], ]); } /** * @param array $data */ public static function seedProduct(string $connection, array $data): void { $entityId = (int) $data['entity_id']; $db = DB::connection($connection); $db->table('catalog_product_entity')->insert([ 'entity_id' => $entityId, 'sku' => $data['sku'] ?? 'SKU-'.$entityId, 'type_id' => $data['type_id'] ?? 'simple', 'attribute_set_id' => $data['attribute_set_id'] ?? 4, 'created_at' => $data['created_at'] ?? '2020-01-01 00:00:00', 'updated_at' => $data['updated_at'] ?? '2020-01-01 00:00:00', ]); $map = [ 'name' => [self::ATTR_PRODUCT_NAME, 'varchar'], 'description' => [self::ATTR_PRODUCT_DESCRIPTION, 'text'], 'short_description' => [self::ATTR_PRODUCT_SHORT_DESCRIPTION, 'text'], 'price' => [self::ATTR_PRODUCT_PRICE, 'decimal'], 'special_price' => [self::ATTR_PRODUCT_SPECIAL_PRICE, 'decimal'], 'cost' => [self::ATTR_PRODUCT_COST, 'decimal'], 'weight' => [self::ATTR_PRODUCT_WEIGHT, 'decimal'], 'status' => [self::ATTR_PRODUCT_STATUS, 'int'], 'meta_title' => [self::ATTR_PRODUCT_META_TITLE, 'varchar'], 'meta_keyword' => [self::ATTR_PRODUCT_META_KEYWORD, 'text'], 'meta_description' => [self::ATTR_PRODUCT_META_DESCRIPTION, 'varchar'], 'url_key' => [self::ATTR_PRODUCT_URL_KEY, 'varchar'], 'hair_colorr' => [self::ATTR_PRODUCT_HAIR_COLORR, 'int'], ]; $values = $data['eav'] ?? $data; foreach ($map as $code => [$attributeId, $type]) { if (! array_key_exists($code, $values) || $values[$code] === null || $values[$code] === '') { continue; } $db->table('catalog_product_entity_'.$type)->insert([ 'entity_id' => $entityId, 'attribute_id' => $attributeId, 'store_id' => 0, 'value' => $values[$code], ]); } if (! array_key_exists('status', $values)) { $db->table('catalog_product_entity_int')->insert([ 'entity_id' => $entityId, 'attribute_id' => self::ATTR_PRODUCT_STATUS, 'store_id' => 0, 'value' => 1, ]); } foreach ($data['images'] ?? [] as $position => $path) { $valueId = $db->table('catalog_product_entity_media_gallery')->insertGetId([ 'entity_id' => $entityId, 'value' => $path, ]); $db->table('catalog_product_entity_media_gallery_value')->insert([ 'value_id' => $valueId, 'store_id' => 0, 'label' => '', 'position' => $position, 'disabled' => 0, ]); } if (isset($data['qty']) || isset($data['is_in_stock'])) { $db->table('cataloginventory_stock_item')->insert([ 'product_id' => $entityId, 'stock_id' => 1, 'qty' => $data['qty'] ?? 0, 'is_in_stock' => $data['is_in_stock'] ?? 0, ]); } foreach ($data['categories'] ?? [] as $category) { $categoryId = (int) $category['entity_id']; if (! $db->table('catalog_category_entity')->where('entity_id', $categoryId)->exists()) { $db->table('catalog_category_entity')->insert([ 'entity_id' => $categoryId, 'path' => $category['path'] ?? (string) $categoryId, ]); $db->table('catalog_category_entity_varchar')->insert([ 'entity_id' => $categoryId, 'attribute_id' => self::ATTR_CATEGORY_NAME, 'store_id' => 0, 'value' => $category['name'] ?? 'Category', ]); } $db->table('catalog_category_product')->insert([ 'category_id' => $categoryId, 'product_id' => $entityId, 'position' => $category['position'] ?? 0, ]); } foreach ($data['options'] ?? [] as $option) { self::seedProductOption($connection, array_merge($option, ['product_id' => $entityId])); } } /** * @param array $data */ public static function seedProductOption(string $connection, array $data): void { $db = DB::connection($connection); $optionId = $db->table('catalog_product_option')->insertGetId([ 'product_id' => $data['product_id'], 'type' => $data['type'] ?? 'drop_down', 'is_require' => $data['is_require'] ?? 1, 'sort_order' => $data['sort_order'] ?? 0, ]); $db->table('catalog_product_option_title')->insert([ 'option_id' => $optionId, 'store_id' => 0, 'title' => $data['title'] ?? 'Option', ]); foreach ($data['values'] ?? [] as $index => $value) { $typeId = $db->table('catalog_product_option_type_value')->insertGetId([ 'option_id' => $optionId, 'sku' => $value['sku'] ?? '', 'sort_order' => $value['sort_order'] ?? $index, ]); $db->table('catalog_product_option_type_title')->insert([ 'option_type_id' => $typeId, 'store_id' => 0, 'title' => $value['title'] ?? '', ]); $db->table('catalog_product_option_type_price')->insert([ 'option_type_id' => $typeId, 'store_id' => 0, 'price' => $value['price'] ?? 0, 'price_type' => $value['price_type'] ?? 'fixed', ]); } } }