extend order item and cart item factories

This commit is contained in:
Annika Wolff 2020-06-11 10:51:40 +02:00
parent 497412abfd
commit a196b7ac02
2 changed files with 34 additions and 13 deletions

View File

@ -3,12 +3,34 @@
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use Faker\Generator as Faker;
use Webkul\Checkout\Models\Cart;
use Webkul\Checkout\Models\CartItem;
use Webkul\Product\Models\Product;
$factory->define(CartItem::class, function (Faker $faker) {
$factory->define(CartItem::class, function (Faker $faker, array $attributes) {
$now = date("Y-m-d H:i:s");
if (isset($attributes['product_id'])) {
$product = Product::where('id', $attributes['product_id'])->first();
} else {
$product = factory(Product::class)->create();
}
$fallbackPrice = $faker->randomFloat(4, 0, 1000);
return [
'quantity' => 1,
'sku' => $product->sku,
'type' => $product->type,
'name' => $product->name,
'price' => $product->price ?? $fallbackPrice,
'base_price' => $product->price ?? $fallbackPrice,
'total' => $product->price ?? $fallbackPrice,
'base_total' => $product->price ?? $fallbackPrice,
'product_id' => $product->id,
'cart_id' => function () {
return factory(Cart::class)->create()->id;
},
'created_at' => $now,
'updated_at' => $now,
];

View File

@ -3,8 +3,8 @@
/** @var \Illuminate\Database\Eloquent\Factory $factory */
use Faker\Generator as Faker;
use Webkul\Product\Models\Product;
use Webkul\Sales\Models\Order;
use Webkul\Product\Models\Product;
use Webkul\Sales\Models\OrderItem;
$factory->define(OrderItem::class, function (Faker $faker, array $attributes) {
@ -16,14 +16,16 @@ $factory->define(OrderItem::class, function (Faker $faker, array $attributes) {
$product = factory(Product::class)->create();
}
$fallbackPrice = $faker->randomFloat(4, 0, 1000);
return [
'sku' => $product->sku,
'type' => $product->type,
'name' => $product->name,
'price' => $product->price,
'base_price' => $product->price,
'total' => $product->price,
'base_total' => $product->price,
'sku' => $product->sku,
'type' => $product->type,
'name' => $product->name,
'price' => $product->price ?? $fallbackPrice,
'base_price' => $product->price ?? $fallbackPrice,
'total' => $product->price ?? $fallbackPrice,
'base_total' => $product->price ?? $fallbackPrice,
'product_id' => $product->id,
'qty_ordered' => 1,
'qty_shipped' => 0,
@ -37,7 +39,4 @@ $factory->define(OrderItem::class, function (Faker $faker, array $attributes) {
'updated_at' => $now,
'product_type' => Product::class,
];
});
});