Example #1
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;
}
Example #2
0
function removeCartToFly()
{
    global $woocommerce;
    $woocommerce->cart->set_quantity($_POST['remove_item'], 0);
    if ($woocommerce->cart) {
        $items_in_cart = $woocommerce->cart->cart_contents_count;
        $prod_ids_in_cart = array();
        foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
            $product_id = apply_filters('woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key);
            array_push($prod_ids_in_cart, $product_id);
        }
        $test01 = 0;
        $test02 = 0;
        $test03 = 0;
        $test04 = 0;
        if (in_array("566", $prod_ids_in_cart)) {
            $test01 = 1;
        }
        if (in_array("568", $prod_ids_in_cart)) {
            $test02 = 1;
        }
        if (in_array("570", $prod_ids_in_cart)) {
            $test03 = 1;
        }
        if (in_array("572", $prod_ids_in_cart)) {
            $test04 = 1;
        }
        $how_many_tests_in_cart = $test01 + $test02 + $test03 + $test04;
        if (in_array("574", $prod_ids_in_cart)) {
            foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
                if ($cart_item['product_id'] == 574) {
                    $woocommerce->cart->set_quantity($cart_item_key, 0);
                }
            }
            if ($how_many_tests_in_cart == 0) {
            }
            if ($how_many_tests_in_cart == 1) {
                $woocommerce->cart->add_to_cart('574', '1', '719', '1');
            }
            if ($how_many_tests_in_cart == 2) {
                $woocommerce->cart->add_to_cart('574', '1', '720', '2');
            }
            if ($how_many_tests_in_cart == 3) {
                $woocommerce->cart->add_to_cart('574', '1', '721', '3');
            }
            if ($how_many_tests_in_cart == 4) {
                $woocommerce->cart->add_to_cart('574', '1', '722', '4');
            }
        }
        // end if there is processing in cart
    }
    // edn of if woocommerce cart
    $ver = explode(".", WC_VERSION);
    if ($ver[0] >= 2 && $ver[1] >= 0 && $ver[2] >= 0) {
        $wc_ajax = new WC_AJAX();
        $wc_ajax->get_refreshed_fragments();
    } else {
        woocommerce_get_refreshed_fragments();
    }
    die;
}