/**
 * Add to cart action
 *
 * Checks for a valid request, does validation (via hooks) and then redirects if valid.
 *
 * @access public
 * @param bool $url (default: false)
 * @return void
 */
function woocommerce_add_to_cart_action($url = false)
{
    if (empty($_REQUEST['add-to-cart']) || !is_numeric($_REQUEST['add-to-cart'])) {
        return;
    }
    global $woocommerce;
    $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_REQUEST['add-to-cart']));
    $was_added_to_cart = false;
    $added_to_cart = array();
    $adding_to_cart = get_product($product_id);
    $add_to_cart_handler = apply_filters('woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart);
    // Variable product handling
    if ('variable' === $add_to_cart_handler) {
        $variation_id = empty($_REQUEST['variation_id']) ? '' : absint($_REQUEST['variation_id']);
        $quantity = empty($_REQUEST['quantity']) ? 1 : apply_filters('woocommerce_stock_amount', $_REQUEST['quantity']);
        $all_variations_set = true;
        $variations = array();
        // Only allow integer variation ID - if its not set, redirect to the product page
        if (empty($variation_id)) {
            $woocommerce->add_error(__('Please choose product options…', 'woocommerce'));
            return;
        }
        $attributes = $adding_to_cart->get_attributes();
        $variation = get_product($variation_id);
        // Verify all attributes
        foreach ($attributes as $attribute) {
            if (!$attribute['is_variation']) {
                continue;
            }
            $taxonomy = 'attribute_' . sanitize_title($attribute['name']);
            if (!empty($_REQUEST[$taxonomy])) {
                // Get value from post data
                // Don't use woocommerce_clean as it destroys sanitized characters
                $value = sanitize_title(trim(stripslashes($_REQUEST[$taxonomy])));
                // Get valid value from variation
                $valid_value = $variation->variation_data[$taxonomy];
                // Allow if valid
                if ($valid_value == '' || $valid_value == $value) {
                    if ($attribute['is_taxonomy']) {
                        $variations[esc_html($attribute['name'])] = $value;
                    } else {
                        // For custom attributes, get the name from the slug
                        $options = array_map('trim', explode('|', $attribute['value']));
                        foreach ($options as $option) {
                            if (sanitize_title($option) == $value) {
                                $value = $option;
                                break;
                            }
                        }
                        $variations[esc_html($attribute['name'])] = $value;
                    }
                    continue;
                }
            }
            $all_variations_set = false;
        }
        if ($all_variations_set) {
            // Add to cart validation
            $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations);
            if ($passed_validation) {
                if ($woocommerce->cart->add_to_cart($product_id, $quantity, $variation_id, $variations)) {
                    woocommerce_add_to_cart_message($product_id);
                    $was_added_to_cart = true;
                    $added_to_cart[] = $product_id;
                }
            }
        } else {
            $woocommerce->add_error(__('Please choose product options…', 'woocommerce'));
            return;
        }
        // Grouped Products
    } elseif ('grouped' === $add_to_cart_handler) {
        if (!empty($_REQUEST['quantity']) && is_array($_REQUEST['quantity'])) {
            $quantity_set = false;
            foreach ($_REQUEST['quantity'] as $item => $quantity) {
                if ($quantity <= 0) {
                    continue;
                }
                $quantity_set = true;
                // Add to cart validation
                $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $item, $quantity);
                if ($passed_validation) {
                    if ($woocommerce->cart->add_to_cart($item, $quantity)) {
                        $was_added_to_cart = true;
                        $added_to_cart[] = $item;
                    }
                }
            }
            if ($was_added_to_cart) {
                woocommerce_add_to_cart_message($added_to_cart);
            }
            if (!$was_added_to_cart && !$quantity_set) {
                $woocommerce->add_error(__('Please choose the quantity of items you wish to add to your cart&hellip;', 'woocommerce'));
                return;
            }
        } elseif ($product_id) {
            /* Link on product archives */
            $woocommerce->add_error(__('Please choose a product to add to your cart&hellip;', 'woocommerce'));
            return;
        }
        // Simple Products
    } else {
        $quantity = empty($_REQUEST['quantity']) ? 1 : apply_filters('woocommerce_stock_amount', $_REQUEST['quantity']);
        // Add to cart validation
        $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
        if ($passed_validation) {
            // Add the product to the cart
            if ($woocommerce->cart->add_to_cart($product_id, $quantity)) {
                woocommerce_add_to_cart_message($product_id);
                $was_added_to_cart = true;
                $added_to_cart[] = $product_id;
            }
        }
    }
    // If we added the product to the cart we can now do a redirect, otherwise just continue loading the page to show errors
    if ($was_added_to_cart) {
        $url = apply_filters('add_to_cart_redirect', $url);
        // If has custom URL redirect there
        if ($url) {
            wp_safe_redirect($url);
            exit;
        } elseif (get_option('woocommerce_cart_redirect_after_add') == 'yes' && $woocommerce->error_count() == 0) {
            wp_safe_redirect($woocommerce->cart->get_cart_url());
            exit;
        } elseif (wp_get_referer()) {
            wp_safe_redirect(add_query_arg('added-to-cart', implode(',', $added_to_cart), remove_query_arg(array('add-to-cart', 'quantity', 'product_id'), wp_get_referer())));
            exit;
        }
    }
}
Ejemplo n.º 2
0
/**
 * AJAX add to cart
 *
 * @access public
 * @return void
 */
