Home › Forums › Basel support forum › Ajax add to cart a custom product
Ajax add to cart a custom product
- This topic has 5 replies, 2 voices, and was last updated 5 years, 1 month ago by Artem Temos.
-
AuthorPosts
-
October 22, 2019 at 1:58 pm #151687
sobingtParticipantI am creating a custom product page for a custom product.
the form is calling a post URL that updates the cart but how do I sidebar and shopping cart widget icon.
October 22, 2019 at 2:23 pm #151698
sobingtParticipant<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>'; }
October 22, 2019 at 2:33 pm #151700
Artem TemosKeymasterHi,
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
October 22, 2019 at 3:17 pm #151708
sobingtParticipantHow does the theme update the cart sidebar in an ajax way?
October 22, 2019 at 3:23 pm #151709
sobingtParticipantthere 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.
October 23, 2019 at 6:09 am #151771
Artem TemosKeymasterThis 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.
-
AuthorPosts
Tagged: add to cart, AJAX, custom product
- You must be logged in to create new topics. Login / Register