使用get_price()函数的WooCommerce自定义价格 [英] WooCommerce custom price using get_price() function

查看:503
本文介绍了使用get_price()函数的WooCommerce自定义价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前一直在根据用户位置使用多种价格/货币.我正在经历整个订购过程,而且我快要完成了.

I've currently been playing around with using multiple prices/currencies based on user location. I'm going through the whole ordering process and I'm almost there.

我正在使用get_price()函数与woocommerce_get_price挂钩(位于class-wc-product.php,第822行),然后从产品中找到我已设置的自定义字段数量(gb_price,us_price等).

I'm using the get_price() function to hook with woocommerce_get_price (located in class-wc-product.php, line 822) and then find custom field amounts that i have set (gb_price, us_price etc) from the product.

在商店,单个产品视图,购物车,结帐中,所有功能都可以正常工作,但是在下订单时,所有功能都将恢复为默认的基本成本和币种.我注意到,这仅在通过functions.php挂接时才会失败.如果我直接在类文件中修改函数本身,那么一切都将完美运行.

All works fine in the shop, single product view, cart, checkout but when placing order it all falls back to the default base cost and currency. I've noticed that this only fails when hooking in via functions.php. If I amend the function itself directly in the class file, everything works perfectly.

我真的不想破坏WC的核心,所以有人可以看看并告诉我为什么它失败了吗?这是我的代码...

I really don't want to hack away at the core of WC so can someone have a look and tell me why it fails? Here's my code...

class-wc-product.php

function get_price() {  
    return apply_filters( 'woocommerce_get_price', $this->price, $this );
}

functions.php

add_filter('woocommerce_get_price', 'return_custom_price', $product, 2); 
function return_custom_price($price, $product) {    
    global $post, $woocommerce;
    // Grab the product id
    $post_id = $product->id; 
    // Get user's ip location and correspond it to the custom field key
    $user_country = $_SESSION['user_location'];
    $get_user_currency = strtolower($user_country.'_price');
    // If the IP detection is enabled look for the correct price
    if($get_user_currency!=''){
        $new_price = get_post_meta($post_id, $get_user_currency, true);
        if($new_price==''){
            $new_price = $price;
        }
    }
    return $new_price;
}   

因此,该命令适用于除订单确认之外的所有位置.如果我只是简单地将函数从functions.php移到get_price()中的类本身,则它可以完美地工作.

So this works everywhere except the order confirmation. If I simply move the function from functions.php to the class itself within get_price(), it works perfectly.

推荐答案

这是我现在使用的代码,虽然它不是完美的方法,但应该可以帮助人们走上正确的道路. (尚未使用WC2.0进行过测试,我几乎可以肯定它不会对2兼容.)请注意,所有使用的会话都是IP驱动的测试,并不是该功能不可或缺的.我从前改编它用作插件.在WP产品中,我使用自定义字段来表示价格,即"us_price","gb_price"等,这就是ip检测的方式.

Here is the code I now use, it's not perfect by any means but should help people get on the right track. (Not tested with WC2.0 yet, I'm almost certain it will not work out of box for 2.) Please note, any sessions used are for IP driven testing and are not integral to the functionality. I adapted it from before to be used as a plugin. In products in WP I use custom fields for the price i.e 'us_price', 'gb_price' etc, this is how the ip detection hooks in.

// 1. Change the amount
function return_custom_price($price, $product) {
    global $post, $woocommerce;
    $post_id = $post->ID;
    // Prevent conflicts with order pages and products, peace of mind.
    if($post_id == '9' || $post_id == '10' || $post_id == '17' || $post_id == '53' || $post_id == ''){
        // cart, checkout, , order received, order now
        $post_id = $product->id;
    }
    $user_country = $_SESSION['user_location'];
    $get_user_currency = strtolower($user_country.'_price');
    // If the IP detection is enabled look for the correct price
    if($get_user_currency!=''){
        $new_price = get_post_meta($post_id, $get_user_currency, true);
        if($new_price==''){
            $new_price = $price;
        }
    }
    if( is_admin() && $_GET['post_type']=='product' ){
        return $price;
    } else {
        return $new_price;
    }
}
add_filter('woocommerce_get_price', 'return_custom_price', $product, 2);

// 2. Update the order meta with currency value and the method used to capture it
function update_meta_data_with_new_currency( $order_id ) {
    if($_SESSION['user_order_quantity']>=2){
        update_post_meta( $order_id, 'group_ticket_amount', $_SESSION['user_order_quantity'] );
        update_post_meta( $order_id, 'master_of', '' );

    }
    update_post_meta( $order_id, 'currency_used', $_SESSION['user_currency'] );
    update_post_meta( $order_id, 'currency_method', $_SESSION['currency_method'] );
}
add_action( 'woocommerce_checkout_update_order_meta', 'update_meta_data_with_new_currency' );

这篇关于使用get_price()函数的WooCommerce自定义价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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