/**
  * Effectue des actions à l'activation du logiciel de caisse / Do some defautl action on POS addon activation
  */
 public static function action_to_do_on_activation()
 {
     global $wpdb;
     /** Activate Barcode attribute **/
     if (false) {
         $wpdb->update(WPSHOP_DBT_ATTRIBUTE, array('status' => 'valid'), array('code' => 'barcode'));
         /** Get the product entity id **/
         $query = $wpdb->prepare('SELECT ID FROM ' . $wpdb->posts . ' WHERE post_type = %s AND post_name = %s', WPSHOP_NEWTYPE_IDENTIFIER_ENTITIES, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT);
         $product_entity_id = $wpdb->get_var($query);
         /** Check the barcode attribute id **/
         $query = $wpdb->prepare('SELECT id FROM ' . WPSHOP_DBT_ATTRIBUTE . ' WHERE code = %s', 'barcode');
         $attribute_barcode_id = $wpdb->get_var($query);
         $query = $wpdb->prepare('SELECT id FROM ' . WPSHOP_DBT_ATTRIBUTE_SET . ' WHERE entity_id = %d', $product_entity_id);
         $products_groups = $wpdb->get_results($query);
         /** For each attributes groups used for product configuration **/
         foreach ($products_groups as $products_group) {
             $query = $wpdb->prepare('SELECT id FROM ' . WPSHOP_DBT_ATTRIBUTE_GROUP . ' WHERE attribute_set_id = %d AND code = %s', $products_group->id, 'general');
             $attributes_set_sections = $wpdb->get_results($query);
             foreach ($attributes_set_sections as $attributes_set_section) {
                 $query = $wpdb->prepare('SELECT MAX(position) AS max_position FROM ' . WPSHOP_DBT_ATTRIBUTE_DETAILS . ' WHERE entity_type_id = %d AND attribute_set_id = %d AND attribute_group_id = %d', $product_entity_id, $products_group->id, $attributes_set_section->id);
                 $max_position = $wpdb->get_var($query);
                 $query = $wpdb->prepare('SELECT * FROM ' . WPSHOP_DBT_ATTRIBUTE_DETAILS . ' WHERE entity_type_id = %d AND attribute_set_id = %d AND attribute_group_id = %d AND attribute_id = %d', $product_entity_id, $products_group->id, $attributes_set_section->id, $attribute_barcode_id);
                 $exist_barcode_details_definition = $wpdb->get_results($query);
                 /** Insert the barcode attribute details **/
                 if (!empty($max_position) && empty($exist_barcode_details_definition)) {
                     $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_DETAILS, array('status' => 'valid', 'creation_date' => current_time('mysql', 0), 'entity_type_id' => $product_entity_id, 'attribute_set_id' => $products_group->id, 'attribute_group_id' => $attributes_set_section->id, 'attribute_id' => $attribute_barcode_id, 'position' => (int) $max_position + 1));
                 }
             }
         }
     }
     /** Activate Barcode for search **/
     $wpdb->update(WPSHOP_DBT_ATTRIBUTE, array('is_used_for_sort_by' => 'yes', 'is_used_in_quick_add_form' => 'yes'), array('code' => 'barcode'));
     /** Activate attribute for the product quick add form **/
     $price_piloting_option = get_option('wpshop_shop_price_piloting');
     $code = !empty($price_piloting_option) && $price_piloting_option == 'HT' ? 'price_ht' : 'product_price';
     $wpdb->update(WPSHOP_DBT_ATTRIBUTE, array('is_used_in_quick_add_form' => 'yes'), array('code' => $code));
     $wpdb->update(WPSHOP_DBT_ATTRIBUTE, array('is_used_in_quick_add_form' => 'yes'), array('code' => 'tx_tva'));
     /** Check If Shop Customer attribute set exist - Deprecated **/
     /* $customer_entity_id = wpshop_entities::get_entity_identifier_from_code( WPSHOP_NEWTYPE_IDENTIFIER_ADDRESS );
     		$query = $wpdb->prepare( 'SELECT id FROM ' .WPSHOP_DBT_ATTRIBUTE_SET. ' WHERE name = %s',  __('Shop Customer', 'wps-pos-i18n') );
     		$exist_attribute_group = $wpdb->get_var( $query ); */
     $exist_attribute_group = 'Nope';
     if (empty($exist_attribute_group)) {
         /** Create a Attributes Group for POS Customer infos **/
         $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_SET, array('status' => 'valid', 'creation_date' => current_time('mysql', 0), 'last_update_date' => current_time('mysql', 0), 'entity_id' => $customer_entity_id, 'name' => __('Shop Customer', 'wps-pos-i18n')));
         $attribute_set_id = $wpdb->insert_id;
         /** Create Attributes Group **/
         $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_GROUP, array('status' => 'valid', 'default_group' => 'yes', 'creation_date' => current_time('mysql', 0), 'last_update_date' => current_time('mysql', 0), 'attribute_set_id' => $attribute_set_id, 'code' => sanitize_title(__('Shop Customer main infos', 'wps-pos-i18n')), 'name' => __('Shop Customer main infos', 'wps-pos-i18n')));
         $main_set_id = $wpdb->insert_id;
         $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_GROUP, array('status' => 'valid', 'creation_date' => current_time('mysql', 0), 'last_update_date' => current_time('mysql', 0), 'attribute_set_id' => $attribute_set_id, 'code' => sanitize_title(__('Shop Customer address infos', 'wps-pos-i18n')), 'name' => __('Shop Customer address infos', 'wps-pos-i18n')));
         $address_set_id = $wpdb->insert_id;
         /** Affect Attributes **/
         $attributes = array('last_name' => $main_set_id, 'first_name' => $main_set_id, 'address_last_name' => $main_set_id, 'address_first_name' => $main_set_id, 'address_user_email' => $main_set_id, 'address' => $address_set_id, 'postcode' => $address_set_id, 'city' => $address_set_id, 'phone' => $address_set_id);
         $i = 1;
         foreach ($attributes as $attribute => $group_id) {
             $attribute_def = wpshop_attributes::getElement($attribute, "'valid'", 'code');
             if (!empty($attribute_def)) {
                 $wpdb->insert(WPSHOP_DBT_ATTRIBUTE_DETAILS, array('status' => 'valid', 'creation_date' => current_time('mysql', 0), 'last_update_date' => current_time('mysql', 0), 'entity_type_id' => $customer_entity_id, 'attribute_set_id' => $attribute_set_id, 'attribute_group_id' => $group_id, 'attribute_id' => $attribute_def->id, 'position' => $i));
                 $i++;
             }
         }
     }
     $user_name = 'default_customer';
     $user_id = username_exists($user_name);
     if (!$user_id) {
         $random_password = wp_generate_password($length = 12, $include_standard_special_chars = false);
         $user_id = wp_create_user($user_name, $random_password, '*****@*****.**');
         if (!empty($user_id)) {
             update_user_meta($user_id, 'last_name', __('Default', 'wps-pos-i18n'));
             update_user_meta($user_id, 'first_name', __('Customer', 'wps-pos-i18n'));
             /** Add the default customer id in Option table **/
             update_option('wpshop_pos_addon_default_customer_id', $user_id);
         }
     }
     /** Get post of user id */
     $customers_associated_to_user = get_posts(array('post_status' => 'draft', 'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS, 'author' => $user_id));
     $customer_id = !empty($customers_associated_to_user) && !empty($customers_associated_to_user[0]) && !empty($customers_associated_to_user[0]->ID) ? $customers_associated_to_user[0]->ID : null;
     $customer_entity_id = wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_CUSTOMERS);
     if (!empty($customer_id)) {
         /** Insert attribute data */
         wpshop_attributes::saveAttributeForEntity(array('varchar' => array('last_name' => __('Default', 'wps-pos-i18n'), 'first_name' => __('Customer', 'wps-pos-i18n'), 'user_email' => '*****@*****.**', 'address_last_name' => __('Default', 'wps-pos-i18n'), 'address_first_name' => __('Customer', 'wps-pos-i18n'), 'address_user_email' => '*****@*****.**', 'address' => __('Default address'), 'postcode' => '42420', 'city' => __('Default city'), 'phone' => '0000000042')), $customer_entity_id, $customer_id);
     }
 }
 /**
  * Récupère le pilotage de prix, le nombre de produit avec un prix incohérent, le type de l'entité et la langue de la boutique. 
  * Parcours la tableau de donnée avec la nouvelle valeur des prix par produit incohérent puis met à jour tout les autres prix de 
  * ce produit. Ensuite renvoie le template avec le nombre de prix incohérent qui on été corrigé et le template de la méthode :
  * ajax_checking_products_values si des produits incohérents sont toujours présent. / Get the price piloting, the number of 
  * product with an inconsistent price, type of the entity and the language of the shop. Browse the given table with the new
  * new value pricing of the inconsistent product and updates any other price of this product. Then display the template 
  * of the number of corrected product and the template of the method : ajax_checking_products_values if inconsistent product
  * already present.
  * 
  * @param array $_POST['product_price'] List of the new price for the product like array( $id_product => $new_price )
  * @return JSON Response
  */
 public function ajax_save_product_price()
 {
     header('Content-Type: application/json');
     $response = array();
     $price_piloting_option = get_option('wpshop_shop_price_piloting');
     $inconsistent_product_number = !empty($_POST['product_price']) ? count($_POST['product_price']) : 0;
     $consistent_product_number = 0;
     $entity_type_id = wpshop_entities::get_entity_identifier_from_code(WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT);
     $language = WPSHOP_CURRENT_LOCALE;
     if (!empty($_REQUEST['icl_post_language'])) {
         $query = $wpdb->prepare("SELECT locale FROM " . $wpdb->prefix . "icl_locale_map WHERE code = %s", $_REQUEST['icl_post_language']);
         $language = $wpdb->get_var($query);
     }
     if (!empty($_POST['product_price'])) {
         foreach ($_POST['product_price'] as $product_id => $price) {
             try {
                 if ($price_piloting_option == 'TTC') {
                     wpshop_attributes::saveAttributeForEntity(array('decimal' => array('product_price' => $price)), $entity_type_id, $product_id, $language, 'wpshop_products');
                 } else {
                     wpshop_attributes::saveAttributeForEntity(array('decimal' => array('price_ht' => $price)), $entity_type_id, $product_id, $language, 'wpshop_product');
                 }
                 wpshop_products::calculate_price($product_id);
                 $consistent_product_number++;
             } catch (Exception $e) {
             }
         }
     }
     $response['template_number'] = __(sprintf('Number of processed product : %d/%d', $consistent_product_number, $inconsistent_product_number), 'wps-product');
     $list_product = wps_product_ctr::get_inconsistent_product();
     ob_start();
     require wpshop_tools::get_template_part(WPS_PRODUCT_DIR, WPS_PRODUCT_TEMPLATES_MAIN_DIR, "backend", "product_check_data");
     $response['template'] = ob_get_clean();
     wp_die(json_encode($response));
 }
