Example #1
0
/**
 * Variation uniq item creation
 */
function ajax_add_new_single_variation()
{
    check_ajax_referer('wpshop_variation_management', 'wpshop_ajax_nonce');
    $output = '';
    $attributes_for_variation = isset($_POST['variation_attr']) ? $_POST['variation_attr'] : null;
    $wpshop_admin_use_attribute_for_single_variation_checkbox = isset($_POST['wpshop_admin_use_attribute_for_single_variation_checkbox']) ? $_POST['wpshop_admin_use_attribute_for_single_variation_checkbox'] : null;
    $variation_specific_definition = isset($_POST['wps_pdt_variations']['new']['attribute']) ? $_POST['wps_pdt_variations']['new']['attribute'] : null;
    $current_post_id = isset($_POST['wpshop_head_product_id']) ? wpshop_tools::varSanitizer($_POST['wpshop_head_product_id']) : null;
    $attribute_to_use_for_creation = array();
    foreach ($attributes_for_variation as $attribute_code => $attribute_value) {
        if (array_key_exists($attribute_code, $wpshop_admin_use_attribute_for_single_variation_checkbox)) {
            $attribute_to_use_for_creation[0][$attribute_code] = $attributes_for_variation[$attribute_code];
            $attr_def = wpshop_attributes::getElement($attribute_code, "'valid'", 'code');
            $variation_specific_definition[$attr_def->data_type][$attribute_code] = $attributes_for_variation[$attribute_code];
        }
    }
    $new_variation_identifier = wpshop_products::creation_variation_callback($attribute_to_use_for_creation, $current_post_id);
    /*	Save variation specific element	*/
    foreach (unserialize(WPSHOP_ATTRIBUTE_PRICES) as $price_attribute_code) {
        $head_product_price_attribute_value = wpshop_attributes::get_attribute_value_content($price_attribute_code, $current_post_id, wpshop_products::currentPageCode);
        $price_attr_def = wpshop_attributes::getElement($price_attribute_code, "'valid'", 'code');
        if (!empty($price_attr_def) && !empty($price_attr_def->data_type) && (empty($variation_specific_definition[$price_attr_def->data_type]) || !array_key_exists($price_attribute_code, $variation_specific_definition[$price_attr_def->data_type]))) {
            $variation_specific_definition[$price_attr_def->data_type][$price_attribute_code] = !empty($head_product_price_attribute_value->value) ? $head_product_price_attribute_value->value : 1;
        }
    }
    wpshop_attributes::saveAttributeForEntity($variation_specific_definition, wpshop_entities::get_entity_identifier_from_code(wpshop_products::currentPageCode), $new_variation_identifier, WPSHOP_CURRENT_LOCALE);
    wpshop_products::calculate_price($new_variation_identifier);
    $output = wpshop_products::display_variation_admin($current_post_id);
    echo $output;
    die;
}