function woocommerce_ajax_add_to_cart()
{
    global $woocommerce;
    $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['product_id']));
    $quantity = empty($_POST['quantity']) ? 1 : apply_filters('woocommerce_stock_amount', $_POST['quantity']);
    $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
    if ($passed_validation && $woocommerce->cart->add_to_cart($product_id, $quantity)) {
        do_action('woocommerce_ajax_added_to_cart', $product_id);
        if (get_option('woocommerce_cart_redirect_after_add') == 'yes') {
            woocommerce_add_to_cart_message($product_id);
            $woocommerce->set_messages();
        }
        // Return fragments
        woocommerce_get_refreshed_fragments();
    } else {
        header('Content-Type: application/json; charset=utf-8');
        // If there was an error adding to the cart, redirect to the product page to show any errors
        $data = array('error' => true, 'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink($product_id), $product_id));
        $woocommerce->set_messages();
        echo json_encode($data);
    }
    die;
}
Ejemplo n.º 3
0
        $redirect_url = get_permalink(icl_object_id(function_exists('wc_get_page_id') ? wc_get_page_id('cart') : woocommerce_get_page_id('cart')), 'page', true);
    } else {
        $redirect_url = get_permalink(function_exists('wc_get_page_id') ? wc_get_page_id('cart') : woocommerce_get_page_id('cart'));
    }
} else {
    $redirect_url = $yith_wcwl->get_wishlist_url();
}
//get the details of the product
$details = $yith_wcwl->get_product_details($_GET['wishlist_item_id']);
//add to the cart
if ($woocommerce->cart->add_to_cart($details[0]['prod_id'], 1)) {
    //$_SESSION['messages'] 	= sprintf( '<a href="%s" class="button">%s</a> %s', get_permalink( woocommerce_get_page_id( 'cart' ) ), __( 'View Cart &rarr;', 'yit' ), __( 'Product successfully added to the cart.', 'yit' ) );
    if (function_exists('wc_add_to_cart_message')) {
        wc_add_to_cart_message($details[0]['prod_id']);
    } else {
        woocommerce_add_to_cart_message($details[0]['prod_id']);
    }
    //$woocommerce->set_messages();
    if (get_option('yith_wcwl_remove_after_add_to_cart') == 'yes') {
        $yith_wcwl->remove($details[0]['ID']);
    }
    header("Location: {$redirect_url}");
} else {
    //if failed, redirect to wishlist page with errors
    if (function_exists('wc_get_notices')) {
        $_SESSION['errors'] = wc_get_notices("error");
    } else {
        $_SESSION['errors'] = $woocommerce->get_errors();
    }
    header("Location: {$error_link_url}");
}
Ejemplo n.º 4
0
/**
 * Add to cart action
 *
 * Checks for a valid request, does validation (via hooks) and then redirects if valid
 **/
