Woocommerce 产品的平均售价

Woocommerce 产品的平均售价,woocommerce,Woocommerce,我试图为单个产品页面创建一个短代码,显示当前产品的平均销售价格。因此,短代码将出现在产品页面的描述中。 此值应适用于简单产品和可变产品。 我能得到简单产品的平均值,但只有当订单中有一个项目时,我不能用可变产品解决这个问题 函数平均销售价格(){ 全球$产品; 如果(是($product,'WC\U product')){ $product_id=$product->get_id(); $orders=wc\u get\u订单(数组( “numberposts”=>-1, “post_type”

我试图为单个产品页面创建一个短代码,显示当前产品的平均销售价格。因此,短代码将出现在产品页面的描述中。 此值应适用于简单产品和可变产品。 我能得到简单产品的平均值,但只有当订单中有一个项目时,我不能用可变产品解决这个问题


函数平均销售价格(){
全球$产品;
如果(是($product,'WC\U product')){
$product_id=$product->get_id();
$orders=wc\u get\u订单(数组(
“numberposts”=>-1,
“post_type”=>“shop_order”,
) );
foreach($orders作为$order){
如果(计数($order->get_items())>0){
foreach($order->get\u items()作为$item\u id=>$item)
$productid=$item->get_variation_id()?$item->get_variation_id():$item->get_product_id();
如果($productid==$product\U id){
订单中的$product_price_+=$item->get_total();
}
}
}
$count_订单=0;
foreach($orders作为$order){
$has_product=false;
foreach($order->get\u items()作为$item\u值)
如果($item_values['product_id']==$product_id)
$has_product=true;
如果($has\u产品)
$count_orders++;
}
$avg_sales_price=round($product_price_in_orders/$count_orders);
回声'
平均售价
“.esc_html(获取商业货币符号().$avg_销售价格)。”
';
}
}
你知道怎么做吗?
谢谢。

要获得基于当前产品的订单,您不必每次都查询所有订单。它占用了太多的资源,尤其是在订单很多的情况下

在应答代码中,您可以使用函数
get\u orders\u ids\u by\u product\u id()
从特定的产品id获取所有订单(您必须在其中设置所需的订单状态)

然后,您可以使用另一个函数根据产品id计算每个产品(简单或变化)的平均价格

// calculate the average price based on the product id using the query within the "get_orders_ids_by_product_id()" function
function calculates_average_price_based_on_product_id( $product_id ) {
    $orders = get_orders_ids_by_product_id( $product_id );
    $count = count( $orders );
    if ( $count <= 0 ) {
        return; // set the return value in case there are no orders for this product
    }
    $prices = 0;
    foreach ( $orders as $order_id ) {
        $order = wc_get_order( $order_id );
        if ( $order ) {
            foreach ( $order->get_items() as $item ) {
                $product = $item->get_product();
                if ( $product_id == $product->get_id() ) {
                    $qty = $item->get_quantity();
                    $total = $item->get_total();
                    $prices += $total / $qty;
                }
            }
        }
    }
    return $prices / $count;
}
钩子 作为快捷码的替代方案,您可以使用
woocommerce\u-before\u-add\u-to\u-cart\u表单
hook在产品页面上添加平均价格,并使用以下功能:

// shows the average price on the product page
add_action( 'woocommerce_before_add_to_cart_form', 'shows_average_price_on_the_product_page', 10 );
function shows_average_price_on_the_product_page() {
    global $product;
    // initialize prices
    $avg_price = 0;
    $current_avg_price = 0;
    // initializes the number of variations that have been purchased at least once
    $count = 0;
    // if the product is variable it calculates the average price of all the variations
    if ( $product->is_type( 'variable' ) ) {
        $variation_ids = $product->get_children();
        // gets the total number of variations
        $total_variations = count( $variation_ids );
        foreach ( $variation_ids as $variation_id ) {
            $variation = wc_get_product( $variation_id );
            // gets the average price of each single variation
            $avg_price_variation = calculates_average_price_based_on_product_id( $variation_id );
            // if the average price of the current product variation is greater than zero, it updates the price and count
            if ( $avg_price_variation > 0 ) {
                $avg_price += $avg_price_variation;
                $count++; 
            }
            // sum the current price of each single variation (in case no variation has ever been purchased)
            $current_avg_price += $variation->get_price();
        }
        // if no variation has been purchased, I display the average price between the current net prices
        if ( $avg_price == 0 ) {
            $avg_price = $current_avg_price / $total_variations;
        } else {
            $avg_price /= $count;
        }
    // if the product is simple
    } else {
        // gets the average price of the product
        $avg_price = calculates_average_price_based_on_product_id( $product->get_id() );
        // if the simple product has never been ordered, I will display the current price
        if ( $avg_price == 0 ) {
            $avg_price = $product->get_price();
        }
    }
    // creates the HTML content to display on the product page
    $html = '<div class="sales-price-number"><span class="sales-price-text">Average Sale Price </span><span class="sales-price-value">' . wc_price( $avg_price ) . ' </span></div>';
    echo $html;
}
//在产品页面上显示平均价格
添加操作(“添加到购物车表单之前的woocommerce”,在产品页面上显示平均价格”,10);
函数在产品页面()上显示平均价格{
全球$产品;
//初始化价格
$avg_价格=0;
$当前平均价格=0;
//初始化已购买至少一次的变体数量
$count=0;
//如果产品是可变的,它会计算所有变化的平均价格
如果($product->is_类型('variable')){
$variation_id=$product->get_children();
//获取变体的总数
$total_variations=计数($variations_id);
foreach($variation\u id作为$variation\u id){
$variation=wc\u get\u产品($variation\u id);
//获取每个变体的平均价格
$avg_price_variation=根据产品id($variation_id)计算平均价格;
//如果当前产品变化的平均价格大于零,则更新价格和计数
如果($avg_price_variation>0){
$avg_price+=$avg_price_variation;
$count++;
}
//各单项变更的当前价格总和(如果未购买任何变更)
$current_avg_price+=$variation->get_price();
}
//如果未购买任何变动,我将显示当前净价之间的平均价格
如果($avg_price==0){
$avg_price=$current_avg_price/$total_variations;
}否则{
$avg_price/=$count;
}
//如果产品很简单
}否则{
//获取产品的平均价格
$avg_price=根据产品id($product->get_id())计算平均价格;
//如果简单产品从未订购过,我将显示当前价格
如果($avg_price==0){
$avg_price=$product->get_price();
}
}
//创建要显示在产品页面上的HTML内容
$html='平均销售价格'.wc_价格($avg_价格)。'';
echo$html;
}
该代码已经过测试,可以正常工作。将其添加到活动主题的functions.php中

建议

尽管上面的代码工作正常,但这并不是获得产品平均价格的最佳方法

每当每个订单的状态更改(例如,更改为“wc completed”)时,您可以为每个产品创建和更新自定义元

您只需要该产品的订单数量计数器,以及根据上次完成的订单计算的平均价格


当新订单变为“已完成”状态时,您将更新相应字段。

问题在于可变产品实际上没有价格。该价格仅针对单个变体设定。在这种情况下,是否要显示每个变体的所有平均价格?@Vdgaetano不是每个变体的平均价格,而是所有变体之间的平均价格。e、 我有三种变体:红色的10美元,绿色的11美元,蓝色的12美元。如果所有这些变体都是以相同的数量购买相同的次数,那么平均价格将为~$11。所以不是每个变量都是平均值,不是每个变量,而是所有变量之间的平均值。e、 我有三种变体:红色的10美元,绿色的11美元,蓝色的12美元。如果所有这些变体都是以相同的数量购买相同的次数,那么平均价格将为~$11。因此,不是针对每个变体,而是ave
// shows the average price on the product page
add_action( 'woocommerce_before_add_to_cart_form', 'shows_average_price_on_the_product_page', 10 );
function shows_average_price_on_the_product_page() {
    global $product;
    // initialize prices
    $avg_price = 0;
    $current_avg_price = 0;
    // initializes the number of variations that have been purchased at least once
    $count = 0;
    // if the product is variable it calculates the average price of all the variations
    if ( $product->is_type( 'variable' ) ) {
        $variation_ids = $product->get_children();
        // gets the total number of variations
        $total_variations = count( $variation_ids );
        foreach ( $variation_ids as $variation_id ) {
            $variation = wc_get_product( $variation_id );
            // gets the average price of each single variation
            $avg_price_variation = calculates_average_price_based_on_product_id( $variation_id );
            // if the average price of the current product variation is greater than zero, it updates the price and count
            if ( $avg_price_variation > 0 ) {
                $avg_price += $avg_price_variation;
                $count++; 
            }
            // sum the current price of each single variation (in case no variation has ever been purchased)
            $current_avg_price += $variation->get_price();
        }
        // if no variation has been purchased, I display the average price between the current net prices
        if ( $avg_price == 0 ) {
            $avg_price = $current_avg_price / $total_variations;
        } else {
            $avg_price /= $count;
        }
    // if the product is simple
    } else {
        // gets the average price of the product
        $avg_price = calculates_average_price_based_on_product_id( $product->get_id() );
        // if the simple product has never been ordered, I will display the current price
        if ( $avg_price == 0 ) {
            $avg_price = $product->get_price();
        }
    }
    // creates the HTML content to display on the product page
    $html = '<div class="sales-price-number"><span class="sales-price-text">Average Sale Price </span><span class="sales-price-value">' . wc_price( $avg_price ) . ' </span></div>';
    echo $html;
}