/** * Variation list creation */ function ajax_add_new_variation_list() { check_ajax_referer('wpshop_variation_management', 'wpshop_ajax_nonce'); global $wpdb; $attributes_for_variation = isset($_POST['wpshop_attribute_to_use_for_variation']) ? $_POST['wpshop_attribute_to_use_for_variation'] : null; $current_post_id = isset($_POST['current_post_id']) ? wpshop_tools::varSanitizer($_POST['current_post_id']) : null; /** Get the list of values of the attribute to affect to a variation */ $var = array(); foreach ($attributes_for_variation as $attribute_code) { $query = $wpdb->prepare("SELECT data_type_to_use FROM " . WPSHOP_DBT_ATTRIBUTE . " WHERE code = %s", $attribute_code); $var[$attribute_code] = wpshop_attributes::get_affected_value_for_list($attribute_code, $current_post_id, $wpdb->get_var($query)); } $possible_variations = wpshop_tools::search_all_possibilities($var); wpshop_products::creation_variation_callback($possible_variations, $current_post_id); $output = wpshop_products::display_variation_admin($current_post_id); echo $output; die; }
/** * Get all attribute available for current * @param unknown_type $current_entity_id * @return Ambigous <multitype:, multitype:NULL > */ public static function get_variation_available_attribute($current_entity_id) { global $wpdb; $final_list = array(); /** Get all attributes defined as usable into product variation for the product type and group */ $query = $wpdb->prepare("SELECT ATT.*, ENTITY_META.meta_value\r\n\t\t\t\tFROM " . self::getDbTable() . " AS ATT\r\n\t\t\t\t\tINNER JOIN " . WPSHOP_DBT_ATTRIBUTE_DETAILS . " AS ATT_DETAILS ON ((ATT_DETAILS.attribute_id = ATT.id) AND (ATT_DETAILS.entity_type_id = %d) AND (ATT_DETAILS.status = 'valid'))\r\n\t\t\t\t\tINNER JOIN " . $wpdb->postmeta . " AS ENTITY_META ON ((ENTITY_META.meta_key = %s) AND (ENTITY_META.meta_value = ATT_DETAILS.attribute_set_id))\r\n\t\t\t\tWHERE ATT.status IN ('valid')\r\n\t\t\t\t\tAND ATT.is_used_for_variation = %s\r\n\t\t\t\t\tAND ENTITY_META.post_id = %d\r\n\t\t\t\tGROUP BY ATT_DETAILS.position, ENTITY_META.post_id, ATT.code\r\n\t\t\t\tORDER BY ATT_DETAILS.position", wpshop_entities::get_entity_identifier_from_code(get_post_type($current_entity_id)), WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, 'yes', $current_entity_id); $attribute_list = $wpdb->get_results($query); foreach ($attribute_list as $attribute) { if (!in_array($attribute->code, unserialize(WPSHOP_VARIATION_ATTRIBUTE_TO_HIDE)) && in_array($attribute->backend_input, array('select', 'multiple-select'))) { $attribute_values_for_variations = wpshop_attributes::get_affected_value_for_list($attribute->code, $current_entity_id, $attribute->data_type_to_use); if (empty($attribute_values_for_variations)) { $final_list['unavailable'][$attribute->code]['label'] = $attribute->frontend_label; $final_list['unavailable'][$attribute->code]['values'] = array(); $final_list['unavailable'][$attribute->code]['attribute_complete_def'] = $attribute; } else { $final_list['available'][$attribute->code]['label'] = $attribute->frontend_label; $final_list['available'][$attribute->code]['values'] = $attribute_values_for_variations; $final_list['available'][$attribute->code]['attribute_complete_def'] = $attribute; } } } return $final_list; }
/** * Save Products attribute values for List attribute data for a products category * @param integer $category_id * @param std_object $attribute_def * @param array $current_category_child */ function save_values_for_list_filterable_attribute($category_id, $attribute_def, $current_category_children) { global $wpdb; $category_option = get_option('wpshop_product_category_' . $category_id); $products = wpshop_categories::get_product_of_category($category_id); /** If there are sub-categories take all products of sub-categories **/ if (!empty($current_category_children)) { foreach ($current_category_children as $current_category_child) { $sub_categories_product_ids = wpshop_categories::get_product_of_category($current_category_child->term_taxonomy_id); if (!empty($sub_categories_product_ids)) { foreach ($sub_categories_product_ids as $sub_categories_product_id) { if (!in_array($sub_categories_product_id, $products)) { $products[] = $sub_categories_product_id; } } } } } if (!empty($category_option) && !empty($category_option['wpshop_category_filterable_attributes']) && !empty($category_option['wpshop_category_filterable_attributes'][$attribute_def->id])) { $category_option['wpshop_category_filterable_attributes'][$attribute_def->id] = array(); } if (!empty($attribute_def)) { $available_attribute_values = array(); $test = array(); foreach ($products as $product) { $available_attribute_values = array_merge($available_attribute_values, wpshop_attributes::get_affected_value_for_list($attribute_def->code, $product, $attribute_def->data_type_to_use)); } $available_attribute_values = array_flip($available_attribute_values); $data_to_save = array(); if (!empty($available_attribute_values)) { $data_to_save = array(); foreach ($available_attribute_values as $k => $available_attribute_value) { if ($attribute_def->data_type_to_use == 'internal') { $attribute_name = get_the_title($k); } else { $query = $wpdb->prepare('SELECT label FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS . ' WHERE attribute_id = %d AND id = %d', $attribute_def->id, $k); $attribute_name = $wpdb->get_var($query); } if (!empty($attribute_name) && !empty($k)) { if (!empty($category_option) && !empty($category_option['wpshop_category_filterable_attributes']) && isset($category_option['wpshop_category_filterable_attributes'][$attribute_def->id])) { $data_to_save[$k] = $attribute_name; $category_option['wpshop_category_filterable_attributes'][$attribute_def->id] = $data_to_save; } } } } } update_option('wpshop_product_category_' . $category_id, $category_option); }