/**
  * 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));
 }
예제 #2
0
 /**
  * Add a product to the cart
  * @param   string	product_id	contains the id of the product to add to the cart
  * @param   string	quantity	contains the quantity of the item to add
  */
 function add_to_cart($product_list, $quantity, $type = 'normal', $extra_params = array(), $from_admin = '', $order_meta = '', $order_id = '')
 {
     global $wpdb;
     /** Check if a cart already exist. If there is already a cart that is not the same type (could be a cart or a quotation)	*/
     if (empty($from_admin)) {
         if (isset($_SESSION['cart']['cart_type']) && $type != $_SESSION['cart']['cart_type']) {
             return __('You have another element type into your cart. Please finalize it by going to cart page.', 'wpshop');
         } else {
             $_SESSION['cart']['cart_type'] = $type;
         }
         $order_meta = $_SESSION['cart'];
     }
     $order_items = array();
     foreach ($product_list as $pid => $product_more_content) {
         if (count($product_list) == 1) {
             if (!isset($quantity[$pid])) {
                 $quantity[$pid] = 1;
             }
             $product = wpshop_products::get_product_data($product_more_content['id']);
             /** Check if the selected product exist	*/
             if ($product === false) {
                 return __('This product does not exist', 'wpshop');
             }
             /** Get information about the product price	*/
             $product_price_check = wpshop_prices::get_product_price($product, 'check_only');
             if ($product_price_check !== true) {
                 return $product_price_check;
             }
             $the_quantity = 1;
             if (!empty($product_more_content['defined_variation_priority']) && $product_more_content['defined_variation_priority'] == 'combined' && !empty($product_more_content['variations']) && !empty($product_more_content['variations'][0])) {
                 /** Get the asked quantity for each product and check if there is enough stock	*/
                 $the_quantity = $quantity[$pid];
             } else {
                 /** Get the asked quantity for each product and check if there is enough stock	*/
                 $the_quantity = $quantity[$pid];
             }
             $quantity[$pid] = $the_quantity;
             $variation_id = 0;
             if (!empty($product_more_content) && !empty($product_more_content['variations']) && !empty($product_more_content['variations'][0]) && !empty($product_more_content['defined_variation_priority']) && $product_more_content['defined_variation_priority'] == 'combined') {
                 $variation_id = $product_more_content['variations'][0];
             }
             $quantity_to_check = !empty($_SESSION) && !empty($_SESSION['cart']) && !empty($_SESSION['cart']['order_items']) && !empty($_SESSION['cart']['order_items'][$pid]) && !empty($_SESSION['cart']['order_items'][$pid]['item_qty']) ? $_SESSION['cart']['order_items'][$pid]['item_qty'] + $the_quantity : $the_quantity;
             $wps_product_ctr = new wps_product_ctr();
             $product_stock = $wps_product_ctr->check_stock($product_more_content['id'], $quantity_to_check, $variation_id);
             if ($product_stock !== true) {
                 return $product_stock;
             }
         }
         $order_items[$pid]['product_id'] = $product_more_content['id'];
         $order_items[$pid]['product_qty'] = $quantity[$pid];
         /** For product with variation	*/
         $order_items[$pid]['product_variation_type'] = !empty($product_more_content['variation_priority']) ? $product_more_content['variation_priority'] : '';
         $order_items[$pid]['free_variation'] = !empty($product_more_content['free_variation']) ? $product_more_content['free_variation'] : '';
         $order_items[$pid]['product_variation'] = '';
         if (!empty($product_more_content['variations'])) {
             foreach ($product_more_content['variations'] as $variation_id) {
                 $order_items[$pid]['product_variation'][] = $variation_id;
             }
         }
     }
     $current_cart = !empty($order_meta) ? $order_meta : array();
     $order = $this->calcul_cart_information($order_items, $extra_params, $current_cart);
     if (empty($from_admin)) {
         self::store_cart_in_session($order);
         /** Store the cart into database for connected user */
         if (get_current_user_id()) {
             $this->persistent_cart_update();
         }
     } else {
         update_post_meta($order_id, '_order_postmeta', $order);
     }
     return 'success';
 }
 public static function product_mini_output($product_id, $category_id, $output_type = 'list', $current_item_position = 1, $grid_element_nb_per_line = WPSHOP_DISPLAY_GRID_ELEMENT_NUMBER_PER_LINE)
 {
     $content = '';
     $product_information = $product_class = '';
     /** Get the product thumbnail	*/
     $productThumbnail = wpshop_display::display_template_element('product_thumbnail_default', array());
     if (has_post_thumbnail($product_id)) {
         $productThumbnail = get_the_post_thumbnail($product_id, 'thumbnail');
     }
     $product = self::get_product_data($product_id);
     /**	Get the product information for output	*/
     if (!empty($product)) {
         $product_title = $product['post_title'];
         $product_name = $product['post_name'];
         $product_link = get_permalink($product_id);
         $product_more_informations = $product['product_content'];
         $product_excerpt = $product['product_excerpt'];
         if (strpos($product['product_content'], '<!--more-->')) {
             $post_content = explode('<!--more-->', $product['product_content']);
             $product_more_informations = $post_content[0];
         }
     } else {
         $productThumbnail = wpshop_display::display_template_element('product_thumbnail_default', array());
         $product_title = '<i>' . __('This product does not exist', 'wpshop') . '</i>';
         $product_link = '';
         $product_more_informations = '';
         $product_excerpt = '';
     }
     /** Retrieve product price	*/
     $price_attribute = wpshop_attributes::getElement('product_price', "'valid'", 'code');
     $price_display = wpshop_attributes::check_attribute_display($price_attribute->is_visible_in_front_listing, $product['custom_display'], 'attribute', 'product_price', 'mini_output');
     $catalog_options = get_option('wpshop_catalog_main_option', array());
     $productPrice = '';
     $wpshop_price_piloting_option = get_option('wpshop_shop_price_piloting');
     $check_product_price = wpshop_prices::check_product_price($product);
     $result_price_piloting = !empty($wpshop_price_piloting_option) && $wpshop_price_piloting_option == 'HT' ? $check_product_price['et'] : $check_product_price['ati'];
     if ($price_display && !(!empty($catalog_options) && (!empty($catalog_options['wpshop_catalog_empty_price_behaviour']) && $catalog_options['wpshop_catalog_empty_price_behaviour'] == 'yes') && $result_price_piloting == 0)) {
         $product_price_infos = get_post_meta($product_id, '_wps_price_infos', true);
         if (!empty($product_price_infos)) {
             $tpl_component_price = array();
             /** Price piloting **/
             $price_ploting = get_option('wpshop_shop_price_piloting');
             $tpl_component_price['CROSSED_OUT_PRICE'] = !empty($product_price_infos['CROSSED_OUT_PRICE']) ? (!empty($product_price_infos['PRICE_FROM']) ? __('Price from', 'wpshop') . ' ' : '') . wpshop_display::display_template_element('product_price_template_crossed_out_price', array('CROSSED_OUT_PRICE_VALUE' => $product_price_infos['CROSSED_OUT_PRICE'])) : '';
             $tpl_component_price['PRODUCT_PRICE'] = empty($product_price_infos['CROSSED_OUT_PRICE']) && !empty($product_price_infos['PRICE_FROM']) ? __('Price from', 'wpshop') . ' ' . $product_price_infos['PRODUCT_PRICE'] : $product_price_infos['PRODUCT_PRICE'];
             $tpl_component_price['MESSAGE_SAVE_MONEY'] = $product_price_infos['MESSAGE_SAVE_MONEY'];
             $tpl_component_price['TAX_PILOTING'] = !empty($price_ploting) && $price_ploting == 'HT' ? __('ET', 'wpshop') : '';
             $product_price_infos['MESSAGE_SAVE_MONEY'];
             $productPrice = wpshop_display::display_template_element('product_price_template_mini_output', $tpl_component_price);
         } else {
             $productPrice = wpshop_prices::get_product_price($product, 'price_display', array('mini_output', $output_type));
         }
     }
     /** Check if there is at less 1 product in stock	*/
     $wps_product_ctr = new wps_product_ctr();
     $productStock = $wps_product_ctr->check_stock($product_id, 1);
     $productStock = $productStock === true ? 1 : null;
     /** Define "Add to cart" button	*/
     $add_to_cart_button_display_state = wpshop_attributes::check_attribute_display(WPSHOP_DEFINED_SHOP_TYPE == 'sale' ? 'yes' : 'no', $product['custom_display'], 'product_action_button', 'add_to_cart', 'mini_output');
     $display_price_state_when_price_is_empty = false;
     if (empty($productPrice) && (!empty($catalog_options) || !empty($catalog_options['wpshop_catalog_empty_price_behaviour']) && $catalog_options['wpshop_catalog_empty_price_behaviour'] == 'yes')) {
         $display_price_state_when_price_is_empty = false;
     } else {
         if (!empty($productPrice)) {
             $display_price_state_when_price_is_empty = true;
         }
     }
     $add_to_cart_button = true === $add_to_cart_button_display_state && true === $display_price_state_when_price_is_empty ? self::display_add_to_cart_button($product_id, $productStock, 'mini') : '';
     $product_quantity_chooser_input = true === $add_to_cart_button_display_state ? wpshop_display::display_template_element('product_complete_sheet_quantity_chooser', array('PRODUCT_ID' => $product_id)) : '';
     /** Define "Ask a quotation" button	*/
     $quotation_button = self::display_quotation_button($product_id, !empty($product['quotation_allowed']) ? $product['quotation_allowed'] : null);
     $product_new_def = self::display_product_special_state('declare_new', $output_type, !empty($product['declare_new']) ? $product['declare_new'] : 'no', !empty($product['set_new_from']) ? $product['set_new_from'] : '', !empty($product['set_new_to']) ? $product['set_new_to'] : '');
     $product_new = $product_new_def['output'];
     $product_class .= $product_new_def['class'];
     $product_featured_def = self::display_product_special_state('highlight_product', $output_type, !empty($product['highlight_product']) ? $product['highlight_product'] : 'no', !empty($product['highlight_from']) ? $product['highlight_from'] : '', !empty($product['highlight_to']) ? $product['highlight_to'] : '');
     $product_featured = $product_featured_def['output'];
     $product_class .= $product_featured_def['class'];
     if (!($current_item_position % $grid_element_nb_per_line)) {
         $product_class .= ' wpshop_last_product_of_line';
     }
     if (!empty($product['product_id'])) {
         /** Template parameters	*/
         $template_part = 'product_mini_' . $output_type;
         $tpl_component = array();
         $tpl_component['PRODUCT_THUMBNAIL_MEDIUM'] = '<img src="' . WPSHOP_DEFAULT_PRODUCT_PICTURE . '" alt="" />';
         $tpl_component['PRODUCT_ID'] = $product_id;
         $tpl_component['PRODUCT_CLASS'] = $product_class;
         $tpl_component['PRODUCT_BUTTON_ADD_TO_CART'] = $add_to_cart_button;
         $tpl_component['PRODUCT_BUTTON_QUOTATION'] = $quotation_button;
         $tpl_component['PRODUCT_QUANTITY_CHOOSER'] = $product_quantity_chooser_input;
         $tpl_component['PRODUCT_BUTTONS'] = $tpl_component['PRODUCT_BUTTON_ADD_TO_CART'] . $tpl_component['PRODUCT_BUTTON_QUOTATION'];
         $tpl_component['PRODUCT_PRICE'] = $productPrice;
         $tpl_component['PRODUCT_PERMALINK'] = $product_link;
         $tpl_component['PRODUCT_TITLE'] = !empty($product_title) ? $product_title : '';
         $tpl_component['PRODUCT_NAME'] = $product_name;
         $tpl_component['PRODUCT_DESCRIPTION'] = $product_more_informations;
         $tpl_component['PRODUCT_IS_NEW'] = $product_new;
         $tpl_component['PRODUCT_IS_FEATURED'] = $product_featured;
         $tpl_component['PRODUCT_EXTRA_STATE'] = $tpl_component['PRODUCT_IS_NEW'] . $tpl_component['PRODUCT_IS_FEATURED'];
         $tpl_component['PRODUCT_THUMBNAIL'] = $productThumbnail;
         if (has_post_thumbnail($product_id)) {
             $image_attributes = wp_get_attachment_metadata(get_post_thumbnail_id($product_id));
             if (!empty($image_attributes) && !empty($image_attributes['sizes']) && is_array($image_attributes['sizes'])) {
                 $existing_image_sizes = get_intermediate_image_sizes();
                 foreach ($existing_image_sizes as $size_name) {
                     $tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] = wp_get_attachment_image(get_post_thumbnail_id($product_id), $size_name);
                     $tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] = !empty($tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)]) ? $tpl_component['PRODUCT_THUMBNAIL_' . strtoupper($size_name)] : WPSHOP_DEFAULT_PRODUCT_PICTURE;
                 }
             }
         }
         $tpl_component['PRODUCT_EXCERPT'] = $product_excerpt;
         $tpl_component['PRODUCT_OUTPUT_TYPE'] = $output_type;
         $tpl_component = apply_filters('wps-filter-product-mini-output', $tpl_component, $product_id);
         /** Build template	*/
         $tpl_way_to_take = wpshop_display::check_way_for_template($template_part);
         if ($tpl_way_to_take[0] && !empty($tpl_way_to_take[1])) {
             /**	Include the old way template part	*/
             ob_start();
             require wpshop_display::get_template_file($tpl_way_to_take[1]);
             $content = ob_get_contents();
             ob_end_clean();
         } else {
             $content = wpshop_display::display_template_element($template_part, $tpl_component);
         }
         unset($tpl_component);
     }
     return $content;
 }
 /**
  * Checking stock in differents checkout steps
  */
 function checking_stock()
 {
     if (!empty($_SESSION) && !empty($_SESSION['cart']) && !empty($_SESSION['cart']['order_items'])) {
         foreach ($_SESSION['cart']['order_items'] as $item_id => $item) {
             $wps_product = new wps_product_ctr();
             $checking = $wps_product->check_stock($item['item_id'], $item['item_qty'], $item_id);
             if ($checking !== true) {
                 unset($_SESSION['cart']['order_items'][$item_id]);
                 $wps_cart_ctr = new wps_cart();
                 $order = $wps_cart_ctr->calcul_cart_information(array());
                 $wps_cart_ctr->store_cart_in_session($order);
             }
         }
     }
 }
