/**
  *	Add additionnal fields to the category edition form
  */
 public static function category_edit_fields()
 {
     $category_id = wpshop_tools::varSanitizer($_REQUEST["tag_ID"]);
     $category_meta_information = get_option(WPSHOP_NEWTYPE_IDENTIFIER_CATEGORIES . '_' . $category_id);
     $tpl_component = array();
     $category_thumbnail_preview = '<img src="' . WPSHOP_DEFAULT_CATEGORY_PICTURE . '" alt="No picture" class="category_thumbnail_preview" />';
     /*	Check if there is already a picture for the selected category	*/
     if (!empty($category_meta_information['wpshop_category_picture'])) {
         $image_post = wp_get_attachment_image($category_meta_information['wpshop_category_picture'], 'thumbnail', false, array('class' => 'category_thumbnail_preview'));
         $category_thumbnail_preview = !empty($image_post) ? $image_post : '<img src="' . WPSHOP_DEFAULT_CATEGORY_PICTURE . '" alt="No picture" class="category_thumbnail_preview" />';
     }
     $tpl_component['CATEGORY_DELETE_PICTURE_BUTTON'] = '';
     if (!empty($category_meta_information) && !empty($category_meta_information['wpshop_category_picture'])) {
         $tpl_component['CATEGORY_DELETE_PICTURE_BUTTON'] = '<a href="#" role="button" id="wps-delete-category-picture" class="wps-bton-second-mini-rounded">' . __('Delete the category picture', 'wpshop') . '</a> ';
     }
     $tpl_component['CATEGORY_PICTURE_ID'] = !empty($category_meta_information['wpshop_category_picture']) ? $category_meta_information['wpshop_category_picture'] : '';
     $tpl_component['CATEGORY_THUMBNAIL_PREVIEW'] = $category_thumbnail_preview;
     if (isset($_GET['tag_ID'])) {
         $tpl_component['CATEGORY_TAG_ID'] = $_GET['tag_ID'];
         $tpl_component['CATEGORY_FILTERABLE_ATTRIBUTES'] = '';
         $wpshop_category_products = wpshop_categories::get_product_of_category($_GET['tag_ID']);
         $filterable_attributes_list = array();
         foreach ($wpshop_category_products as $wpshop_category_product) {
             $elementId = wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT);
             if (!empty($elementId)) {
                 $product_attributes = wpshop_attributes::get_attribute_list_for_item($elementId, $wpshop_category_product);
                 if (!empty($product_attributes)) {
                     foreach ($product_attributes as $key => $product_attribute) {
                         if (!empty($product_attribute) && !empty($product_attribute->is_filterable) && strtolower(__($product_attribute->is_filterable, 'wpshop')) == strtolower(__('Yes', 'wpshop'))) {
                             if (!array_key_exists($product_attribute->attribute_id, $filterable_attributes_list)) {
                                 $filterable_attributes_list[$product_attribute->attribute_id] = $product_attribute;
                                 $sub_tpl_component['CATEGORY_FILTERABLE_ATTRIBUTE_ID'] = $product_attribute->attribute_id;
                                 $sub_tpl_component['CATEGORY_FILTERABLE_ATTRIBUTE_NAME'] = __($product_attribute->frontend_label, 'wpshop');
                                 if (!empty($category_meta_information) && !empty($category_meta_information['wpshop_category_filterable_attributes']) && array_key_exists($product_attribute->attribute_id, $category_meta_information['wpshop_category_filterable_attributes'])) {
                                     $sub_tpl_component['CATEGORY_FILTERABLE_ATTRIBUTE_CHECKED'] = 'checked="checked"';
                                 } else {
                                     $sub_tpl_component['CATEGORY_FILTERABLE_ATTRIBUTE_CHECKED'] = '';
                                 }
                                 $tpl_component['CATEGORY_FILTERABLE_ATTRIBUTES'] .= wpshop_display::display_template_element('wpshop_category_filterable_attribute_element', $sub_tpl_component, array(), 'admin');
                                 unset($sub_tpl_component);
                             }
                         }
                     }
                 }
             }
         }
     } else {
         $tpl_component['CATEGORY_TAG_ID'] = 1;
     }
     $output = wpshop_display::display_template_element('wpshop_category_edit_interface_admin', $tpl_component, array(), 'admin');
     echo $output;
 }
示例#2
0
/**
 * Save information for product when bulk edit
 */
