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; }
/** * 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'; }
/** * 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); } } } }