wordpress 添加自定义产品(油漆颜色)到Woocommerce购物车通过Ajax

3okqufwl  于 11个月前  发布在  WordPress
关注(0)|答案(1)|浏览(134)

我正在建立一个油漆商店,客户可以选择RAL颜色,并选择以升为单位的数量。

根据颜色配方计算价格。当前添加产品时,价格和产品描述会正确显示。

然而,当我尝试选择另一种颜色时,它不会将产品添加到购物车中。下面是迄今为止我能做的最好的自定义代码:

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;
}

字符串
更新:只有第一次添加产品时才会调用钩子woocommerce_add_cart_item。在任何连续调用中,钩子都不会执行...
更新2:我使用的插件自定义油漆变化的WooCommerce

ajsxfq5m

ajsxfq5m1#

我修复它的方法是在现有代码上方添加此代码,以便再次调用钩子“woocommerce_add_cart_item”:

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;
}

字符串

相关问题