Add default addons functionality to customer vehicles
Introduced a method to automatically add default addons to customer vehicles based on predefined product IDs. Updated the vehicle creation workflow to include the addition of these default addons. Adjusted type casting and comments for better clarity and functionality.
This commit is contained in:
@@ -77,7 +77,7 @@ class customer_vehicles_o extends db
|
||||
|
||||
/**
|
||||
* Get the available addons for the vehicle
|
||||
* @return array{customer_vehicles_addons_o}
|
||||
* @return array{product_options_o}
|
||||
* @throws Exception If the object is not selected
|
||||
*/
|
||||
public function getAvailableAddons(): array
|
||||
@@ -89,13 +89,44 @@ class customer_vehicles_o extends db
|
||||
return (bool)$addon['product']['subscription_allowed'];
|
||||
});
|
||||
return array_map(function ($addon) {
|
||||
return (new product_options_o())->select($addon['id']);
|
||||
return (new product_options_o())->select((int)$addon['id']);
|
||||
}, $available_addons);
|
||||
}
|
||||
|
||||
public function structure(): void
|
||||
/**
|
||||
* Add the default addons to the vehicle
|
||||
* @return void
|
||||
* @throws Exception If the object is not selected
|
||||
* @throws Exception If the there is an error adding the addon
|
||||
* @throws Exception If the there is an error selecting the addon
|
||||
* @throws Exception If the there is an error selecting the vehicle
|
||||
*/
|
||||
public function addDefaultAddons(): void
|
||||
{
|
||||
$this->setTable('customer_vehicles');
|
||||
self::requireSelected();
|
||||
// If any of the available addons have the product id in the $automatic_addon_product_ids array, add it to the vehicle
|
||||
$automatic_addon_product_ids = [
|
||||
23, // Spot Free- Varevogn
|
||||
24, // Spot Free- Lastbil
|
||||
];
|
||||
$available_addons = self::getAvailableAddons();
|
||||
/** @var product_options_o $addon */
|
||||
foreach ( $available_addons as $addon ) {
|
||||
//echo 'Vehicle id: ' . $this->id . PHP_EOL;
|
||||
//echo 'Addon id: ' . $addon->id . PHP_EOL;
|
||||
//echo 'Primary product id: ' . $addon->product_id->value() . PHP_EOL;
|
||||
//echo 'Addon product id: ' . $addon->option_id->value() . PHP_EOL;
|
||||
//echo 'Is in array: ' . (in_array((int)$addon->option_id->value(), $automatic_addon_product_ids) ? 'true' : 'false') . PHP_EOL;
|
||||
//print_r($available_addons);
|
||||
if (in_array((int)$addon->option_id->value(), $automatic_addon_product_ids)) {
|
||||
// Add the addon to the vehicle
|
||||
(new customer_vehicles_addons_o())->add(
|
||||
(int)$this->id,
|
||||
(int)$addon->id,
|
||||
(int)1,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -112,7 +143,7 @@ class customer_vehicles_o extends db
|
||||
]);
|
||||
self::select((int)$tmp_id);
|
||||
self::objectChanged();
|
||||
|
||||
self::addDefaultAddons();
|
||||
}
|
||||
|
||||
public function objectChanged(): void
|
||||
@@ -120,6 +151,10 @@ class customer_vehicles_o extends db
|
||||
// Since the customer_vehicles object is not cached, there is no need to invalidate the cache
|
||||
}
|
||||
|
||||
public function structure(): void
|
||||
{
|
||||
$this->setTable('customer_vehicles');
|
||||
}
|
||||
|
||||
public function getObjectProperties(): void
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user