自定义 Woocommerce 可变产品价格范围,用于多个活动变体 [英] Custom Woocommerce variable product price range for more than one active variation

查看:56
本文介绍了自定义 Woocommerce 可变产品价格范围,用于多个活动变体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用我在 Internet 上找到的这个脚本来删除价格范围并仅显示 WooCommerce/WordPress 中可变产品的最低价格,语法如下:从美元 xxxx>

我希望脚本做同样的事情,但当可变产品只有一个变体时例外.有一个自动 cron(bash + SQL 脚本)可以删除不可用的产品.有时它会留下一个只有一个变体的可变产品,并且列出这个变体并标明从 xxx 美元起"的价格看起来很可笑,因为只有一个变体).

您将如何添加一个条件,将这个 From US$ xxx 仅应用于具有多个变体的可变产品.我的主要目标是在目录/类别/商店页面上使用它,因为已经有一个片段可以从可变的单个产品页面中删除价格范围.谢谢.

add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format_310', 10, 2 );函数 bbloomer_variation_price_format_310( $price, $product ) {//1. 找到最低正常价格和销售价格$min_var_reg_price = $product->get_variation_regular_price( 'min', true );$min_var_sale_price = $product->get_variation_sale_price( 'min', true );//2. 新的 $price如果( $min_var_sale_price ){$price = sprintf( __( 'From %1$s', 'woocommerce' ), wc_price( $min_var_reg_price ) );}//3. 返回修改后的 $price返回 $price;}//显示具有相同变化价格的可变产品的价格add_filter('woocommerce_available_variation', function ($value, $object = null, $variation = null) {if ($value['price_html'] == '') {$value['price_html'] = ''.$variation->get_price_html() .'</span>';}返回 $value;}, 10, 3);

解决方案

在您的第一个函数中计算可见子级将允许您实现这一目标.我还重新审视了您应该命名的第二个函数:

add_filter( 'woocommerce_variable_price_html', 'custom_variation_price_html', 20, 2 );函数 custom_variation_price_html( $price_html, $product ) {$visible_children = $product->get_visible_children();if( count($visible_children) <= 1 ) 返回 $price_html;//如果只有一种变化则退出$regular_price_min = $product->get_variation_regular_price( 'min', true );$sale_price_min = $product->get_variation_sale_price( 'min', true );如果( $sale_price_min )$price_html = __( 'From', 'woocommerce' ).''.wc_price( $regular_price_min );返回 $price_html;}add_filter('woocommerce_available_variation', 'custom_available_variation', 20, 3);函数 custom_available_variation( $args, $product, $variation ) {if( $args['price_html'] == '' )$args['price_html'] = ''.$variation->get_price_html() .'</span>';返回 $args;}

此代码位于活动子主题(或主题)的 function.php 文件中.

经过测试并有效.

I'm using this script, which I found on the Internet, to remove price range and display only minimum price for variable products in WooCommerce/WordPress in following syntax: From US$ xxxx

I'd like the script to do this same thing with exception when variable product has only one variation. There is an automated cron (bash + SQL script) which removes products that are unavailable. Sometimes it leaves a variable product with only one variation, and listing this variation with a price stating "From US$ xxx" looks rediculous, as there is only one single variation).

How would you add a condition that applies this From US$ xxx to only variable products that have more than one variation. My primary goal is to use it on catalog/category/shop pages, as there is a already a snippet that removes price range from variable single product page. Thanks.

add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format_310', 10, 2 );
function bbloomer_variation_price_format_310( $price, $product ) {

// 1. Find the minimum regular and sale prices

$min_var_reg_price = $product->get_variation_regular_price( 'min', true );
$min_var_sale_price = $product->get_variation_sale_price( 'min', true );

// 2. New $price

if ( $min_var_sale_price ) {
$price = sprintf( __( 'From %1$s', 'woocommerce' ), wc_price( $min_var_reg_price ) );
}

// 3. Return edited $price

return $price;
}

// Display Price For Variable Product With Same Variations Prices
add_filter('woocommerce_available_variation', function ($value, $object = null, $variation = null) {
    if ($value['price_html'] == '') {
        $value['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>';
    }
    return $value;
}, 10, 3);

解决方案

Counting visible children in your 1st function will allow you to achieve that. Also I have revisited your 2nd function which should be named:

add_filter( 'woocommerce_variable_price_html', 'custom_variation_price_html', 20, 2 );
function custom_variation_price_html( $price_html, $product ) {
    $visible_children = $product->get_visible_children();
    if( count($visible_children) <= 1 ) return $price_html; // Exit if only one variation

    $regular_price_min = $product->get_variation_regular_price( 'min', true );
    $sale_price_min = $product->get_variation_sale_price( 'min', true );

    if ( $sale_price_min ) 
        $price_html = __( 'From', 'woocommerce' ).' '.wc_price( $regular_price_min );

    return $price_html;
}

add_filter('woocommerce_available_variation', 'custom_available_variation', 20, 3 ) ;
function custom_available_variation( $args, $product, $variation ) {
    if( $args['price_html'] == '' )
        $args['price_html'] = '<span class="price">' . $variation->get_price_html() . '</span>';
    
    return $args;
}

This code goes on function.php file of your active child theme (or theme).

Tested and works.

这篇关于自定义 Woocommerce 可变产品价格范围,用于多个活动变体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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