예제 #5
0
<?php

/**
 * Plugin Name: WPShop Products
 * Plugin URI: http://www.wpshop.fr/documentations/presentation-wpshop/
 * Description: WpShop Products
 * Version: 0.1
 * Author: Eoxia
 * Author URI: http://eoxia.com/
 */
if (!defined('WPSHOP_VERSION')) {
    die(__("You are not allowed to use this service.", 'wpshop'));
}
DEFINE('WPS_PRODUCT_VERSION', '2.0');
DEFINE('WPS_PRODUCT_DIR', basename(dirname(__FILE__)));
DEFINE('WPS_PRODUCT_PATH', dirname(__FILE__));
DEFINE('WPS_PRODUCT_URL', str_replace(str_replace("\\", "/", ABSPATH), site_url() . '/', str_replace("\\", "/", WPS_PRODUCT_PATH)));
/**	Define the templates directories	*/
DEFINE('WPS_PRODUCT_TEMPLATES_MAIN_DIR', WPS_PRODUCT_PATH . '/templates/');
include plugin_dir_path(__FILE__) . '/model/wps_product_mdl.php';
include plugin_dir_path(__FILE__) . '/controller/wps_product_ctr.php';
include plugin_dir_path(__FILE__) . '/controller/wps_product_ajax_ctr.01.php';
include plugin_dir_path(__FILE__) . '/controller/wps_product_administration_ctr.php';
$wps_product = new wps_product_ctr();
$wps_product->install_modules();
$wps_administration_product = new wps_product_administration_ctr();
 /**
  * Verifty post identifier and run a barcode generator
  * @param string $post_ID Ident of post
  */
 public function insert_barcode($post_ID)
 {
     global $wpdb;
     $types_with_barcode = array(WPSHOP_IDENTIFIER_PRODUCT, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT, WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION, WPSHOP_NEWTYPE_IDENTIFIER_COUPON, WPSHOP_NEWTYPE_IDENTIFIER_ORDER);
     $post = get_post($post_ID);
     /** Si c'est un type qui est dans le tableau $types_with_barcode */
     if (!empty($post->post_type) && in_array($post->post_type, $types_with_barcode)) {
         $conf = get_option('wps_barcode');
         $ref = '';
         if (strlen($post_ID) < 5) {
             $length = 5 - strlen($post_ID);
             for ($i = 0; $i < $length; $i++) {
                 $ref .= '0';
             }
         }
         $ref .= strval($post_ID);
         if ($conf['type'] === 'normal') {
             $array['normal'] = array('country' => $conf['normal_country_code'], 'enterprise' => $conf['normal_enterprise_code'], 'ID' => $ref);
         } else {
             if ($conf['type'] === 'internal') {
                 $pDate = new DateTime($post->post_date);
                 $date = $pDate->format('my');
                 if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT || $post->post_type === WPSHOP_IDENTIFIER_PRODUCT) {
                     $type = $conf['internal_product'];
                 } else {
                     if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_ORDER) {
                         $type = $conf['internal_invoice_client'];
                     } else {
                         if ($post->post_type === WPSHOP_NEWTYPE_IDENTIFIER_COUPON) {
                             $type = $conf['internal_coupons'];
                         } else {
                             $type = '000';
                         }
                     }
                 }
                 $array['internal'] = array('type' => $type, 'date' => $date, 'ID' => $ref);
             }
         }
         $barcode = $this->wps_generate_barcode($array);
         /** For add a product */
         if (isset($_REQUEST['wpshop_product_attribute']['varchar']['barcode'])) {
             if ($_REQUEST['wpshop_product_attribute']['varchar']['barcode'] !== '') {
                 wpeologs_ctr::log_datas_in_files('wps_barcode', array('object_id' => $post_ID, 'message' => sprintf(__('Change barcode: %s replacing %s for %s object ID', 'wps_barcode'), '<b>' . $_REQUEST['wpshop_product_attribute']['varchar']['barcode'] . '</b>', '<b>' . $barcode . '</b>', '<b>' . $post_ID . '</b>')), 0);
                 $barcode = $_REQUEST['wpshop_product_attribute']['varchar']['barcode'];
             } else {
                 wpeologs_ctr::log_datas_in_files('wps_barcode', array('object_id' => $post_ID, 'message' => sprintf(__('Adding barcode: %s for %s object ID', 'wps_barcode'), '<b>' . $barcode . '</b>', '<b>' . $post_ID . '</b>')), 0);
                 $_REQUEST['wpshop_product_attribute']['varchar']['barcode'] = $barcode;
             }
         } else {
             /** On met à jour l'attribut barcode */
             $products = new wps_product_ctr();
             $products->update_the_attribute_for_product($post_ID, 'varchar', 'barcode', $barcode);
         }
     }
 }