/** * Refresh Price in complete product sheet and Cart summary display */ function wpshop_ajax_wpshop_variation_selection() { global $wpdb; $wpshop_cart = new wps_cart(); $wpshop_products = new wpshop_products(); $response = ''; $response_status = $has_variation = false; $tpl_component = array(); //Sended vars $product_id = isset($_POST['wpshop_pdt']) ? intval(wpshop_tools::varSanitizer($_POST['wpshop_pdt'])) : null; $wpshop_variation_selected = isset($_POST['wpshop_variation']) ? $_POST['wpshop_variation'] : null; $wpshop_free_variation = isset($_POST['wpshop_free_variation']) ? $_POST['wpshop_free_variation'] : null; $wpshop_current_for_display = isset($_POST['wpshop_current_for_display']) ? $_POST['wpshop_current_for_display'] : null; $product_qty = isset($_POST['product_qty']) ? $_POST['product_qty'] : 1; // Check if variations exists if (!empty($wpshop_variation_selected) || !empty($wpshop_free_variation)) { //Recover all selected variations $variations_selected = array(); if (!empty($wpshop_variation_selected)) { foreach ($wpshop_variation_selected as $selected_variation) { $variation_definition = explode('-_variation_val_-', $selected_variation); $variations_selected[$variation_definition[0]] = $variation_definition[1]; } } // Check variations priority $product_with_variation = wpshop_products::get_variation_by_priority($variations_selected, $product_id); // Check if $product_with_variation have variations if (!empty($product_with_variation[$product_id]['variations']) || !empty($wpshop_free_variation)) { $formatted_product = $wpshop_cart->prepare_product_to_add_to_cart($product_id, $product_qty, $variations_selected); $product_to_add_to_cart = $formatted_product[0]; foreach ($formatted_product[0] as $pid => $product_more_content) { $order_items[$pid]['product_id'] = $product_more_content['id']; /** 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; } } } // If Product list is not empty, add products to order if (!empty($order_items)) { foreach ($order_items as $product_id => $d) { $product_key = $product_id; // Formate datas $product_id = $head_product_id = $d['product_id']; $product_variation = !empty($d['product_variation']) ? $d['product_variation'] : null; // If product is a single variation product if (!empty($product_variation) && count($product_variation) == 1) { $product_id = $product_variation[0]; } // Construct final product $product = wpshop_products::get_product_data($d['product_id'], true); $the_product = array_merge(array('product_id' => $d['product_id'], 'product_qty' => 1), $product); // Add variation to product into cart for storage if (!empty($product_variation)) { $the_product = wpshop_products::get_variation_price_behaviour($the_product, $product_variation, $head_product_id, array('type' => $d['product_variation_type'])); } // Free Variations Checking if (!empty($d['free_variation'])) { $the_product['item_meta']['free_variation'] = $d['free_variation']; $head_product_id = $the_product['product_id']; } // If product is a variation, we check parent product general if (get_post_type($the_product['product_id']) == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT_VARIATION) { $parent_def = wpshop_products::get_parent_variation($the_product['product_id']); if (!empty($parent_def) && !empty($parent_def['parent_post'])) { $variation_def = get_post_meta($parent_def['parent_post']->ID, '_wpshop_variation_defining', true); $parent_meta = $parent_def['parent_post_meta']; if (!empty($variation_def) && !empty($variation_def['options']) && !empty($variation_def['options']['priority']) && in_array('combined', $variation_def['options']['priority']) && !empty($variation_def['options']['price_behaviour']) && in_array('addition', $variation_def['options']['price_behaviour']) && !empty($variation_def['attributes']) && count($variation_def['attributes']) > 1) { $the_product['product_price'] += number_format(str_replace(',', '.', $parent_meta['product_price']), 2, '.', ''); $the_product['price_ht'] += number_format(str_replace(',', '.', $parent_meta['price_ht']), 2, '.', ''); $the_product['tva'] += number_format(str_replace(',', '.', $parent_meta['tva']), 2, '.', ''); } } } } } if (!empty($the_product) && empty($the_product['price_ttc_before_discount']) && empty($the_product['price_ht_before_discount'])) { $price_infos = wpshop_prices::check_product_price($the_product, true); if (!empty($price_infos['discount']['discount_exist'])) { $the_product['price_ttc_before_discount'] = $the_product['product_price']; $the_product['price_ht_before_discount'] = $the_product['price_ht']; } $the_product['price_ht'] = !empty($price_infos['discount']) && !empty($price_infos['discount']['discount_exist']) && $price_infos['discount']['discount_exist'] ? $price_infos['discount']['discount_et_price'] : $price_infos['et']; $the_product['product_price'] = !empty($price_infos['discount']) && !empty($price_infos['discount']['discount_exist']) && $price_infos['discount']['discount_exist'] ? $price_infos['discount']['discount_ati_price'] : $price_infos['ati']; $the_product['tva'] = !empty($price_infos['discount']) && !empty($price_infos['discount']['discount_exist']) && $price_infos['discount']['discount_exist'] ? $price_infos['discount']['discount_tva'] : $price_infos['tva']; } $product = wpshop_products::get_product_data($product_id, true); // Add free variations to product if (!empty($wpshop_free_variation)) { $the_product['item_meta']['free_variation'] = $wpshop_free_variation; } // Change picture if have a selected variation $the_selected_variation = !empty($product_with_variation) && !empty($product_with_variation[$head_product_id]) && !empty($product_with_variation[$head_product_id]['variations']) ? $product_with_variation[$head_product_id]['variations'] : null; $response['wps_product_image'] = $wpshop_products->wps_selected_variation_picture($head_product_id, $the_selected_variation); // Price Display $price_attribute = wpshop_attributes::getElement('product_price', "'valid'", 'code'); $price_display = wpshop_attributes::check_attribute_display($price_attribute->is_visible_in_front, $product['custom_display'], 'attribute', 'product_price', 'complete_sheet'); $productPrice = ''; if ($price_display) { $response['product_price_output'] = wpshop_prices::get_product_price($the_product, 'price_display', 'complete_sheet', false, true); } //Get Summary cart $response['product_output'] = $wpshop_products->wps_get_summary_variations_product($product_id, $the_product, !empty($wpshop_variation_selected) || !empty($wpshop_free_variation) ? true : false); $response_status = true; } else { //Product without variations $product_data = wpshop_products::get_product_data($product_id); $response['product_price_output'] = wpshop_prices::get_product_price($product_data, 'price_display', 'complete_sheet'); } } echo json_encode(array($response_status, $response)); wp_die(); }
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; }
/** * Build an array with the different items to add to an order * * @param array $products The item list to add to the order * * @return array $item_list The item to add to order */ function add_product_to_order($product) { global $wpdb; if (!empty($product) && empty($product['price_ttc_before_discount']) && empty($product['price_ht_before_discount'])) { $price_infos = wpshop_prices::check_product_price($product, true); $product['price_ht'] = !empty($price_infos['discount']) && !empty($price_infos['discount']['discount_exist']) && $price_infos['discount']['discount_exist'] ? $price_infos['discount']['discount_et_price'] : $price_infos['et']; $product['product_price'] = !empty($price_infos['discount']) && !empty($price_infos['discount']['discount_exist']) && $price_infos['discount']['discount_exist'] ? $price_infos['discount']['discount_ati_price'] : $price_infos['ati']; $product['tva'] = !empty($price_infos['discount']) && !empty($price_infos['discount']['discount_exist']) && $price_infos['discount']['discount_exist'] ? $price_infos['discount']['discount_tva'] : $price_infos['tva']; } $price_piloting = get_option('wpshop_shop_price_piloting'); if (!empty($price_piloting) && $price_piloting == 'HT') { $total_ht = $product['price_ht'] * $product['product_qty']; $tva_total_amount = $total_ht * ($product['tx_tva'] / 100); $total_ttc = $total_ht + $tva_total_amount; } else { $total_ttc = $product['product_price'] * $product['product_qty']; $total_ht = $total_ttc / (1 + $product['tx_tva'] / 100); $tva_total_amount = $total_ttc - $total_ht; } $tva = !empty($product[WPSHOP_PRODUCT_PRICE_TAX]) ? $product[WPSHOP_PRODUCT_PRICE_TAX] : null; $item_discount_type = $item_discount_value = $item_discount_amount = 0; $d_amount = !empty($product) && !empty($product['discount_amount']) ? wpshop_tools::formate_number($product['discount_amount']) : null; $d_rate = !empty($product) && !empty($product['discount_rate']) ? wpshop_tools::formate_number($product['discount_rate']) : null; $d_special = !empty($product) && !empty($product['special_price']) ? wpshop_tools::formate_number($product['special_price']) : null; if (!empty($d_amount)) { $item_discount_type = 'discount_amount'; $item_discount_amount = $product['discount_amount']; $item_discount_value = $product['discount_amount']; } elseif (!empty($d_rate)) { $item_discount_type = 'discount_rate'; $item_discount_amount = $product['discount_rate']; $item_discount_value = $product['discount_rate']; } elseif (!empty($d_special)) { $item_discount_type = 'special_price'; $item_discount_amount = $product['special_price']; $item_discount_value = $product['special_price']; } $item = array('item_id' => $product['product_id'], 'item_ref' => !empty($product['product_reference']) ? $product['product_reference'] : null, 'item_name' => !empty($product['product_name']) ? $product['product_name'] : 'wpshop_product_' . $product['product_id'], 'item_qty' => $product['product_qty'], 'item_pu_ht' => $product['price_ht'], 'item_pu_ttc' => $product['product_price'], 'item_ecotaxe_ht' => 0, 'item_ecotaxe_tva' => 19.6, 'item_ecotaxe_ttc' => 0, 'item_discount_type' => $item_discount_type, 'item_discount_value' => $item_discount_value, 'item_discount_amount' => $item_discount_amount, 'item_tva_rate' => $tva, 'item_tva_amount' => $product['tva'], 'item_total_ht' => $total_ht, 'item_tva_total_amount' => $tva_total_amount, 'item_total_ttc' => $total_ttc, 'item_meta' => !empty($product['item_meta']) ? $product['item_meta'] : array()); $array_not_to_do = array(WPSHOP_PRODUCT_PRICE_HT, WPSHOP_PRODUCT_PRICE_TTC, WPSHOP_PRODUCT_PRICE_TAX_AMOUNT, 'product_qty', WPSHOP_PRODUCT_PRICE_TAX, 'product_id', 'product_reference', 'product_name', 'variations'); if (!empty($product['item_meta'])) { foreach ($product['item_meta'] as $key => $value) { if (!isset($item['item_' . $key]) && !in_array($key, $array_not_to_do) && !empty($product[$key])) { $item['item_' . $key] = $product[$key]; } } } /** Check if it's a variation product **/ if (!empty($product) && !empty($product['item_meta']) && !empty($product['item_meta']['variations'])) { foreach ($product['item_meta']['variations'] as $k => $variation) { $product_variation_def = get_post_meta($k, '_wpshop_variations_attribute_def', true); if (!empty($product_variation_def)) { foreach ($product_variation_def as $attribute_code => $variation_id) { $variation_attribute_def = wpshop_attributes::getElement($attribute_code, '"valid"', 'code'); if (!empty($variation_attribute_def)) { $item['item_meta']['variation_definition'][$attribute_code]['NAME'] = $variation_attribute_def->frontend_label; if ($variation_attribute_def->data_type_to_use == 'custom') { $query = $wpdb->prepare('SELECT label FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS . ' WHERE id=%d', $variation_id); $variation_name = $wpdb->get_var($query); } else { $variation_post = get_post($variation_id); $variation_name = $variation_post->post_title; } $item['item_meta']['variation_definition'][$attribute_code]['UNSTYLED_VALUE'] = $variation_name; $item['item_meta']['variation_definition'][$attribute_code]['VALUE'] = $variation_name; } } } } } else { /** Check if it's product with one variation **/ $product_variation_def = get_post_meta($product['product_id'], '_wpshop_variations_attribute_def', true); if (!empty($product_variation_def)) { foreach ($product_variation_def as $attribute_code => $variation_id) { $variation_attribute_def = wpshop_attributes::getElement($attribute_code, '"valid"', 'code'); if (!empty($variation_attribute_def)) { $item['item_meta']['variation_definition'][$attribute_code]['NAME'] = $variation_attribute_def->frontend_label; if ($variation_attribute_def->data_type_to_use == 'custom') { $query = $wpdb->prepare('SELECT label FROM ' . WPSHOP_DBT_ATTRIBUTE_VALUES_OPTIONS . ' WHERE id=%d', $variation_id); $variation_name = $wpdb->get_var($query); } else { $variation_post = get_post($variation_id); $variation_name = $variation_post->post_title; } $item['item_meta']['variation_definition'][$attribute_code]['UNSTYLED_VALUE'] = $variation_name; $item['item_meta']['variation_definition'][$attribute_code]['VALUE'] = $variation_name; } } } } return $item; }
/** * Save the price which is displayed on website */ function save_displayed_price_meta() { if (!empty($_POST) && !empty($_POST['ID']) && !empty($_POST['post_type']) && $_POST['post_type'] == WPSHOP_NEWTYPE_IDENTIFIER_PRODUCT) { $price_piloting = get_option('wpshop_shop_price_piloting'); $product_data = wpshop_products::get_product_data($_POST['ID']); $price_infos = wpshop_prices::check_product_price($product_data); if (!empty($price_infos)) { if (!empty($price_infos['discount']) && !empty($price_infos['discount']['discount_exist'])) { $displayed_price = !empty($price_piloting) && $price_piloting == 'HT' ? $price_infos['discount']['discount_et_price'] : $price_infos['discount']['discount_ati_price']; } else { if (!empty($price_infos['fork_price']) && !empty($price_infos['fork_price']['have_fork_price'])) { $displayed_price = $price_infos['fork_price']['min_product_price']; } else { $displayed_price = !empty($price_piloting) && $price_piloting == 'HT' ? $price_infos['et'] : $price_infos['ati']; } } update_post_meta($_POST['ID'], '_wpshop_displayed_price', number_format($displayed_price, 2, '.', '')); } } }