WooCommerce中的自定义条件购物车商品价格

时间:2018-03-20 09:21:06

标签: php wordpress woocommerce cart price

我在这里遇到了一个问题,我现在一直试图解决这个问题。尝试了我能在互联网上找到的一切。

简而言之,它是一个订购助手(清洁)的系统,我使用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); 

        } 

    }

}

任何形式的帮助都将不胜感激!

1 个答案:

答案 0 :(得分:0)

对于购物车中的价格变化,您需要在之前进行价格计算(在添加到购物车事件后提交数据)并设置自定义价格计算购物车项目作为自定义数据,以避免出现问题和错误:

// Save custom field value in cart item
add_filter( 'woocommerce_add_cart_item_data', 'save_custom_field_in_cart_object', 30, 3 );
function save_custom_field_in_cart_object( $cart_item_data, $product_id, $variation_id ) {
    // Get the correct Id to be used (compatible with product variations)
    $the_id = $variation_id > 0 ? $variation_id : $product_id;

    if( isset( $_POST['servicetime'] ) ){
        $product = wc_get_product( $the_id ); // Get the WC_Product object
        $product_price = (float) $product->get_price(); // Get the product price
        $service_time = (float) sanitize_text_field( $_POST['servicetime'] ); // Get service time
        // Set the price calculation as custom data in cart item
        $cart_item_data['custom_price'] = $product_price * $service_time;
    }
    return $cart_item_data;
}

// Change price of items in the cart
add_action( 'woocommerce_before_calculate_totals', 'add_custom_item_price', 1 );
function add_custom_item_price( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    foreach ( $cart->get_cart() as $cart_item ) {
        // ID 650 is the cleaning equipment id
        if( $cart_item['data']->get_id() != 650 && isset( $cart_item['custom_price'] ) ) { 
            // Get the calculated custom price
            $new_price = (float) $cart_item['custom_price'];
            // set the new item price in cart
            $cart_item['data']->set_price( $new_price );
        }
    }
}

代码放在活动子主题(或主题)的function.php文件中。它现在应该工作......