wordpress - 如何在wordpress woocommerce上双重计算产品?
问题描述
我需要一次重复计算产品。例如,当我打开一个产品时,它需要显示 0 个选择的项目。当我单击加号(+)时,它需要一次添加 2 个。我怎样才能在 wordpress woocommerce 上做到这一点?
解决方案
美好的一天,帖木儿。
据我了解,您想将产品两次添加到购物车并显示数字“2”?
你可以使用这个 WC 钩子:
add_action('woocommerce_add_to_cart', 'custome_add_to_cart');
function custome_add_to_cart() {
global $woocommerce;
$product_id = $_POST['assessories'];
$found = false;
//check if product already in cart
if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $_product->id == $product_id )
$found = true;
}
// if product not found, add it
if ( ! $found )
WC()->cart->add_to_cart( $product_id );
} else {
// if no products in cart, add it
WC()->cart->add_to_cart( $product_id );
}
}
当它返回结果时,用 JS 更新订购产品的数量。
请告诉它是否可以帮助您?
推荐阅读
- c# - 当我发送 4 位整数时出现“System.Numeric.Vectors”错误
- reactjs - ReactJS,React-Router:调用父函数
- mongodb - Storm-kafka-mongoDB 集成
- python - 如何从python中的图像(文档表单)中裁剪空白部分?
- javascript - 获取动态创建的表格行的 HTML
- android-studio - 在 android studio 3.3.2 中创建新项目时出错
- arrays - 如何循环数组,以便一个填满,而另一个清空到串行监视器中?
- java - 如何使用 Selenium 获取元素的部分文本
- c# - 将非特定数量的数据集行添加到列表中
- java - java错误:找不到符号GPSException