在Woocommerce中添加带有自定义数据的产品的单独购物车商品

时间:2017-12-12 08:11:18

标签: php wordpress woocommerce cart custom-fields

嗨,目前我在前端的每个产品上都有自定义表单。在这里,客户可以根据此价格更改自定义宽度和高度

为此我写了下面的代码,它正在工作

add_filter( 'woocommerce_add_cart_item', 'add_custom_cart_item_data', 10, 2 );
function add_custom_cart_item_data( $cart_item_data, $cart_item_key ) {

    if( isset( $_POST['new-width'] ) )
        $cart_item_data['new-width'] = $_POST['new-width'];
    if(isset( $_POST['new-height'] ) )
       $cart_item_data['new-height'] = $_POST['new-height'];

    return $cart_item_data;
}


add_action( 'woocommerce_before_calculate_totals', 'set_custom_cart_item_price', 10, 1 );
function set_custom_cart_item_price( $wc_cart ) {

    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    foreach ( $wc_cart->get_cart() as $cart_item ){
        if( ! empty($cart_item['new-width']) && ! empty($cart_item['new-height']) ){
            $new_value=$cart_item['new-width']*$cart_item['new-height'];
            $cart_item['data']->set_price($new_value);
            }
    } 
}

现在我面临的问题是

客户将产品1添加到购物车width=30 and height=30.,因此总价为30*30=90 [基于$cart_item['new-width']*$cart_item['new-height']] 和客户使用width=40 and height=40添加相同的product1。根据我的公式,价格将变为40*40=160。结帐时的总价格应为160+90=250 但我得到的是90 + 90 = 180

在结帐页面我需要的是

(1)product-1(width=30, height=30)  90
(2)product-1(width=40, height=40)  160
total 250

但目前显示

(1)product-1(width=30, height=30)  90  qty(2)

total 180

请帮忙

1 个答案:

答案 0 :(得分:1)

已更新 (2018年10月)

首先,您应该更好地使用 woocommerce_add_cart_item_data 挂钩,这样可以获得不同的项目:

add_filter( 'woocommerce_add_cart_item_data', 'save_custom_fields_data_to_cart', 10, 2 );
function save_custom_fields_data_to_cart( $cart_item_data, $product_id ) {
    $data = array();

    // Set the custom data in the cart item
    if( isset( $_POST['new-height'] ) && ! empty( $_POST['new-height'] ) 
    && isset( $_POST['new-height'] ) && ! empty( $_POST['new-height'] ) )
    {
        // Save the new calculated price as cart item custom data
        $cart_item_data[custom_data]['price']  = $_POST['new-width'] * $_POST['new-height'];

        // Save 'new-width' and 'new-height' as cart item custom data (if needed)
        $cart_item_data[custom_data]['width']  = $_POST['new-width'];
        $cart_item_data[custom_data]['height'] = $_POST['new-height'];

        // Every add to cart action is set as a unique line item
        $cart_item_data[custom_data]['unique_key'] = md5( microtime().rand() );
    }
    return $cart_item_data;
}

然后稍作改动:

add_action( 'woocommerce_before_calculate_totals', 'set_custom_cart_item_price', 10, 1 );
function set_custom_cart_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 ){
        if( isset( $cart_item['custom_data']['price'] ) && ! empty( $cart_item['custom_data']['price'] ) ){
            $cart_item['data']->set_price( $cart_item['custom_data']['price'] );
        }
    }
}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

测试并运作

注意:30 x 30不是90,但 900 40 x 40不是160,但它是 1600