Home Forums Basel support forum Ajax add to cart a custom product

Ajax add to cart a custom product

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #151687

    sobingt
    Participant

    I am creating a custom product page for a custom product.

    https://wp.imfpa.tk/pay/

    the form is calling a post URL that updates the cart but how do I sidebar and shopping cart widget icon.

    #151698

    sobingt
    Participant

    <a href="?add-to-cart=24649" data-quantity="1" class="button product_type_simple add_to_cart_button ajax_add_to_cart basel-tooltip added" data-product_id="24649" data-product_sku="SO1" aria-label="Add “Ankle Socks (Set of 3)” to your cart" rel="nofollow"><span class="basel-tooltip-label">Add to cart</span>Add to cart</a>

    I know i can using this snippet to add normal product. How do i add custom price to it

    
    if ($_POST["amount"]) {
        global $woocommerce;
        $custom_price = $_POST["amount"];
        // Cart item data to send & save in order
        $cart_item_data = array('custom_price' => $custom_price);
        update_post_meta(153844, '_regular_price', $custom_price);
        update_post_meta(153844, '_price', $custom_price);
        $product_cart_id = $woocommerce - > cart - > generate_cart_id(153844);
        $in_cart = $woocommerce - > cart - > find_product_in_cart($product_cart_id);
    
        if ($in_cart) {
    
            $woocommerce - > cart - > remove_cart_item($product_cart_id);
            // Calculate totals
            $woocommerce - > cart - > calculate_totals();
            // Save cart to session
            $woocommerce - > cart - > set_session();
            // Maybe set cart cookies
            //$woocommerce->cart->maybe_set_cart_cookies();
    
        }
        // woocommerce function to add product into cart check its documentation also
        // what we need here is only $product_id & $cart_item_data other can be default.
        $woocommerce - > cart - > add_to_cart($product_id = 153844, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data);
        // Calculate totals
        $woocommerce - > cart - > calculate_totals();
        // Save cart to session
        $woocommerce - > cart - > set_session();
        // Maybe set cart cookies
        //$woocommerce->cart->maybe_set_cart_cookies();
        //sleep(2);
        //wp_redirect('cart');
        exit;
        //echo '<script> jQuery(document).ready(function(){ addToCartAjax(); }); </script>';
    }                                            
    
    #151700

    Artem Temos
    Keymaster

    Hi,

    Sorry, but there is no such option in our theme and WooCommerce by default. It may require additional code customization or extra plugin but we don’t have an instruction for this. It is out of our theme support scope.

    Regards

    #151708

    sobingt
    Participant

    How does the theme update the cart sidebar in an ajax way?

    #151709

    sobingt
    Participant

    there is some function in the theme in js/function.js line 1098 addToCartAllTypes which get fired but I am not sure when it is fired.

    #151771

    Artem Temos
    Keymaster

    This function is called only when the page is fully loaded to init add to cart with AJAX on the product page. You can review its code to see how AJAX works in general.

Viewing 6 posts - 1 through 6 (of 6 total)