function woocommerce_add_to_cart_action($url = false)
{
    global $woocommerce;
    if (empty($_REQUEST['add-to-cart'])) {
        return;
    }
    $added_to_cart = false;
    switch ($_REQUEST['add-to-cart']) {
        // Variable Products
        case 'variation':
            // Only allow integer variation ID - if its not set, redirect to the product page
            if (empty($_REQUEST['variation_id']) || !is_numeric($_REQUEST['variation_id']) || $_REQUEST['variation_id'] < 1) {
                $woocommerce->add_error(__('Please choose product options&hellip;', 'woocommerce'));
                wp_redirect(apply_filters('woocommerce_add_to_cart_product_id', get_permalink($_REQUEST['product_id'])));
                exit;
            }
            // Get product ID to add and quantity
            $product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $_REQUEST['product_id']);
            $variation_id = (int) $_REQUEST['variation_id'];
            $quantity = isset($_REQUEST['quantity']) ? (int) $_REQUEST['quantity'] : 1;
            $attributes = (array) maybe_unserialize(get_post_meta($product_id, '_product_attributes', true));
            $variations = array();
            $all_variations_set = true;
            // Verify all attributes for the variable product were set
            foreach ($attributes as $attribute) {
                if (!$attribute['is_variation']) {
                    continue;
                }
                $taxonomy = 'attribute_' . sanitize_title($attribute['name']);
                if (!empty($_REQUEST[$taxonomy])) {
                    // Get value from post data
                    $value = esc_attr(stripslashes($_REQUEST[$taxonomy]));
                    // Use name so it looks nicer in the cart widget/order page etc - instead of a sanitized string
                    $variations[esc_attr($attribute['name'])] = $value;
                } else {
                    $all_variations_set = false;
                }
            }
            if ($all_variations_set) {
                // Add to cart validation
                $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
                if ($passed_validation) {
                    if ($woocommerce->cart->add_to_cart($product_id, $quantity, $variation_id, $variations)) {
                        woocommerce_add_to_cart_message();
                        $added_to_cart = true;
                    }
                }
            } else {
                $woocommerce->add_error(__('Please choose product options&hellip;', 'woocommerce'));
                wp_redirect(apply_filters('woocommerce_add_to_cart_product_id', get_permalink($_REQUEST['product_id'])));
                exit;
            }
            break;
            // Grouped Products
        // Grouped Products
        case 'group':
            if (isset($_REQUEST['quantity']) && is_array($_REQUEST['quantity'])) {
                $quantity_set = false;
                foreach ($_REQUEST['quantity'] as $item => $quantity) {
                    if ($quantity < 1) {
                        continue;
                    }
                    $quantity_set = true;
                    // Add to cart validation
                    $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $item, $quantity);
                    if ($passed_validation) {
                        if ($woocommerce->cart->add_to_cart($item, $quantity)) {
                            woocommerce_add_to_cart_message();
                            $added_to_cart = true;
                        }
                    }
                }
                if (!$added_to_cart && !$quantity_set) {
                    $woocommerce->add_error(__('Please choose a quantity&hellip;', 'woocommerce'));
                    wp_redirect(apply_filters('woocommerce_add_to_cart_product_id', get_permalink($_REQUEST['product_id'])));
                    exit;
                }
            } elseif ($_REQUEST['product_id']) {
                /* Link on product archives */
                $woocommerce->add_error(__('Please choose a product&hellip;', 'woocommerce'));
                wp_redirect(get_permalink($_REQUEST['product_id']));
                exit;
            }
            break;
            // Simple Products - add-to-cart contains product ID
        // Simple Products - add-to-cart contains product ID
        default:
            // Only allow integers
            if (!is_numeric($_REQUEST['add-to-cart'])) {
                break;
            }
            // Get product ID to add and quantity
            $product_id = (int) $_REQUEST['add-to-cart'];
            $quantity = isset($_REQUEST['quantity']) ? (int) $_REQUEST['quantity'] : 1;
            // Add to cart validation
            $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
            if ($passed_validation) {
                // Add the product to the cart
                if ($woocommerce->cart->add_to_cart($_REQUEST['add-to-cart'], $quantity)) {
                    woocommerce_add_to_cart_message();
                    $added_to_cart = true;
                }
            }
            break;
    }
    // If we added the product to the cart we can now do a redirect, otherwise just continue loading the page to show errors
    if ($added_to_cart) {
        $url = apply_filters('add_to_cart_redirect', $url);
        // If has custom URL redirect there
        if ($url) {
            wp_safe_redirect($url);
            exit;
        } elseif (get_option('woocommerce_cart_redirect_after_add') == 'yes' && $woocommerce->error_count() == 0) {
            wp_safe_redirect($woocommerce->cart->get_cart_url());
            exit;
        }
    }
}
/**
 * Add to cart
 **/
