计算和更新购物车中的产品价格 - WooCommerce
Calculate and update product price in cart - WooCommerce
我正在创建一个 WooCommerce(版本 5.6.0)网站,用户可以在其中购买泡沫产品,价格在存档页面上计算。基本上,用户输入尺寸,然后根据公式计算价格。我目前正在努力更新购物车中的价格,我以前没有在这个级别定制产品和价格的经验,所以任何帮助将不胜感激。到目前为止我完成的步骤:
1.Get(通过Ajax)并在WC_Session
中设置自定义产品价格
function get_custom_product_price_set_to_session(){
// Check that there is a 'foamPrice' and foamProductI _POST variable
if (isset($_POST['foamProductId']) && isset($_POST['foamPrice'])) {
// Enable customer WC_Session (needed on first add to cart)
if (!WC()->session->has_session()) {
WC()->session->set_customer_session_cookie(true);
}
// Set the product_id and the custom price in WC_Session variable
WC()->session->set('foam_price', [
'id' => (int)wc_clean($_POST['foamProductId']),
'price' => (float)wc_clean($_POST['foamPrice']),
]);
}}
add_action('wp_ajax_get_custom_product_price_set_to_session', 'get_custom_product_price_set_to_session');
add_action('wp_ajax_nopriv_get_custom_product_price_set_to_session', 'get_custom_product_price_set_to_session');
2.Change 购物车商品价格来自 WC_Session 数据:
add_action('woocommerce_before_calculate_totals', 'custom_cart_item_price', 20, 1);
function custom_cart_item_price($cart){
if (is_admin() && !defined('DOING_AJAX'))
return;
// Must be required since Woocommerce version 3.2 for cart items properties changes
if (did_action('woocommerce_before_calculate_totals') >= 2)
return;
// Loop through our specific cart item keys
foreach ($cart->get_cart() as $cart_item) {
// Get custom product price for the current item
if (($data = WC()->session->get('foam_price')) && $cart_item['data']->get_id() == $data['id']) {
// Set the new product price
$cart_item['data']->set_price($data['price']);
}
}}
我 运行 遇到的问题是,它仅适用于添加到购物车的最新产品,每次我添加新产品时,之前添加到购物车的产品都将重新设置为 0 价格产品到购物车。如果有人能够提供一些指导,我将不胜感激。谢谢磨坊!
有
WC()->session->set('foam_price', [
'id' => (int)wc_clean($_POST['foamProductId']),
'price' => (float)wc_clean($_POST['foamPrice']),
]);
你基本上是用最新的添加到购物车来覆盖之前的“foam_price”。
在我看来,您需要为不同的产品 ID 存储不同的值。
尝试(未测试):
function get_custom_product_price_set_to_session(){
// Check that there is a 'foamPrice' and foamProductI _POST variable
if (isset($_POST['foamProductId']) && isset($_POST['foamPrice'])) {
// Enable customer WC_Session (needed on first add to cart)
if (!WC()->session->has_session()) {
WC()->session->set_customer_session_cookie(true);
}
// Set the product_id and the custom price in WC_Session variable
WC()->session->set('foam_price' . wc_clean($_POST['foamProductId']), [
'id' => (int)wc_clean($_POST['foamProductId']),
'price' => (float)wc_clean($_POST['foamPrice']),
]);
}}
add_action('wp_ajax_get_custom_product_price_set_to_session', 'get_custom_product_price_set_to_session');
add_action('wp_ajax_nopriv_get_custom_product_price_set_to_session', 'get_custom_product_price_set_to_session');
...然后:
add_action('woocommerce_before_calculate_totals', 'custom_cart_item_price', 20, 1);
function custom_cart_item_price($cart){
if (is_admin() && !defined('DOING_AJAX'))
return;
// Must be required since Woocommerce version 3.2 for cart items properties changes
if (did_action('woocommerce_before_calculate_totals') >= 2)
return;
// Loop through our specific cart item keys
foreach ($cart->get_cart() as $cart_item) {
// Get custom product price for the current item
if (($data = WC()->session->get('foam_price' . $cart_item['data']->get_id())) && $cart_item['data']->get_id() == $data['id']) {
// Set the new product price
$cart_item['data']->set_price($data['price']);
}
}}
我正在创建一个 WooCommerce(版本 5.6.0)网站,用户可以在其中购买泡沫产品,价格在存档页面上计算。基本上,用户输入尺寸,然后根据公式计算价格。我目前正在努力更新购物车中的价格,我以前没有在这个级别定制产品和价格的经验,所以任何帮助将不胜感激。到目前为止我完成的步骤:
1.Get(通过Ajax)并在WC_Session
中设置自定义产品价格function get_custom_product_price_set_to_session(){
// Check that there is a 'foamPrice' and foamProductI _POST variable
if (isset($_POST['foamProductId']) && isset($_POST['foamPrice'])) {
// Enable customer WC_Session (needed on first add to cart)
if (!WC()->session->has_session()) {
WC()->session->set_customer_session_cookie(true);
}
// Set the product_id and the custom price in WC_Session variable
WC()->session->set('foam_price', [
'id' => (int)wc_clean($_POST['foamProductId']),
'price' => (float)wc_clean($_POST['foamPrice']),
]);
}}
add_action('wp_ajax_get_custom_product_price_set_to_session', 'get_custom_product_price_set_to_session');
add_action('wp_ajax_nopriv_get_custom_product_price_set_to_session', 'get_custom_product_price_set_to_session');
2.Change 购物车商品价格来自 WC_Session 数据:
add_action('woocommerce_before_calculate_totals', 'custom_cart_item_price', 20, 1);
function custom_cart_item_price($cart){
if (is_admin() && !defined('DOING_AJAX'))
return;
// Must be required since Woocommerce version 3.2 for cart items properties changes
if (did_action('woocommerce_before_calculate_totals') >= 2)
return;
// Loop through our specific cart item keys
foreach ($cart->get_cart() as $cart_item) {
// Get custom product price for the current item
if (($data = WC()->session->get('foam_price')) && $cart_item['data']->get_id() == $data['id']) {
// Set the new product price
$cart_item['data']->set_price($data['price']);
}
}}
我 运行 遇到的问题是,它仅适用于添加到购物车的最新产品,每次我添加新产品时,之前添加到购物车的产品都将重新设置为 0 价格产品到购物车。如果有人能够提供一些指导,我将不胜感激。谢谢磨坊!
有
WC()->session->set('foam_price', [
'id' => (int)wc_clean($_POST['foamProductId']),
'price' => (float)wc_clean($_POST['foamPrice']),
]);
你基本上是用最新的添加到购物车来覆盖之前的“foam_price”。
在我看来,您需要为不同的产品 ID 存储不同的值。
尝试(未测试):
function get_custom_product_price_set_to_session(){
// Check that there is a 'foamPrice' and foamProductI _POST variable
if (isset($_POST['foamProductId']) && isset($_POST['foamPrice'])) {
// Enable customer WC_Session (needed on first add to cart)
if (!WC()->session->has_session()) {
WC()->session->set_customer_session_cookie(true);
}
// Set the product_id and the custom price in WC_Session variable
WC()->session->set('foam_price' . wc_clean($_POST['foamProductId']), [
'id' => (int)wc_clean($_POST['foamProductId']),
'price' => (float)wc_clean($_POST['foamPrice']),
]);
}}
add_action('wp_ajax_get_custom_product_price_set_to_session', 'get_custom_product_price_set_to_session');
add_action('wp_ajax_nopriv_get_custom_product_price_set_to_session', 'get_custom_product_price_set_to_session');
...然后:
add_action('woocommerce_before_calculate_totals', 'custom_cart_item_price', 20, 1);
function custom_cart_item_price($cart){
if (is_admin() && !defined('DOING_AJAX'))
return;
// Must be required since Woocommerce version 3.2 for cart items properties changes
if (did_action('woocommerce_before_calculate_totals') >= 2)
return;
// Loop through our specific cart item keys
foreach ($cart->get_cart() as $cart_item) {
// Get custom product price for the current item
if (($data = WC()->session->get('foam_price' . $cart_item['data']->get_id())) && $cart_item['data']->get_id() == $data['id']) {
// Set the new product price
$cart_item['data']->set_price($data['price']);
}
}}