I'm trying to create a function that will replicate/clone/duplicate a product including all it's properties and it's shipping options.
However, I succeeded to duplicate the product but the shipping options are not replicated. See my codes below;
Any help will be highly appreciated
Thanks
public function CreateProductPost(Request $request){
if (Auth::user()->vendor == false) {
return redirect()->route('profile');
}
if ($request->name == null) {
session()->flash('errormessage','Product name is required');
return redirect()->back()->withInput();
}
if (mb_strlen($request->name) > 60) {
session()->flash('errormessage','Product name cannot be longer than 60 characters.');
return redirect()->back()->withInput();
}
if ($request->category_id == null) {
session()->flash('errormessage','Product category is required');
return redirect()->back()->withInput();
}
if ($request->price == null) {
session()->flash('errormessage','Product price is required');
return redirect()->back()->withInput();
}
if (!is_numeric($request->price) || $request->price < 0.01) {
session()->flash('errormessage','Invalid price');
return redirect()->back()->withInput();
}
if ($request->currency == null) {
session()->flash('errormessage','Product currency is required');
return redirect()->back()->withInput();
}
if (!in_array($request->currency, ['usd', 'eur', 'gbp'])) {
session()->flash('errormessage','Invalid currency');
return redirect()->back()->withInput();
}
if ($request->inventory == null) {
session()->flash('errormessage','Product inventory is required');
return redirect()->back()->withInput();
}
if (!is_numeric($request->inventory) || $request->inventory < 1) {
session()->flash('errormessage','Invalid inventory');
return redirect()->back()->withInput();
}
$category = Category::where('id',$request->category_id)->first();
if ($category == null) {
session()->flash('errormessage','Invalid category, choose from given categories');
return redirect()->back()->withInput();
}
if ($request->description == null) {
session()->flash('errormessage','Product description is required');
return redirect()->back()->withInput();
}
if ($request->fromc == null) {
session()->flash('errormessage','Product ships from is required');
return redirect()->back()->withInput();
}
if ($request->tocount == null) {
session()->flash('errormessage','Product ships to is required');
return redirect()->back()->withInput();
}
if ($request->hasFile('image')) {
$file = $request->file('image');
$size = $request->file('image')->getSize();
if ($size > 500000) {
session()->flash('errormessage','Image is too large');
return redirect()->back()->withInput();
}
if (substr($file->getMimeType(), 0, 5) !== 'image') {
session()->flash('errormessage','File is not an image');
return redirect()->back()->withInput();
}
if (!$file->isValid()) {
session()->flash('errormessage','Image is not valid');
return redirect()->back()->withInput();
}
}
$fe = $request->finalize_early && Auth::user()->fep;
$shippingoptions = [];
$shipping = $request->shipping;
foreach ($shipping as $i => $opt) {
if (1 == $i and (empty($opt['desc']) or empty($opt['days']) or empty($opt['price']))) {
session()->flash('errormessage','At least one shipping option is required, fill all the fields.');
return redirect()->back()->withInput();
}
if (!empty($opt['desc']) and !empty($opt['days']) and !empty($opt['price'])) {
$shippingoptions[] = $opt;
}
}
$product = new Product;
$product->name = $request->name;
$product->uniqueid = random_int(10000, 99999);
$product->category_id = $category->id;
$product->description = $request->description;
$product->refund_policy = $request->refund_policy;
$product->fromc = $request->fromc;
$product->tocount = $request->tocount;
$product->price = $request->price;
$product->currency = $request->currency;
$product->inventory = $request->inventory;
if ($request->image !== null) {
$product->image = $request->image->store('uploads','public');
}
$product->buyout = 0;
$product->fe = $fe;
$product->seller_id = Auth::user()->id;
$product->save();
foreach ($shippingoptions as $opt) {
$so = new ShippingOption();
$so->product_id = $product->id;
$so->desc = $opt['desc'];
$so->days = $opt['days'];
$so->price = $opt['price'];
$so->save();
}
session()->flash('successmessage','Product successfully created');
return redirect()->route('products');
}
function DuplicateProductPost($uniqueid, Request $request){
$product = Product::where('uniqueid',$uniqueid)->first();
if ($product == null) {
return redirect()->route('products');
}
if (Auth::user()->id !== $product->seller->id) {
return redirect()->route('products');
}
$newProduct = $product->replicate();
$newProduct->uniqueid = random_int(10000, 99999);
$newProduct->save();
session()->flash('successmessage','Product successfully duplicated');
return redirect()->route('products');
}
Any help will be highly appreciated
Thanks
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire