WooCommerce显示自定义变体价格为范围

huangapple go评论74阅读模式
英文:

WooCommerce display custom variation prices as range

问题

The "filter_variation_prices_price" function you provided seems to call the "filter_product_variation_price" function to adjust the price for a variation. However, the issue might be related to the logic of how it's applied to the price range.

Without the full context of your WooCommerce setup, it's a bit challenging to pinpoint the exact issue, but here are some suggestions to consider:

  1. Ensure that the "woocommerce_variation_prices_price" filter is being applied correctly. Double-check that it's hooked in the right place and with the correct priority.

  2. Verify that the data for the customer's zipcode is being retrieved correctly within the "filter_variation_prices_price" function. You can add debugging statements or log messages to check this.

  3. Make sure that the logic within the "filter_variation_prices_price" function correctly applies the zipcode-based pricing to the variation. You may need to adjust the logic to ensure it considers all variations and their zipcodes.

  4. Check for any conflicts with other WooCommerce plugins or themes that might affect price display.

If you continue to face issues, you might want to seek assistance from the WooCommerce community or a developer familiar with WooCommerce customization, as debugging and fixing such issues may require a more detailed examination of your specific setup.

英文:

I have a couple of products with some variations. On the homepage, archive page and single product page I am showing the price range of the variations.

I want to change the prices of my variations based on the customer's zipcode.
In the variation itself I have a custom field. In this field I can set the price for different zipcodes, for example:
1256PO:120,9856IE:150 -> zipcode:price

I already have the code to check the zipcode and update the price for the variation itself on the productpage and checkoutpage. This works.

add_filter('woocommerce_product_variation_get_price', 'filter_product_variation_price', 10, 2);

function filter_product_variation_price($price, $variation) {
    if (WC()->session) {
        $customer_zipcode = WC()->session->get('customer')['postcode'];
        if (!$customer_zipcode) {
            return $price;

        }
        $zipcode_prices = $variation->get_meta('_zipcode_prices');
        if (!$zipcode_prices) {
            return $price;
        }
        $zipcode_prices = explode(',', $zipcode_prices);
        foreach ($zipcode_prices as $zipcode_price) {
            list($zipcode, $zipcode_price) = explode(':', $zipcode_price);
            if ($zipcode == $customer_zipcode) {
                return $zipcode_price;
            }
        }
    }
    return $price;
}

This code does not update the displayed price range for the parent product. I tried this:

add_filter('woocommerce_variation_prices_price', 'filter_variation_prices_price', 10, 2);

function filter_variation_prices_price($price, $variation) {
    return filter_product_variation_price($price, $variation);
}

This does update the price range but it just checks the lowest and highest price. It does not check the zipcode for some reason.

What I need is to change all prices displayed based on the customers zipcode.
Is there anyone who can tell me whats wrong with the filter_variation_prices_price function.

答案1

得分: 1

所有可变产品的价格范围都在WooCommerce中进行缓存以提高性能显示,因此您需要使用woocommerce_get_variation_prices_hash过滤器挂钩,这将允许以更高效的方式刷新缓存的变化价格,而不必每次执行此挂钩时删除相关的瞬态数据。

以下是您修订后的代码:

// 获取客户邮政编码的自定义函数
function get_customer_zipcode(){
    if ( WC()->session ) {
        $customer_data = WC()->session->get('customer');
        if ( isset($customer_data['postcode']) ) {
            return $customer_data['postcode'];
        }
    }
    return false;
}

add_filter('woocommerce_product_variation_get_price', 'filter_product_variation_price', 99, 2 ); // 产品变体价格
add_filter('woocommerce_variation_prices_price', 'filter_product_variation_price', 99, 2 ); // 变量产品显示的价格范围
function filter_product_variation_price( $price, $variation ) {
    $zipcode = get_customer_zipcode();
    
    if ( $zipcode ) {
        $zipcode_prices = $variation->get_meta('_zipcode_prices');
        
        if ( ! $zipcode_prices ) {
            return $price;
        }
        
        $zipcode_prices = explode(',', $zipcode_prices);
        
        foreach ($zipcode_prices as $zipcode_price) {
            list($zipcode, $zipcode_price) = explode(':', $zipcode_price);
            if ($zipcode == $customer_zipcode) {
                return $zipcode_price;
            }
        }
    }
    return $price;
}

// 处理价格缓存(请参阅末尾的说明)
add_filter( 'woocommerce_get_variation_prices_hash', 'filter_get_variation_price_hash', 99, 3 );
function filter_get_variation_price_hash( $price_hash, $product, $for_display ) {
    $zipcode = get_customer_zipcode();
    
    if ( $zipcode ) {
        $zipcode_prices = $variation->get_meta('_zipcode_prices');
        
        if ( ! $zipcode_prices ) {
            return $price_hash;
        }
    }
    $price_hash[] = $zipcode;
    
    return $price_hash;
}

此代码应放置在活动子主题(或活动主题)的functions.php文件中。应该有效。

参考:缓存和动态定价 - 对get_variation_prices方法的即将更改

相关:https://stackoverflow.com/questions/45806249/change-product-prices-via-a-hook-in-woocommerce-3/45807054#45807054

英文:

All Variable Product price range are cached in WooCommerce for better performance display, so you need to use woocommerce_get_variation_prices_hash filter hook, that will allow to refresh variations cached prices in a much more efficient way, without deleting related transients anytime that this hooks are executed.

Here is your revisited code:

// Custom function that get the customer zipcode
function get_customer_zipcode(){
	if ( WC()->session ) {
		$customer_data = WC()->session->get('customer');
        if ( isset($customer_data['postcode']) ) {
            return $customer_data['postcode'];
        }
    }
    return false;
}

add_filter('woocommerce_product_variation_get_price', 'filter_product_variation_price', 99, 2 ); // Product Variation price
add_filter('woocommerce_variation_prices_price', 'filter_product_variation_price', 99, 2 ); // Variable product displayed price range
function filter_product_variation_price( $price, $variation ) {
	$zipcode = get_customer_zipcode();
	
    if ( $zipcode ) {
        $zipcode_prices = $variation->get_meta('_zipcode_prices');
        
        if ( ! $zipcode_prices ) {
            return $price;
        }
        
        $zipcode_prices = explode(',', $zipcode_prices);
        
        foreach ($zipcode_prices as $zipcode_price) {
            list($zipcode, $zipcode_price) = explode(':', $zipcode_price);
            if ($zipcode == $customer_zipcode) {
                return $zipcode_price;
            }
        }
    }
    return $price;
}

// Handling price caching (see explanations at the end)
add_filter( 'woocommerce_get_variation_prices_hash', 'filter_get_variation_price_hash', 99, 3 );
function filter_get_variation_price_hash( $price_hash, $product, $for_display ) {
    $zipcode = get_customer_zipcode();
	
    if ( $zipcode ) {
        $zipcode_prices = $variation->get_meta('_zipcode_prices');
        
        if ( ! $zipcode_prices ) {
            return $price_hash;
        }
	}
    $price_hash[] = $zipcode;
    
    return $price_hash;
}

Code goes in functions.php file of the active child theme (or active theme). It should work.

See: Caching and dynamic pricing – upcoming changes to the get_variation_prices method

Related: https://stackoverflow.com/questions/45806249/change-product-prices-via-a-hook-in-woocommerce-3/45807054#45807054

huangapple
  • 本文由 发表于 2023年5月25日 21:23:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76332798.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定