示例#3
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;
}
 /**
  * Save informations for current entity
  */
 public static function save_entities_custom_informations($post)
 {
     global $wpdb;
     global $wpshop_account;
     $post_id = !empty($_REQUEST['post_ID']) ? intval(wpshop_tools::varSanitizer($_REQUEST['post_ID'])) : null;
     if (!empty($post_id) && empty($_POST['edit_other_thing']) || !empty($_REQUEST['post_ID']) && !(bool) $_POST['edit_other_thing'] && get_post_type($_REQUEST['post_ID']) != WPSHOP_NEWTYPE_IDENTIFIER_ORDER) {
         $current_post_type = get_post_type($post_id);
         /*	Vérification de l'existence de l'envoi de l'identifiant du set d'attribut	*/
         if (!empty($_REQUEST[$current_post_type . '_attribute_set_id'])) {
             $attribute_set_id = intval(wpshop_tools::varSanitizer($_REQUEST[$current_post_type . '_attribute_set_id']));
             $attribet_set_infos = wpshop_attributes_set::getElement($attribute_set_id, "'valid'", 'id');
             if ($attribet_set_infos->entity == $_REQUEST['post_type']) {
                 /*	Enregistrement de l'identifiant du set d'attribut associé à l'entité	*/
                 update_post_meta($post_id, sprintf(WPSHOP_ATTRIBUTE_SET_ID_META_KEY, $current_post_type), $attribute_set_id);
                 /*	Enregistrement de tous les attributs	*/
                 if (!empty($_REQUEST[$current_post_type . '_attribute'])) {
                     /*	Traduction des virgule en point pour la base de donnees	*/
                     if (!empty($_REQUEST[$current_post_type . '_attribute']['decimal'])) {
                         foreach ($_REQUEST[$current_post_type . '_attribute']['decimal'] as $attributeName => $attributeValue) {
                             if (!is_array($attributeValue)) {
                                 $_REQUEST[$current_post_type . '_attribute']['decimal'][$attributeName] = str_replace(',', '.', $_REQUEST[$current_post_type . '_attribute']['decimal'][$attributeName]);
                             }
                         }
                     }
                     /*	Enregistrement des valeurs des différents attributs	*/
                     wpshop_attributes::saveAttributeForEntity($_REQUEST[$current_post_type . '_attribute'], wpshop_entities::get_entity_identifier_from_code($current_post_type), $post_id, WPSHOP_CURRENT_LOCALE);
                     /*	Enregistrement des valeurs des attributs dans les metas de l'entité => Permet de profiter de la recherche native de wordpress	*/
                     $productMetaDatas = array();
                     foreach ($_REQUEST[$current_post_type . '_attribute'] as $attributeType => $attributeValues) {
                         foreach ($attributeValues as $attributeCode => $attributeValue) {
                             $productMetaDatas[$attributeCode] = $attributeValue;
                         }
                     }
                     update_post_meta($_REQUEST['post_ID'], WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, $productMetaDatas);
                 }
             }
         }
         if (!empty($_REQUEST['attribute'])) {
             $current_id = array();
             foreach ($_REQUEST['attribute'] as $key => $values) {
                 $ad_id = '';
                 $addresses_id = get_post_meta($_REQUEST['post_ID'], '_wpshop_attached_address', true);
                 if (!empty($addresses_id)) {
                     foreach ($addresses_id as $address_id) {
                         $address_type = get_post_meta($address_id, '_wpshop_address_attribute_set_id', true);
                         if ($address_type == $key) {
                             $ad_id = $address_id;
                         }
                     }
                 }
                 $_REQUEST['item_id'] = $ad_id;
                 $result = wps_address::save_address_infos($key);
                 $current_id[] = $result['current_id'];
             }
             update_post_meta($_REQUEST['post_ID'], '_wpshop_attached_address', $current_id);
         } else {
             $current_id = array();
             if (!empty($_REQUEST['address_type'])) {
                 foreach ($_REQUEST['address_type'] as $key => $value) {
                     $current_id[] = $value;
                 }
             }
             update_post_meta($_REQUEST['post_ID'], '_wpshop_entity_attached_address', $current_id);
         }
     }
     /** Save price infos **/
     if (!empty($_REQUEST) && !empty($_REQUEST['post_ID']) && !empty($_REQUEST['post_type']) && $_REQUEST['post_type'] == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT) {
     }
     flush_rewrite_rules();
 }
 /**
  * Create a new variation for product
  *
  * @param integer $head_product The product identifier to create the new variation for
  * @param array $variation_attributes Attribute list for the variation
  *
  * @return mixed <number, WP_Error> The variation identifier or an error in case the creation was not succesfull
  */
 public static function create_variation($head_product, $variation_attributes)
 {
     /** Create custom title */
     $title_variation = "";
     if (!empty($variation_attributes)) {
         foreach ($variation_attributes as $type => $array) {
             if (!empty($array)) {
                 foreach ($array as $code => $value) {
                     $title_variation .= " " . $code . " " . $value;
                 }
             }
         }
     }
     $variation = array('post_title' => sprintf(__('Product %s variation %s', 'wpshop'), $head_product, get_the_title($head_product)) . $title_variation, 'post_content' => '', 'post_status' => 'publish', 'post_author' => get_current_user_id(), 'post_parent' => $head_product, 'post_type' => WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION);
     $variation_id = wp_insert_post($variation);
     wpshop_attributes::saveAttributeForEntity($variation_attributes, wpshop_entities::get_entity_identifier_from_code(wpshop_products::currentPageCode), $variation_id, WPSHOP_CURRENT_LOCALE, '');
     /*	Update product price looking for shop parameters	*/
     wpshop_products::calculate_price($variation_id);
     /*	Save the attributes values into wordpress post metadata database in order to have a backup and to make frontend search working	*/
     $productMetaDatas = array();
     foreach ($variation_attributes as $attributeType => $attributeValues) {
         foreach ($attributeValues as $attributeCode => $attributeValue) {
             if (!empty($attributeValue)) {
                 $productMetaDatas[$attributeCode] = $attributeValue;
             }
         }
     }
     update_post_meta($variation_id, '_wpshop_variations_attribute_def', $productMetaDatas);
     update_post_meta($variation_id, WPSHOP_PRODUCT_ATTRIBUTE_META_KEY, $productMetaDatas);
     update_post_meta($variation_id, WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, get_post_meta($head_product, WPSHOP_PRODUCT_ATTRIBUTE_SET_ID_META_KEY, true));
     return $variation_id;
 }