function woocommerce_add_to_cart_action($url = false)
{
    global $woocommerce;
    if (empty($_GET['add-to-cart']) || !$woocommerce->verify_nonce('add_to_cart', '_GET')) {
        return;
    }
    if (is_numeric($_GET['add-to-cart'])) {
        //single product
        $quantity = isset($_POST['quantity']) ? (int) $_POST['quantity'] : 1;
        // Add to the cart
        if ($woocommerce->cart->add_to_cart($_GET['add-to-cart'], $quantity)) {
            woocommerce_add_to_cart_message();
        }
    } elseif ($_GET['add-to-cart'] == 'variation') {
        // Variation add to cart
        if (empty($_POST['variation_id']) || !is_numeric($_POST['variation_id'])) {
            $woocommerce->add_error(__('Please choose product options&hellip;', 'woothemes'));
            wp_safe_redirect(get_permalink($_GET['product']));
            exit;
        } else {
            $product_id = (int) $_GET['product'];
            $variation_id = (int) $_POST['variation_id'];
            $quantity = isset($_POST['quantity']) ? (int) $_POST['quantity'] : 1;
            $attributes = (array) maybe_unserialize(get_post_meta($product_id, 'product_attributes', true));
            $variations = array();
            $all_variations_set = true;
            foreach ($attributes as $attribute) {
                if (!$attribute['is_variation']) {
                    continue;
                }
                $taxonomy = 'attribute_' . sanitize_title($attribute['name']);
                if (!empty($_POST[$taxonomy])) {
                    // Get value from post data
                    $value = esc_attr(stripslashes($_POST[$taxonomy]));
                    // Use name so it looks nicer in the cart widget/order page etc - instead of a sanitized string
                    $variations[esc_attr($attribute['name'])] = $value;
                } else {
                    $all_variations_set = false;
                }
            }
            if ($all_variations_set && $variation_id > 0) {
                // Add to cart
                if ($woocommerce->cart->add_to_cart($product_id, $quantity, $variation_id, $variations)) {
                    woocommerce_add_to_cart_message();
                }
            } else {
                $woocommerce->add_error(__('Please choose product options&hellip;', 'woothemes'));
                wp_redirect(get_permalink($_GET['product']));
                exit;
            }
        }
    } elseif ($_GET['add-to-cart'] == 'group') {
        // Group add to cart
        if (isset($_POST['quantity']) && is_array($_POST['quantity'])) {
            $total_quantity = 0;
            foreach ($_POST['quantity'] as $item => $quantity) {
                if ($quantity > 0) {
                    if ($woocommerce->cart->add_to_cart($item, $quantity)) {
                        woocommerce_add_to_cart_message();
                    }
                    $total_quantity = $total_quantity + $quantity;
                }
            }
            if ($total_quantity == 0) {
                $woocommerce->add_error(__('Please choose a quantity&hellip;', 'woothemes'));
            }
        } elseif ($_GET['product']) {
            /* Link on product pages */
            $woocommerce->add_error(__('Please choose a product&hellip;', 'woothemes'));
            wp_redirect(get_permalink($_GET['product']));
            exit;
        }
    }
    $url = apply_filters('add_to_cart_redirect', $url);
    // If has custom URL redirect there
    if ($url) {
        wp_safe_redirect($url);
        exit;
    } elseif (get_option('woocommerce_cart_redirect_after_add') == 'yes' && $woocommerce->error_count() == 0) {
        wp_safe_redirect($woocommerce->cart->get_cart_url());
        exit;
    } elseif (wp_get_referer()) {
        wp_safe_redirect(wp_get_referer());
        exit;
    } else {
        wp_safe_redirect(home_url());
        exit;
    }
}
Ejemplo n.º 6
0
/**
 * Add to cart action
 *
 * Checks for a valid request, does validation (via hooks) and then redirects if valid.
 *
 * @access public
 * @param bool $url (default: false)
 * @return void
 */
