Example #1
0
/**
 * 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();
}