示例#1
0
/**
 * @todo - Should probably refactor this at some point - very procedural,
 *		   WAY too many foreach loops for my liking :)  But it does the trick
 *
 * @param <type> $term_id
 */
function save_term_prices($term_id)
{
    // First - Saves options from input
    if (isset($_POST['variation_price']) || isset($_POST["apply_to_current"])) {
        $term_prices = get_option('term_prices');
        $term_prices[$term_id]["price"] = $_POST["variation_price"];
        $term_prices[$term_id]["checked"] = isset($_POST["apply_to_current"]) ? "checked" : "unchecked";
        update_option('term_prices', $term_prices);
    }
    // Second - If box was checked, let's then check whether or not it was flat, differential, or percentile, then let's apply the pricing to every product appropriately
    if (isset($_POST["apply_to_current"])) {
        //Check for flat, percentile or differential
        $var_price_type = '';
        if (flat_price($_POST["variation_price"])) {
            $var_price_type = 'flat';
        } elseif (differential_price($_POST["variation_price"])) {
            $var_price_type = 'differential';
        } elseif (percentile_price($_POST["variation_price"])) {
            $var_price_type = 'percentile';
        }
        //Now, find all products with this term_id, update their pricing structure (terms returned include only parents at this point, we'll grab relevent children soon)
        $products_to_mod = get_objects_in_term($term_id, "wpsc-variation");
        $product_parents = array();
        foreach ((array) $products_to_mod as $get_parent) {
            $post = get_post($get_parent);
            if (!$post->post_parent) {
                $product_parents[] = $post->ID;
            }
        }
        //Now that we have all parent IDs with this term, we can get the children (only the ones that are also in $products_to_mod, we don't want to apply pricing to ALL kids)
        foreach ($product_parents as $parent) {
            $args = array('post_parent' => $parent, 'post_type' => 'wpsc-product');
            $children = get_children($args, ARRAY_A);
            foreach ($children as $childrens) {
                $parent = $childrens["post_parent"];
                $children_ids[$parent][] = $childrens["ID"];
                $children_ids[$parent] = array_intersect($children_ids[$parent], $products_to_mod);
            }
        }
        //Got the right kids, let's grab their parent pricing and modify their pricing based on var_price_type
        foreach ((array) $children_ids as $parents => $kids) {
            $kids = array_values($kids);
            foreach ($kids as $kiddos) {
                $price = wpsc_determine_variation_price($kiddos);
                update_product_meta($kiddos, 'price', $price);
            }
        }
    }
}
/**
 * Determine the price of a variation product based on the variation it's assigned
 * to. Because each variation term can have its own price (eg. 10, +10, -5%), this
 * function also takes those into account.
 *
 * @since 3.8.6
 * @param int $variation_id ID of the variation product
 * @param string $terms Optional. Defaults to false. Variation terms assigned to
 * the variation product. Pass this argument to save one SQL query.
 * @return float Calculated price of the variation
 */
function wpsc_determine_variation_price($variation_id, $term_ids = false)
{
    $flat = array();
    $diff = 0;
    $variation = get_post($variation_id);
    $price = (double) get_product_meta($variation->post_parent, 'price', true);
    if (!$term_ids) {
        $term_ids = wp_get_object_terms($variation_id, 'wpsc-variation', array('fields' => 'ids'));
    }
    $term_price_arr = get_option('term_prices');
    foreach ($term_ids as $term_id) {
        if (isset($term_price_arr[$term_id])) {
            $term_price = trim($term_price_arr[$term_id]['price']);
        } else {
            continue;
        }
        if (flat_price($term_price)) {
            $flat[] = $term_price;
        } elseif (differential_price($term_price)) {
            $diff += (double) $term_price;
        } elseif (percentile_price($term_price)) {
            $diff += (double) $term_price / 100 * $price;
        }
    }
    // Variation price should at least be the maximum of all flat prices
    if (!empty($flat)) {
        $price = max($flat);
    }
    $price += $diff;
    return $price;
}
示例#3
0
/**
 * term_id_price function 
 * Retreives associated price, if any, with term_id
 * @param integer term ID
 * @param integer parent product price
 * @return integer modified price for child product, based on term ID price and parent price
 */
function term_id_price($term_id, $parent_price)
{
    $term_price_arr = get_option('term_prices');
    if (isset($term_price_arr[$term_id])) {
        $price = $term_price_arr[$term_id]["price"];
    } else {
        $price = 0;
    }
    //Check for flat, percentile or differential
    $var_price_type = '';
    if (flat_price($price)) {
        $var_price_type = 'flat';
        $price = floatval($price);
    } elseif (differential_price($price)) {
        $var_price_type = 'differential';
    } elseif (percentile_price($price)) {
        $var_price_type = 'percentile';
    }
    if (strchr($price, '-')) {
        $negative = true;
    } else {
        $positive = true;
    }
    if ($positive) {
        if ($var_price_type == 'differential') {
            $differential = floatval($price);
            $price = $parent_price + $differential;
        } elseif ($var_price_type == 'percentile') {
            $percentage = floatval($price) / 100;
            $price = $parent_price + $parent_price * $percentage;
        }
    } else {
        if ($var_price_type == 'differential') {
            $differential = floatval($price);
            $price = $parent_price - $differential;
        } elseif ($var_price_type == 'percentile') {
            $percentage = floatval($price) / 100;
            $price = $parent_price - $parent_price * $percentage;
        }
    }
    return $price;
}