function ajax_product_bulk_edit_save()
{
    global $wpdb;
    check_ajax_referer('product_bulk_edit_save', 'wpshop_ajax_nonce');
    $post_ids = isset($_POST['post_ids']) && !empty($_POST['post_ids']) ? $_POST['post_ids'] : array();
    $post_attributes = isset($_POST['attribute']) && !empty($_POST['attribute']) ? $_POST['attribute'] : array();
    if (!empty($post_ids) && is_array($post_ids) && !empty($post_attributes) && is_array($post_attributes)) {
        $attribute_to_save = array();
        foreach ($post_attributes as $attribute) {
            $attribute_component = explode('_-val-_', $attribute);
            $attribute_definition = explode('[', $attribute_component[0]);
            $attribute_data_type = substr($attribute_definition[1], 0, -1);
            $attribute_code = substr($attribute_definition[2], 0, -1);
            if (!empty($attribute_component[1])) {
                $attribute_to_save[$attribute_data_type][$attribute_code] = $attribute_component[1];
            }
        }
        foreach ($post_ids as $post_id) {
            $query = $wpdb->prepare("SELECT locale FROM " . $wpdb->prefix . "icl_locale_map WHERE code = (SELECT language_code FROM " . $wpdb->prefix . "icl_translations WHERE element_id = %d )", $post_id);
            $lang_wpml = $wpdb->get_var($query);
            $lang = !empty($lang_wpml) ? $lang_wpml : WPSHOP_CURRENT_LOCALE;
            /*	Save the attributes values into wpshop eav database	*/
            wpshop_attributes::saveAttributeForEntity($attribute_to_save, wpshop_entities::get_entity_identifier_from_code(wpshop_products::currentPageCode), $post_id, $lang, 'bulk');
            /*	Update product price looking for shop parameters	*/
            wpshop_products::calculate_price($post_id);
            /*	Save the attributes values into wordpress post metadata database in order to have a backup and to make frontend search working	*/
            $productMetaDatas = get_post_meta($post_id, '_wpshop_product_metadata', true);
            if (!empty($productMetaDatas)) {
                $attributes_list = wpshop_attributes::get_attribute_list_for_item(wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT), $post_id);
                if (!empty($attributes_list)) {
                    foreach ($attributes_list as $attribute) {
                        $value_key = 'attribute_value_' . $attribute->data_type;
                        $productMetaDatas[$attribute->code] = $attribute->{$value_key};
                    }
                }
            }
            foreach ($attribute_to_save as $attributeType => $attributeValues) {
                foreach ($attributeValues as $attributeCode => $attributeValue) {
                    if ($attributeCode == 'product_attribute_set_id') {
                        /*	Update the attribute set id for the current product	*/
                        update_post_meta($post_id, WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, $attributeValue);
                    }
                    $productMetaDatas[$attributeCode] = $attributeValue;
                }
            }
            update_post_meta($post_id, WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, $productMetaDatas);
            $parent_product_metadata = get_post_meta($post_id, '_wpshop_product_metadata', true);
            /* If the product have some variations */
            $query = $wpdb->prepare('SELECT * FROM ' . $wpdb->posts . ' WHERE post_parent = %d AND post_type = %s', $post_id, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION);
            $product_variations = $wpdb->get_results($query);
            if (!empty($product_variations)) {
                foreach ($product_variations as $product_variation) {
                    $variation_post_meta = get_post_meta($product_variation->ID, '_wpshop_product_metadata', true);
                    $common_attributes = unserialize(WPSHOP_COMMON_ATTRIBUTES_PARENT_VARIATION_PRODUCT);
                    if (!empty($common_attributes)) {
                        if (!empty($parent_product_metadata)) {
                            foreach ($parent_product_metadata as $key => $value) {
                                if (in_array($key, $common_attributes)) {
                                    $variation_post_meta[$key] = $value;
                                    update_post_meta($product_variation->ID, '_' . $key, $value);
                                }
                            }
                            update_post_meta($product_variation->ID, '_wpshop_product_metadata', $variation_post_meta);
                        }
                    }
                }
            }
        }
    }
    die;
}
 /**
  * Get variation list for a given product
  *
  * @param integer $head_product The product identifier to get the variation for
  * @return object The variation list
  */
 public static function get_variation($head_product)
 {
     global $wpdb;
     $variations_output = null;
     // 		$variations = query_posts(array(
     // 			'post_type' 	=> WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION,
     // 			'post_parent' 	=> $head_product,
     // 			'orderby' 		=> 'ID',
     // 			'order' 		=> 'ASC',
     // 			'posts_per_page'=> -1
     // 		));
     $query = $wpdb->prepare('SELECT * FROM ' . $wpdb->posts . ' WHERE post_type = %s AND post_parent = %d AND post_status = %s ORDER BY ID ASC', WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION, $head_product, 'publish');
     $variations = $wpdb->get_results($query);
     if (!empty($variations)) {
         $head_wpshop_variation_definition = get_post_meta($head_product, '_wpshop_variation_defining', true);
         foreach ($variations as $post_def) {
             $data = wpshop_attributes::get_attribute_list_for_item(wpshop_entities::get_entity_identifier_from_code(self::currentPageCode), $post_def->ID, WPSHOP_CURRENT_LOCALE, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT);
             foreach ($data as $content) {
                 $attribute_value = 'attribute_value_' . $content->data_type;
                 if (!empty($content->{$attribute_value})) {
                     if (!empty($head_wpshop_variation_definition['attributes']) && in_array($content->code, $head_wpshop_variation_definition['attributes'])) {
                         $variations_output[$post_def->ID]['variation_def'][$content->code] = $content->{$attribute_value};
                     } else {
                         $variations_output[$post_def->ID]['variation_dif'][$content->code] = $content->{$attribute_value};
                     }
                 }
             }
             $variations_output[$post_def->ID]['post'] = $post_def;
         }
     }
     wp_reset_query();
     return $variations_output;
 }