first upload all files
This commit is contained in:
52
Modules/Product/Http/Controllers/ProductPriceController.php
Normal file
52
Modules/Product/Http/Controllers/ProductPriceController.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Product\Http\Controllers;
|
||||
|
||||
use Modules\Cart\CartItem;
|
||||
use Darryldecode\Cart\ItemCollection;
|
||||
use Modules\Product\Entities\Product;
|
||||
use Modules\Product\Services\ChosenProductOptions;
|
||||
|
||||
class ProductPriceController
|
||||
{
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$product = Product::queryWithoutEagerRelations()
|
||||
->select('id')
|
||||
->withPrice()
|
||||
->findOrFail($id);
|
||||
|
||||
$variantPrice = $this->cartItem($product, request('options', []))
|
||||
->total()
|
||||
->convertToCurrentCurrency()
|
||||
->format();
|
||||
|
||||
return product_price_formatted($product, function ($price) use ($product, $variantPrice) {
|
||||
if (! $product->hasSpecialPrice()) {
|
||||
return $variantPrice;
|
||||
}
|
||||
|
||||
return "{$variantPrice} <span class='previous-price'>{$price}</span>";
|
||||
});
|
||||
}
|
||||
|
||||
private function cartItem(Product $product, array $options)
|
||||
{
|
||||
$chosenOptions = new ChosenProductOptions($product, $options);
|
||||
|
||||
return new CartItem(new ItemCollection([
|
||||
'id' => $product->id,
|
||||
'quantity' => 1,
|
||||
'attributes' => [
|
||||
'product' => $product,
|
||||
'options' => $chosenOptions->getEntities(),
|
||||
],
|
||||
]));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user