php - WooCommerce 价格覆盖不起作用

标签 php wordpress woocommerce cart price

我使用 woocommerce_before_add_to_cart_button hook

设置了隐藏的输入项
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 );

现在我可以回显此 woocommerce_before_calculate_totals Hook 中的 $_POST['added_price']

我编写的代码如下所示:

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 );

我在这里做错了什么?

最佳答案

这是基于您的代码问题的完整、干净、经过测试且有效的解决方案。

这里我添加了您的自定义字段 key/value在相关购物车项目的购物车对象中,而不是在 calculate_gift_wrap_fee() 中获取 Post 值 功能。

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

我已评论'gift_wrap_fee''gift_wrap_price'因为现在并不真正需要它们(但如果您愿意,可以取消注释它们)。

这是这段代码:

// 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.x 到 3+ 的 WooCommerce 版本。

关于php - WooCommerce 价格覆盖不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45331393/

相关文章:

php - 基于用户角色的 Woo Commerce 登录重定向

javascript - 如何使用ajax从php获取多个数据并将其存储在选择选项中

php - 将 WooCommerce 订单保存到远程数据库

php - 如何在 Woocommerce 中的属性上方的一行中添加自定义文本?

wordpress - Woocommerce:当类别仅包含一项时如何直接跳转到产品页面

php - 使用 WordPress 的下拉列表

php - 如果 div 里面的 div 得到了类,则为按钮添加特定的样式

php不被识别为使用xampp的内部或外部命令

php - 使用 Laravel5.4 自定义 .txt 日志文件

php - 如何获取json数组并插入数据库。 php