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

问题描述 投票:0回答:1

我遇到了一个问题,几周来我一直在努力解决这个问题。尝试了我在互联网上可以找到的所有内容。

简而言之,它是一个订购助手系统(用于清洁),我使用 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 product-price
1个回答
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 文件中。现在应该可以工作了……

© www.soinside.com 2019 - 2024. All rights reserved.