/** * Get a specific Price object * * @api * @since 1.2 * * @param mixed $variant the id of the variant, or array('product'=>int, 'option' => array('menu1name'=>'option', 'menu2name'=>'option') ) to specify variant by product id and option * @param string $pricetype (optional default:variant) product, variant, or addon * @return ShoppPrice Price object or false on error **/ function shopp_product_variant($variant = false, $pricetype = 'variant') { if (false === $variant) { shopp_debug(__FUNCTION__ . " failed: Variant id required."); return false; } if (is_numeric($variant)) { $Price = new ShoppPrice($variant); if (empty($Price->id)) { shopp_debug(__FUNCTION__ . " failed: Unable to load variant {$variant}."); return false; } } else { if (is_array($variant)) { // specifying variant by product id and option $Product = new stdClass(); if (isset($variant['product']) && is_numeric($variant['product'])) { $Product = new ShoppProduct($variant['product']); $Product->load_data(array('prices', 'meta', 'summary')); } if (empty($Product->id) || empty($Product->prices)) { shopp_debug(__FUNCTION__ . " failed: Unable to load variant. Invalid Product."); return false; } $pricetype = $pricetype == 'variant' ? 'variation' : $pricetype; $pricetypes = array('product', 'variation', 'addon'); if (!in_array($pricetype, $pricetypes)) { shopp_debug(__FUNCTION__ . " failed: Invalid pricetype. Can be product, variant, or addon."); return false; } if ('product' == $pricetype) { // No product context for product with variants if ('on' == $Product->variants) { shopp_debug(__FUNCTION__ . " failed: Invalid pricetype for this product."); return false; } foreach ($Product->prices as $price) { if ('product' == $price->context) { $Price = new ShoppPrice(); $Price->populate($price); $Price->load_settings(); $Price->load_download(); break; } } } else { // addon or variant if (!isset($variant['option']) || !is_array($variant['option']) || empty($variant['option'])) { shopp_debug(__FUNCTION__ . " failed: Missing option array."); return false; } $menukey = substr($pricetype, 0, 1); $flag = $pricetype == 'variation' ? 'variants' : 'addons'; if (!isset($Product->options[$menukey]) || $Product->{$flag} == 'off') { shopp_debug(__FUNCTION__ . " failed: No product variant options of type {$pricetype} for product {$Product->id}"); return false; } // build simple option menu array $menu = array(); foreach ($Product->options[$menukey] as $optionmenu) { $key = $optionmenu['name']; $menu[$key] = array(); foreach ($optionmenu['options'] as $option) { $menu[$key][] = $option['name']; } } list($optionkey, $options, $label, $mapping) = $Product->optionmap($variant['option'], $menu, $pricetype); if ('variation' == $pricetype && !isset($Product->pricekey[$optionkey]) || !$options) { shopp_debug(__FUNCTION__ . " failed: Invalid option."); return false; } if ('variation' == $pricetype) { $price = $Product->pricekey[$optionkey]; } else { // Find the option foreach ($Product->prices as $price) { if ($price->context == $pricetype && $price->options == $options) { break; } } } $Price = new Price(); $Price->populate($price); $Price->load_settings(); $Price->load_download(); } // end if product type / addon/variants type } } if (!isset($Price)) { shopp_debug(__FUNCTION__ . " failed: Product, Variant, or Addon Price object could not be found."); return false; } return $Price; }