【发布时间】:2018-08-29 00:56:12
【问题描述】:
我在这里遇到了一个问题,我已经尝试解决好几个星期了。尝试了我在互联网上可以找到的所有内容。
简而言之,它是一个订购助手系统(用于清洁),我使用 woocommerce + woocommerce 预订 + woocommerce 供应商。 基本上我需要手动将清洁设备添加到购物车中,然后我需要将购物车物品(助手)价格乘以服务时间(清洁设备除外)。但只要购物车里有清洁设备,其他物品(帮手)的价格就会乘以 2 倍的服务时间。
我不明白出了什么问题,当清洁设备不在购物车中时,一切都很好。购物车中有多少其他物品(助手)并不重要,它总是将服务时间乘以 2。
// Add equipment to the cart
add_action( 'woocommerce_before_calculate_totals', 'add_equipment_to_cart', 10 );
function add_equipment_to_cart() {
$product_id = 650; // id=650 - cleaning equipment id
// check whether radio 'Do you have equipment?' is checked to 'Yes' or 'No'
if($_POST['equipment'] == 'No') {
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
// check whether equipment is already in the cart
if( $values['data']->id == $product_id ) {
$equipment = $product_id;
}
}
// add it to cart only if it is not there
if(empty($equipment)){
WC()->cart->add_to_cart( $product_id );
}
} elseif($_POST['equipment'] == 'Yes'){
foreach( WC()->cart->get_cart() as $cart_item_key2 => $values2 ) {
$_product2 = $values2['data'];
if($values2['data']->id == $product_id ){
WC()->cart->remove_cart_item($cart_item_key2);
}
}
}
}
// Change price of items in the cart
add_action( 'woocommerce_before_calculate_totals', 'add_custom_item_price', 1 );
function add_custom_item_price( $cart_object ) {
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$item_id = $cart_item['data']->get_id();
if($item_id != 650) { // 650 - cleaning equipment id
// product original price
$original_price = $cart_item['data']->get_price();
// comes from an input
$servicetime = $_POST['servicetime'];
$new_price = $original_price * $servicetime;
// set the new item price in cart
$cart_item['data']->set_price($new_price);
}
}
}
任何形式的帮助都将不胜感激!
【问题讨论】:
标签: php wordpress woocommerce cart price