function woocommerce_add_to_cart_action($url = false)
{
    global $woocommerce;
    if (empty($_REQUEST['add-to-cart'])) {
        return;
    }
    $added_to_cart = false;
    switch ($_REQUEST['add-to-cart']) {
        // Variable Products
        case 'variation':
            // Only allow integer variation ID - if its not set, redirect to the product page
            if (empty($_REQUEST['variation_id']) || !is_numeric($_REQUEST['variation_id']) || $_REQUEST['variation_id'] < 1) {
                $woocommerce->add_error(__('Please choose product options&hellip;', 'woocommerce'));
                wp_redirect(apply_filters('woocommerce_add_to_cart_product_id', get_permalink($_REQUEST['product_id'])));
                exit;
            }
            // Get product ID to add and quantity
            $product_id = (int) apply_filters('woocommerce_add_to_cart_product_id', $_REQUEST['product_id']);
            $variation_id = (int) $_REQUEST['variation_id'];
            $quantity = isset($_REQUEST['quantity']) ? (int) $_REQUEST['quantity'] : 1;
            $attributes = (array) maybe_unserialize(get_post_meta($product_id, '_product_attributes', true));
            $variations = array();
            $all_variations_set = true;
            // Verify all attributes for the variable product were set
            foreach ($attributes as $attribute) {
                if (!$attribute['is_variation']) {
                    continue;
                }
                $taxonomy = 'attribute_' . sanitize_title($attribute['name']);
                if (!empty($_REQUEST[$taxonomy])) {
                    // Get value from post data
                    $value = esc_attr(stripslashes($_REQUEST[$taxonomy]));
                    // Use name so it looks nicer in the cart widget/order page etc - instead of a sanitized string
                    $variations[esc_attr($attribute['name'])] = $value;
                } else {
                    $all_variations_set = false;
                }
            }
            $add2cart1 = "";
            if (isset($_REQUEST['add2cart1'])) {
                $add2cart1 = $_REQUEST['add2cart1'];
            }
            //			if($add2cart1!="yes")
            //			{
            if (isset($_REQUEST['pcode'])) {
                $_SESSION['pcode'] = $_REQUEST['pcode'];
            }
            if (isset($_REQUEST['pickUp'])) {
                $pickUp = $_REQUEST['pickUp'];
            }
            if (isset($_REQUEST['Delivery'])) {
                $delivery1 = $_REQUEST['Delivery'];
            }
            if (isset($_REQUEST['bigpostdp'])) {
                $bigpostdp = $_REQUEST['bigpostdp'];
            }
            if ($pickUp == "on") {
                $_SESSION['pickUp'] = "on";
                $_SESSION['Delivery'] = "off";
                $_SESSION['BigPostDB'] = "off";
                $ware1 = $_REQUEST['warehouse'];
                if ($ware1 == "yes") {
                    $ship_pu = (int) $_REQUEST['freightprice'];
                    if (isset($_SESSION['pickup_ship']) == false) {
                        $_SESSION['pickup_ship'] = $ship_pu;
                        $_SESSION['pickup_shipname'] = get_the_title($product_id) . ": " . $_REQUEST['warehouse2'];
                    } else {
                        $_SESSION['pickup_ship'] = $_SESSION['pickup_ship'] . "~" . $ship_pu;
                        $_SESSION['pickup_shipname'] = $_SESSION['pickup_shipname'] . "~" . get_the_title($product_id) . ": " . $_REQUEST['warehouse2'];
                    }
                }
                if ($ware1 == "no") {
                    $ship_pu = (int) $_REQUEST['freightprice'];
                    if (isset($_SESSION['pickup_ship']) == false) {
                        $_SESSION['pickup_ship'] = $ship_pu;
                        $_SESSION['pickup_shipname'] = get_the_title($product_id) . ": Melbourne";
                    } else {
                        $_SESSION['pickup_ship'] = $_SESSION['pickup_ship'] . "~" . $ship_pu;
                        $_SESSION['pickup_shipname'] = $_SESSION['pickup_shipname'] . "~" . get_the_title($product_id) . ": Melbourne";
                    }
                }
            }
            if ($delivery1 == "on") {
                $_SESSION['pickUp'] = "off";
                $_SESSION['Delivery'] = "on";
                $_SESSION['BigPostDB'] = "off";
                $ware1 = $_REQUEST['warehouse'];
                $ship_delivery = (double) $_REQUEST['freightprice'];
                if ($_REQUEST['hdsatl'] != "no") {
                    if (isset($_SESSION['hdsatl']) == false) {
                        $_SESSION['hdsatl'] = get_the_title($product_id) . "(#" . $_POST['pdsku'] . ")";
                    } else {
                        $_SESSION['hdsatl'] = $_SESSION['hdsatl'] . " , " . get_the_title($product_id) . "(#" . $_POST['pdsku'] . ")";
                    }
                }
                if ($ware1 == "yes") {
                    if (isset($_SESSION['delivery_warehouse']) == false) {
                        $_SESSION['ship_delivery'] = $ship_delivery;
                        $_SESSION['delivery_warehouse'] = get_the_title($product_id) . ": " . $_SESSION['pcode'];
                    } else {
                        $_SESSION['ship_delivery'] = $_SESSION['ship_delivery'] . "~" . $ship_delivery;
                        $_SESSION['delivery_warehouse'] = $_SESSION['delivery_warehouse'] . "~" . get_the_title($product_id) . ": " . $_SESSION['pcode'];
                    }
                }
                if ($ware1 == "no") {
                    if (isset($_SESSION['delivery_warehouse']) == false) {
                        $_SESSION['ship_delivery'] = $ship_delivery;
                        $_SESSION['delivery_warehouse'] = get_the_title($product_id) . ": Melbourne";
                    } else {
                        $_SESSION['ship_delivery'] = $_SESSION['ship_delivery'] . "~" . $ship_delivery;
                        $_SESSION['delivery_warehouse'] = $_SESSION['delivery_warehouse'] . "~" . get_the_title($product_id) . ": Melbourne";
                    }
                }
            }
            if ($bigpostdp == "on") {
                $_SESSION['pickUp'] = "off";
                $_SESSION['Delivery'] = "off";
                $_SESSION['BigPostDB'] = "on";
                if ($_REQUEST['freightprice'] == "0") {
                    $ship_bigdp = 0;
                } else {
                    $ship_bigdp = (double) $_REQUEST['freightprice'];
                }
                if (isset($_SESSION['bigpost_suburb']) == false) {
                    $bigpost_suburb = $_REQUEST['bigpost_suburb'];
                    $_SESSION['bigpost_suburb'] = $bigpost_suburb;
                }
                ///echo "<script>alert('" . $product_id . "');</script>";
                if (isset($_SESSION['ship_bigdp']) == false) {
                    $_SESSION['ship_bigdp'] = $ship_bigdp;
                    $_SESSION['BigPostDB_shipname'] = get_the_title($product_id) . " : " . $bigpost_suburb;
                    $_SESSION['BigPost_address'] = $_REQUEST['bigpost_add'];
                } else {
                    $_SESSION['ship_bigdp'] = $_SESSION['ship_bigdp'] . "~" . $ship_bigdp;
                    $_SESSION['BigPostDB_shipname'] = $_SESSION['BigPostDB_shipname'] . "~" . get_the_title($product_id) . " : " . $_SESSION['bigpost_suburb'];
                    $_SESSION['BigPost_address'] = $_SESSION['BigPost_address'];
                }
            }
            //			}
            if ($all_variations_set) {
                // Add to cart validation
                $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
                if ($passed_validation) {
                    if ($woocommerce->cart->add_to_cart($product_id, $quantity, $variation_id, $variations)) {
                        woocommerce_add_to_cart_message();
                        $added_to_cart = true;
                    }
                }
            } else {
                $woocommerce->add_error(__('Please choose product options&hellip;', 'woocommerce'));
                wp_redirect(apply_filters('woocommerce_add_to_cart_product_id', get_permalink($_REQUEST['product_id'])));
                exit;
            }
            break;
            // Grouped Products
        // Grouped Products
        case 'group':
            if (isset($_REQUEST['quantity']) && is_array($_REQUEST['quantity'])) {
                $quantity_set = false;
                foreach ($_REQUEST['quantity'] as $item => $quantity) {
                    if ($quantity < 1) {
                        continue;
                    }
                    $quantity_set = true;
                    // Add to cart validation
                    $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $item, $quantity);
                    if ($passed_validation) {
                        if ($woocommerce->cart->add_to_cart($item, $quantity)) {
                            woocommerce_add_to_cart_message();
                            $added_to_cart = true;
                        }
                    }
                }
                if (!$added_to_cart && !$quantity_set) {
                    $woocommerce->add_error(__('Please choose a quantity&hellip;', 'woocommerce'));
                    wp_redirect(apply_filters('woocommerce_add_to_cart_product_id', get_permalink($_REQUEST['product_id'])));
                    exit;
                }
            } elseif ($_REQUEST['product_id']) {
                /* Link on product archives */
                $woocommerce->add_error(__('Please choose a product&hellip;', 'woocommerce'));
                wp_redirect(get_permalink($_REQUEST['product_id']));
                exit;
            }
            break;
            // Simple Products - add-to-cart contains product ID
        // Simple Products - add-to-cart contains product ID
        default:
            // Only allow integers
            if (!is_numeric($_REQUEST['add-to-cart'])) {
                break;
            }
            // Get product ID to add and quantity
            $product_id = (int) $_REQUEST['add-to-cart'];
            $quantity = isset($_REQUEST['quantity']) ? (int) $_REQUEST['quantity'] : 1;
            $add2cart1 = "";
            if (isset($_REQUEST['add2cart1'])) {
                $add2cart1 = $_REQUEST['add2cart1'];
            }
            if ($add2cart1 != "yes") {
                if (isset($_REQUEST['pcode'])) {
                    $_SESSION['pcode'] = $_REQUEST['pcode'];
                }
                if (isset($_REQUEST['pickUp'])) {
                    $pickUp = $_REQUEST['pickUp'];
                }
                if (isset($_REQUEST['Delivery'])) {
                    $delivery1 = $_REQUEST['Delivery'];
                }
                if (isset($_REQUEST['bigpostdp'])) {
                    $bigpostdp = $_REQUEST['bigpostdp'];
                }
                if ($pickUp == "on") {
                    $_SESSION['pickUp'] = "on";
                    $_SESSION['Delivery'] = "off";
                    $_SESSION['BigPostDB'] = "off";
                    $ware1 = $_REQUEST['warehouse'];
                    if ($ware1 == "yes") {
                        $ship_pu = (int) $_REQUEST['freightprice'];
                        if (isset($_SESSION['pickup_ship']) == false) {
                            $_SESSION['pickup_ship'] = $ship_pu;
                            $_SESSION['pickup_shipname'] = get_the_title($product_id) . ": " . $_REQUEST['warehouse2'];
                        } else {
                            $_SESSION['pickup_ship'] = $_SESSION['pickup_ship'] . "~" . $ship_pu;
                            $_SESSION['pickup_shipname'] = $_SESSION['pickup_shipname'] . "~" . get_the_title($product_id) . ": " . $_REQUEST['warehouse2'];
                        }
                    }
                    if ($ware1 == "no") {
                        $ship_pu = (int) $_REQUEST['freightprice'];
                        if (isset($_SESSION['pickup_ship']) == false) {
                            $_SESSION['pickup_ship'] = $ship_pu;
                            $_SESSION['pickup_shipname'] = get_the_title($product_id) . ": Melbourne";
                        } else {
                            $_SESSION['pickup_ship'] = $_SESSION['pickup_ship'] . "~" . $ship_pu;
                            $_SESSION['pickup_shipname'] = $_SESSION['pickup_shipname'] . "~" . get_the_title($product_id) . ": Melbourne";
                        }
                    }
                }
                if ($delivery1 == "on") {
                    $_SESSION['pickUp'] = "off";
                    $_SESSION['Delivery'] = "on";
                    $_SESSION['BigPostDB'] = "off";
                    $ware1 = $_REQUEST['warehouse'];
                    $ship_delivery = (double) $_REQUEST['freightprice'];
                    if ($_REQUEST['hdsatl'] != "no") {
                        if (isset($_SESSION['hdsatl']) == false) {
                            $_SESSION['hdsatl'] = get_the_title($product_id) . "(#" . $_POST['pdsku'] . ")";
                        } else {
                            $_SESSION['hdsatl'] = $_SESSION['hdsatl'] . " , " . get_the_title($product_id) . "(#" . $_POST['pdsku'] . ")";
                        }
                    }
                    if ($ware1 == "yes") {
                        if (isset($_SESSION['delivery_warehouse']) == false) {
                            $_SESSION['ship_delivery'] = $ship_delivery;
                            $_SESSION['delivery_warehouse'] = get_the_title($product_id) . ": " . $_SESSION['pcode'];
                        } else {
                            $_SESSION['ship_delivery'] = $_SESSION['ship_delivery'] . "~" . $ship_delivery;
                            $_SESSION['delivery_warehouse'] = $_SESSION['delivery_warehouse'] . "~" . get_the_title($product_id) . ": " . $_SESSION['pcode'];
                        }
                    }
                    if ($ware1 == "no") {
                        if (isset($_SESSION['delivery_warehouse']) == false) {
                            $_SESSION['ship_delivery'] = $ship_delivery;
                            $_SESSION['delivery_warehouse'] = get_the_title($product_id) . ": Melbourne";
                        } else {
                            $_SESSION['ship_delivery'] = $_SESSION['ship_delivery'] . "~" . $ship_delivery;
                            $_SESSION['delivery_warehouse'] = $_SESSION['delivery_warehouse'] . "~" . get_the_title($product_id) . ": Melbourne";
                        }
                    }
                }
                if ($bigpostdp == "on") {
                    $_SESSION['pickUp'] = "off";
                    $_SESSION['Delivery'] = "off";
                    $_SESSION['BigPostDB'] = "on";
                    if ($_REQUEST['freightprice'] == "0") {
                        $ship_bigdp = 0;
                    } else {
                        $ship_bigdp = (double) $_REQUEST['freightprice'];
                    }
                    if (isset($_SESSION['bigpost_suburb']) == false) {
                        $bigpost_suburb = $_REQUEST['bigpost_suburb'];
                        $_SESSION['bigpost_suburb'] = $bigpost_suburb;
                    }
                    if (isset($_SESSION['ship_bigdp']) == false) {
                        $_SESSION['ship_bigdp'] = $ship_bigdp;
                        $_SESSION['BigPostDB_shipname'] = get_the_title($product_id) . " : " . $bigpost_suburb;
                        $_SESSION['BigPost_address'] = $_REQUEST['bigpost_add'];
                    } else {
                        $_SESSION['ship_bigdp'] = $_SESSION['ship_bigdp'] . "~" . $ship_bigdp;
                        $_SESSION['BigPostDB_shipname'] = $_SESSION['BigPostDB_shipname'] . "~" . get_the_title($product_id) . " : " . $_SESSION['bigpost_suburb'];
                        $_SESSION['BigPost_address'] = $_SESSION['BigPost_address'];
                    }
                }
            }
            if ($add2cart1 == "yes") {
                if ($_SESSION['pickUp'] == "on") {
                    $_SESSION['pickup_ship'] = $_SESSION['pickup_ship'] . "~" . "0";
                    $_SESSION['pickup_shipname'] = $_SESSION['pickup_shipname'] . "~" . get_the_title($product_id) . ": Melbourne";
                }
                if ($_SESSION['Delivery'] == "on") {
                    $_SESSION['delivery_warehouse'] = $_SESSION['delivery_warehouse'] . "~" . get_the_title($product_id) . ": " . $_SESSION['pcode'];
                }
                if ($_SESSION['BigPostDB'] == "on") {
                    $_SESSION['ship_bigdp'] = $_SESSION['ship_bigdp'] . "~" . "0";
                    $_SESSION['BigPostDB_shipname'] = $_SESSION['BigPostDB_shipname'] . "~" . get_the_title($product_id) . " : " . $_SESSION['bigpost_suburb'];
                }
            }
            //	echo "<script>alert('" . $_SESSION['pcode'] . "-----" . $_SESSION['Delivery'] . "');</script>";
            //		exit;
            // Add to cart validation
            $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity);
            if ($passed_validation) {
                // Add the product to the cart
                if ($woocommerce->cart->add_to_cart($_REQUEST['add-to-cart'], $quantity)) {
                    woocommerce_add_to_cart_message();
                    $added_to_cart = true;
                }
            }
            break;
    }
    // If we added the product to the cart we can now do a redirect, otherwise just continue loading the page to show errors
    if ($added_to_cart) {
        $url = apply_filters('add_to_cart_redirect', $url);
        // If has custom URL redirect there
        if ($url) {
            wp_safe_redirect($url);
            exit;
        } elseif (get_option('woocommerce_cart_redirect_after_add') == 'yes' && $woocommerce->error_count() == 0) {
            wp_safe_redirect($woocommerce->cart->get_cart_url());
            exit;
        }
    }
}
 /**
  * Purchase a job package
  * @param  int|string $package_id
  * @param  int $job_id
  * @return bool Did it work or not?
  */
 private static function process_package($package_id, $is_user_package, $job_id)
 {
     if ($is_user_package) {
         $user_package = wc_paid_listings_get_user_package($package_id);
         $package = wc_get_product($user_package->get_product_id());
         // Give job the package attributes
         update_post_meta($job_id, '_job_duration', $user_package->get_duration());
         update_post_meta($job_id, '_featured', $user_package->is_featured() ? 1 : 0);
         update_post_meta($job_id, '_package_id', $user_package->get_product_id());
         update_post_meta($job_id, '_user_package_id', $package_id);
         if ($package && 'listing' === $package->package_subscription_type) {
             update_post_meta($job_id, '_job_expires', '');
             // Never expire automatically
         }
         // Approve the job
         if (in_array(get_post_status($job_id), array('pending_payment', 'expired'))) {
             wc_paid_listings_approve_job_listing_with_package($job_id, get_current_user_id(), $package_id);
         }
         do_action('wcpl_process_package_for_job_listing', $package_id, $is_user_package, $job_id);
         return true;
     } elseif ($package_id) {
         $package = wc_get_product($package_id);
         // Give job the package attributes
         update_post_meta($job_id, '_job_duration', $package->get_duration());
         update_post_meta($job_id, '_featured', $package->is_featured() ? 1 : 0);
         update_post_meta($job_id, '_package_id', $package_id);
         if ('listing' === $package->package_subscription_type) {
             update_post_meta($job_id, '_job_expires', '');
             // Never expire automatically
         }
         // Add package to the cart
         WC()->cart->add_to_cart($package_id, 1, '', '', array('job_id' => $job_id));
         woocommerce_add_to_cart_message($package_id);
         // Clear cookie
         wc_setcookie('chosen_package_id', '', time() - HOUR_IN_SECONDS);
         wc_setcookie('chosen_package_is_user_package', '', time() - HOUR_IN_SECONDS);
         do_action('wcpl_process_package_for_job_listing', $package_id, $is_user_package, $job_id);
         // Redirect to checkout page
         wp_redirect(get_permalink(woocommerce_get_page_id('checkout')));
         exit;
     }
 }