Ecommerce Shopify WordPress Discussion

Add Custom Products (Paint Colors) To Woocommerce Cart Via Ajax

I'm building a paint store where customers can select a RAL color and choose quantity in liters. The price is calculated based on the color recipe. Currently when a product is added, the price and product description are showed correctly. However when I try to pick up another color, it does not add the product in the cart. Here is the best custom code I could make so far: add_filter('woocommerce_add_cart_item' , 'set_woo_prices'); add_filter('woocommerce_get_cart_item_from_session', 'set_session_prices', 10 , 3); function set_woo_prices($woo_data) { if (empty($_POST['kforwselectcolorprice'])) { return $woo_data; } $woo_data['my_prod_name'] = $_POST['kforwselectcolorname']; foreach ($_POST['kforwoo_variation_quantity'] as $prodVarName => $prodVarQty) { if (empty($prodVarQty)) { continue; } $woo_data['my_prod_name'] .= sprintf("<br/> %s x %d", $prodVarName, $prodVarQty); } $woo_data['my_price'] = $_POST['kforwselectcolorprice']; $woo_data['data']->set_price($_POST['kforwselectcolorprice']); $woo_data['data']->set_name($woo_data['my_prod_name']); return $woo_data; } function set_session_prices($woo_data, $values, $key) { if (empty($woo_data['my_price']) || empty($woo_data['my_prod_name'])) { return $woo_data; } $woo_data['data']->set_price($woo_data['my_price']); $woo_data['data']->set_name($woo_data['my_prod_name']); return $woo_data; } Update: Only the first time a product is added the hook woocommerce_add_cart_item is called. On any consecutive calls, the hook is not executed... Update2: I'm using the plugin Custom Paint Variation for WooCommerce
They way I fixed it was adding this code above the existing one, so that the hook "woocommerce_add_cart_item" is called again: add_filter('woocommerce_add_cart_item_data', 'set_woo_prices_data', 10, 3); function set_woo_prices_data($cart_item_data, $product_id) { if (empty($_POST['kforwselectcolorprice'])) { return $cart_item_data; } $cart_item_data['kforwoo_product_id'] = $product_id; $cart_item_data['kforwselectcolor'] = $_POST['kforwselectcolor']; $cart_item_data['kforwcolorprice'] = $_POST['kforwselectcolorprice']; $cart_item_data['kforwoo_variation_quantity'] = $_POST['kforwoo_variation_quantity']; return $cart_item_data; }

January 7, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.