public function setProduct($product, $amount = 1.0)
{
// Note: Only if the sectedprice is not set or its index does not exist
if (!isset($product->selectedPrice) || !isset($product->allPrices[$product->selectedPrice])) {
$bestIndex = null;
$fallbackIndex = null;
foreach ($product->allPrices as $i => $priceRule) {
if (
isset($priceRule['price_quantity_start']) &&
isset($priceRule['price_quantity_end'])
) {
$start = (int)$priceRule['price_quantity_start'];
$end = (int)$priceRule['price_quantity_end'];
// Search for the perfect range
if ($start <= $amount && ($end == 0 || $amount <= $end)) {
$bestIndex = $i;
break;
}
// Save Fallback to 0–0 if it appears
if ($start === 0 && $end === 0) {
$fallbackIndex = $i;
}
}
}
// choose the best available index
if ($bestIndex !== null) {
$product->selectedPrice = $bestIndex;
} elseif ($fallbackIndex !== null) {
$product->selectedPrice = $fallbackIndex;
} else {
// do not set anything - it will stay as it is
}
}
// Next original code ...
if (!empty($product->allPrices[$product->selectedPrice])) {
$this->productPrices = $product->allPrices[$product->selectedPrice];
$this->productCurrency = $this->productPrices['product_currency'];
$product->product_tax_id = $this->product_tax_id = $this->productPrices['product_tax_id'];
$this->product_discount_id = $this->productPrices['product_discount_id'];
}
$productVendorId = !empty($product->virtuemart_vendor_id) ? $product->virtuemart_vendor_id : 1;
$this->setVendorId($productVendorId);
$this->_cats = isset($product->categories) ? $product->categories : array();
$this->_product = $product;
$this->_product->amount = floatval($amount);
if (!isset($this->_product->quantity)) $this->_product->quantity = 1;
$this->_manufacturerId = !empty($product->virtuemart_manufacturer_id) ? $product->virtuemart_manufacturer_id : 0;
return $this->productPrices;
}
Page created in 1.949 seconds with 9 queries.