WC Fields Factory:在购物车功能中使用产品页面中的条目

时间:2019-03-24 14:32:37

标签: php wordpress woocommerce product cart

在Woocommerce中,我使用WC Fields Factory插件在将产品信息添加到购物车之前捕获产品页面中的信息。我需要访问输入的数据才能在我的子主题函数文件中执行计算并在购物车中显示正确的信息,但是还无法找到如何做的方法。但是,我可以确认在产品页面上输入的数据已正确添加到订单确认电子邮件/订单详细信息屏幕中。

基于Custom cart item weight in Woocommerce答案代码,这是我完整的修改后的代码版本:

quotesRef.orderByChild('index').once('value', function(snapshot) {
    snapshot.forEach(function(childSnapShot) {
        vm.allQuotes.push({
            key: childSnapShot.key,
            quoteTxt: childSnapShot.val().quote
        })
    })
})

我只需要在下面的“默认产品重量和尺寸”部分中使用用户提交的详细信息,而不使用存储的产品详细信息,其余功能粘贴在上下文中:

add_filter( 'woocommerce_add_cart_item_data', 'add_weight_custom_cart_item_data', 10, 3 );
function add_weight_custom_cart_item_data( $cart_item_data, $product_id, $variation_id ){
    // For product variations handling
    $product_id = $variation_id > 0 ? $variation_id : $product_id;

    // Get an instance of the product object
    $product = wc_get_product( $product_id );

    // The default product weight and dimensions
    $cart_item_data['weight']['default'] = $product->get_weight() ?: '0'; // Change this to use weight entered on product page (new field)
    $cart_item_data['length']['default'] = $product->get_length() ?: '0'; // Change this to use length entered on product page (new field)
    $cart_item_data['width']['default'] = $product->get_width() ?: '0';   // Change this to use width entered on product page (new field)
    $cart_item_data['height']['default'] = $product->get_height() ?: '0'; // Change this to use height entered on product page (new field)
    //___________________ Also add another field 'carton_qty' from the page to use in the below calculations instead of 2

    ## ====> HERE YOU CAN MAKE YOUR WEIGHT CALCULATIONS <==== ##
    $new_weight_value = (($cart_item_data['length']['default'] * $cart_item_data['width']['default'] * $cart_item_data['height']['default']) / 5000);
    $old_total_weight = ($cart_item_data['weight']['default'] * 2);
    $new_total_weight = ($new_weight_value * 2);

    // Set the new calculated weights
    $cart_item_data['weight']['new'] = $new_weight_value;
    $cart_item_data['weight']['totalold'] = $old_total_weight;
    $cart_item_data['weight']['totalnew'] = $new_total_weight;

    return $cart_item_data;
}

add_filter( 'woocommerce_get_item_data', 'display_cart_item_weight', 10, 2 );
function display_cart_item_weight( $item_data, $cart_item ) {
    if ( isset($cart_item['weight']) ) {
        // Display original weight
        if ( isset($cart_item['weight']['default']) ) {
            $item_data[] = array(
                'key'       => __('Carton Weight (std)', 'woocommerce'),
                'value'     => wc_format_weight( $cart_item['weight']['default'] ),
            );
        }

        // Display calculated weight
        if ( isset($cart_item['weight']['new']) ) {
            $item_data[] = array(
                'key'     => __( 'Carton Weight (vol)', 'woocommerce' ),
                'value'   => wc_format_weight( $cart_item['weight']['new'] ),
            );      
        }

        // Display calculated original total weight
        if ( isset($cart_item['weight']['totalold']) ) {
            $item_data[] = array(
                'key'     => __( 'Line Weight (std)', 'woocommerce' ),
                'value'   => wc_format_weight( $cart_item['weight']['totalold'] ),
            );      
        }
        // Display calculated total weight

        if ( isset($cart_item['weight']['totalnew']) ) {
            $item_data[] = array(
                'key'     => __( 'Line Weight (vol)', 'woocommerce' ),
                'value'   => wc_format_weight( $cart_item['weight']['totalnew'] ),
            );
            if ($cart_item['weight']['totalold'] < $cart_item['weight']['totalnew']){
                echo "<span style='color:#515151;font-size:1.6em;'><b>&nbsp;&#9951;</b></span>";
            }               
        }
    }
    return $item_data;
}

// Set the new weight in cart items
add_action( 'woocommerce_before_calculate_totals', 'set_custom_cart_item_weight', 25, 1 );
function set_custom_cart_item_weight( $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['weight']['new']) ) {
            $cart_item['data']->set_weight(max ($cart_item['weight']['totalold'], $cart_item['weight']['totalnew']));
        }
    }
}

一个示例,重量在WC Field Factory的前端显示为字段名称“ carton_weight”。我已经尝试了各种方法,包括获取订单元数据。我在插件文件中找到了一些代码,这些代码指示我应该使用// The default product weight and dimensions $cart_item_data['weight']['default'] = $product->get_weight() ?: '0'; // Change this to use weight entered on product page (new field) $cart_item_data['length']['default'] = $product->get_length() ?: '0'; // Change this to use length entered on product page (new field) $cart_item_data['width']['default'] = $product->get_width() ?: '0'; // Change this to use width entered on product page (new field) $cart_item_data['height']['default'] = $product->get_height() ?: '0'; // Change this to use height entered on product page (new field) //___________________ Also add another field 'carton_qty' from the page to use in the below calculations instead of 2 等,但是我尝试过的任何方法都没有用。

我正在使用WC Fields Factory版本3.0.3(当前版本)。

0 个答案:

没有答案