/** * shopp_product_set_addon_options - Creates a complete set of addon product options on a specified product, by letting you * specify the set of options types, and corresponding options. This function will create new addon options in the database and * will attach them to the specified product. * * @api * @since 1.2 * * @param int $product (required) The product id of the product that you wish to add the addon options to. * @param array $options (Description...) A two dimensional array describing the addon options. * The outer array is keyed on the name of the option type (Framing, Matting, Glass, etc.) * The inner contains the corresponding option values. * Ex. $options = array( 'Framing' => array('Wood', 'Gold'), 'Glass' => array('Anti-glare', 'UV Protectant') ); * @return array addon Price objects that have been created on the product. * **/ function shopp_product_set_addon_options($product = false, $options = array(), $summary = 'save') { if (!$product || empty($options)) { shopp_debug(__FUNCTION__ . " failed: Missing required parameters."); return false; } $Product = new ShoppProduct($product); if (empty($Product->id)) { shopp_debug(__FUNCTION__ . " failed: Product not found for product id {$product}."); return false; } $Product->load_data(array('summary')); // clean up old variations $table = ShoppDatabaseObject::tablename(ShoppPrice::$table); db::query("DELETE FROM {$table} WHERE product={$product} AND context='addon'"); $prices = array(); $mapping = array(); foreach ($options as $type => $opts) { foreach ($opts as $index => $option) { $addon = array($type => $option); $Price = new ShoppPrice(); $Price->type = 'Shipped'; $Price->product = $product; $Price->context = 'addon'; $Price->sortorder = $index + 2; // default price sort order is 1, start at minimum 2 #2847 list($Price->optionkey, $Price->options, $Price->label, $mapping) = $Product->optionmap($addon, $options, 'addon'); $Price->save(); shopp_set_meta($Price->id, 'price', 'options', $Price->options); $prices[] = $Price; } } $metaopts = shopp_product_meta($product, 'options'); $metaopts['a'] = array(); $i = 1; foreach ($options as $optname => $option) { if (!isset($metaopts['a'][$i])) { $metaopts['a'][$i] = array('id' => $i, 'name' => $optname, 'options' => array()); } foreach ($option as $value) { $metaopts['a'][$i]['options'][$mapping[$optname][$value]] = array('id' => $mapping[$optname][$value], 'name' => $value, 'linked' => "off"); } $i++; } shopp_set_product_meta($product, 'options', $metaopts); $Product->addons = "on"; if ('save' == $summary) { $Product->sumup(); } return $prices; }