我只有可变产品具有自定义基本价格。现在我只想更改单个产品页面、购物车页面和结账页面中的每个变体价格,而不会对每个原始价格产生任何影响。 假设我有 1 种产品,该产品有一种变体,其变体价格为 10 美元,基本价格为 200
所以,在 frontedn 中,我想显示当选择用户时,此变化价格应为 2000 美元,但在管理惩罚中,变化价格应为 10 美元。
我正在尝试这个代码
// Function to calculate custom price based on variation
function custom_variation_price_html( $price_html, $product ) {
// Check if it's a variable product
if ( $product->is_type( 'variable' ) ) {
// Get all available variations
$variations = $product->get_available_variations();
// Get the base price
$base_price = get_post_meta( $product->get_id(), '_base_price', true );
// Check if base price is set and greater than 0
if ( isset( $base_price ) && $base_price > 0 ) {
foreach ( $variations as $variation ) {
// Get the variation object
$variation_obj = wc_get_product( $variation['variation_id'] );
// Get the regular price of the variation
$regular_price = $variation_obj->get_price();
// Calculate the custom price
$custom_price = floatval( $regular_price ) * $base_price;
// Format the custom price
$formatted_price = wc_price( $custom_price );
// Replace the variation price in the price HTML
$price_html = str_replace( wc_price( $regular_price ), $formatted_price, $price_html );
}
}
}
// Return the modified price HTML
return $price_html;
}
add_filter( 'woocommerce_get_price_html', 'custom_variation_price_html', 10, 2 );
这是我在上面的代码中得到的结果:
当我更改变化时,我希望价格覆盖。
你没有使用正确的方法和所需的钩子......
上下文:如果我理解得很清楚,您的可变产品中有一个自定义字段
_base_price
,如下所示(仅用于测试):
// Add a custom field to variable product in backend
add_action( 'woocommerce_product_options_pricing', 'add_custom_variable_product_option_pricing' );
function add_custom_variable_product_option_pricing() {
echo '</div>
<div class="options_group pricing show_if_variable" >';
woocommerce_wp_text_input( array(
'id' => '_base_price',
'label' => __('Base price rate', 'woocommerce'),
'data_type' => 'price,'
));
}
// Save variable product custom submitted value in Backend
add_action( 'woocommerce_admin_process_product_object', 'save_custom_variable_product_option_pricing', 30, 1 );
function save_custom_variable_product_option_pricing( $product ){
if ( ! $product->is_type('variable') ) return;
$base_price = isset($_POST['_base_price']) ? wc_clean( wp_unslash($_POST['_base_price']) ) : null;
$product->update_meta_data('_base_price', $base_price );
}
基于 通过 WooCommerce 3+ 中的挂钩更改产品价格答案代码,您将使用以下代码替换:
// Change product variation prices
add_filter('woocommerce_product_variation_get_regular_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_price', 99, 2 );
function custom_price( $price, $variation ) {
$product = wc_get_product($variation->get_parent_id());
$base_price = (float) $product->get_meta('_base_price');
if ( $price > 0 && $base_price > 0 ) {
return $price * $product->get_meta('_base_price');
}
}
// Change variable product price range
add_filter('woocommerce_variation_prices_price', 'custom_variable_price', 99, 3 );
add_filter('woocommerce_variation_prices_regular_price', 'custom_variable_price', 99, 3 );
function custom_variable_price( $price, $variation, $product ) {
$base_price = (float) $product->get_meta('_base_price');
if ( $price > 0 && $base_price > 0 ) {
return $price * $product->get_meta('_base_price');
}
}
// Handling price caching
add_filter( 'woocommerce_get_variation_prices_hash', 'add_price_multiplier_to_variation_prices_hash', 99, 3 );
function add_price_multiplier_to_variation_prices_hash( $price_hash, $product, $for_display ) {
$base_price = (float) $product->get_meta('_base_price');
if ( $base_price > 0 ) {
$price_hash[] = $base_price;
}
return $price_hash;
}
代码位于子主题的functions.php 文件中(或插件中)。已测试并有效。