WooCommerce价格覆盖无效 [英] WooCommerce Price overriding not working

查看:71
本文介绍了WooCommerce价格覆盖无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 woocommerce_before_add_to_cart_button 钩子

function add_gift_wrap_field() {
    ?>`<input type="hidden" id="price_val" name="added_price" value="100.34">`<?php
}
add_action( 'woocommerce_before_add_to_cart_button', 'add_gift_wrap_field' );

针对产品保存字段:

function save_gift_wrap_fee( $cart_item_data, $product_id ) {

    if( isset( $_POST['added_price'] ) ) {

        $cart_item_data = array();

        $cart_item_data[ "gift_wrap_fee" ] = "YES"; 
        $cart_item_data[ "gift_wrap_price" ] = 100;     
    }
    return $cart_item_data;

}
add_filter( 'woocommerce_add_cart_item_data', 'save_gift_wrap_fee', 99, 2 );

现在,我可以回显 $ _ POST ['added_price'] woocommerce_before_calculate_totals 挂钩中的code>。

Now I can echo out the $_POST['added_price'] inside this woocommerce_before_calculate_totals hook.

我编写的代码如下所示:

The code I written is shown below:

function calculate_gift_wrap_fee( $cart_object ) {
    if( !WC()->session->__isset( "reload_checkout" )) {
        /* Gift wrap price */
        $additionalPrice = number_format($_POST['added_price'], 2); 

        $additionalPrice = floatval($additionalPrice);
        //echo $additionalPrice; exit(); shows the value
        foreach ( $cart_object->cart_contents as $key => $value ) {
            if( isset( $value["gift_wrap_fee"] ) ) {
                    /* Woocommerce 3.0 + */
                    $orgPrice = floatval( $value['data']->get_price() );
                    $value['data']->set_price( $orgPrice + $additionalPrice );    //not adding the $additionalPrice here.     
            }
        }
    }
}
add_action( 'woocommerce_before_calculate_totals', 'calculate_gift_wrap_fee', 99 );

我在这里做什么错了?

推荐答案

这是基于您的代码问题的完整,经过测试和有效的完整解决方案。

Here is the complete clean, tested and working solution based on your code question.

这里我包括了您的自定义字段 / 在相关购物车项目的购物车对象中,而不是在 calculate_gift_wrap_fee() 函数中获取Post值。

Here I have included your custom field key/value in the cart object for the related cart item, instead of getting the Post value in your calculate_gift_wrap_fee() function.

这样就不可能丢失自定义字段值,并且新的价格计算是可靠的。

This way it's not possible to loose the custom field value and the new price calculation is reliable.

I已评论'gift_wrap_fee''gift_wrap_price',因为它们现在确实不需要了(但是您可以取消注释)

I have commented 'gift_wrap_fee' and 'gift_wrap_price' as they are not really needed now (but you can uncomment them if you like).

这里是这段代码:

// The hidden product custom field
add_action( 'woocommerce_before_add_to_cart_button', 'add_gift_wrap_field' );
function add_gift_wrap_field() {
    ?>
        <input type="hidden" id="price_val" name="added_price" value="100.34">
    <?php
}

// Adding the custom field to as custom data for this cart item in the cart object
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 ) {
    $bool = false;
    $data = array();
    if( ! empty( $_REQUEST['added_price'] ) ) {
        $cart_item_data['custom_data']['added_price'] = $_REQUEST['added_price'];
        // Below this 2 values are not really needed (I think)
        // (You can uncomment them if needed)

        ## $cart_item_data['custom_data']['gift_wrap_fee'] = 'YES';
        ## $cart_item_data['custom_data']['gift_wrap_price'] = 100;

        // below statement make sure every add to cart action as unique line item
        $cart_item_data['custom_data']['unique_key'] = md5( microtime().rand() );
        WC()->session->set( 'custom_data', $data );
    }
    return $cart_item_data;
}

// Changing the cart item price based on custom field calculation
add_action( 'woocommerce_before_calculate_totals', 'calculate_gift_wrap_fee', 10, 1 );
function calculate_gift_wrap_fee( $cart ) {

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

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

    // Iterating though cart items
    foreach ( $cart->get_cart() as $cart_item ) {
        // Continue if we get the custom data for the current cart item
        if( ! empty( $cart_item['custom_data'] ) ){
            // Get the custom field "added price" value
            $added_price = number_format( $cart_item['custom_data']['added_price'], 2 );
            // The WC_Product object
            $product = $cart_item['data'];
            // Get the price (WooCommerce versions 2.5.x to 3+)
            $product_price = method_exists( $product, 'get_price' ) ? floatval(product->get_price()) : floatval($product->price);
            // New price calculation
            $new_price = $product_price + $added_price;
            // Set the calculeted price (WooCommerce versions 2.5.x to 3+)
            method_exists( $product, 'set_price' ) ? $product->set_price( $new_price ) : $product->price = $new_price;
        }
    }
}

代码已输入您的活动子主题(或主题)或任何插件文件中的function.php文件。

此代码经过测试,可在2.5版的WooCommerce版本上使用.x到3 +。

This code is tested and works on WooCommerce versions from 2.5.x to 3+.

这篇关于WooCommerce价格覆盖无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