【问题标题】:WooCommerce - Ordering related products by sales custom calculationWooCommerce - 通过销售自定义计算订购相关产品
【发布时间】:2016-09-11 12:23:37
【问题描述】:

Woocommerce 相关产品模板位于:

/wp-content/plugins/woocommerce/templates/single-product/related.php

但它目前按随机排序。

我想通过 'total_sales' + 'sales_count' 订购(这些是 2 个元键保存整数值,'sales_count' 是一个额外的自定义场地)。

这里是查询:

 $args = apply_filters( 'woocommerce_related_products_args', array(
    'post_type'            => 'product',
    'ignore_sticky_posts'  => 1,
    'no_found_rows'        => 1,
    'posts_per_page'       => 4,
    'orderby'   => 'total_sales',
     'order' => 'DESC',
    'post__in'             => $related
) );
    $products = new WP_Query( $args );

上述查询排序产品购买 total_sales 但我需要使用 total_salessales_count 的总和对其进行排序?

有没有办法做到这一点?

谢谢

【问题讨论】:

  • 对不起,我添加了更多信息,它只需要按两个自定义字段的总和排序,其他参数正常
  • 当客户下订单时,wocommerce 的total_sales 值增量也有一个自定义字段,我添加的sales_count 自定义字段也包含一些整数,sales_count 的用法是我们将其用于显示在产品上页 sales_count+total_sales 。因此产品之前没有任何实际销售额,该产品使用 sales_count 字段显示一些销售额
  • $units_sold = get_post_meta($product->id, 'total_sales', true ); $soldty= get_post_meta($product->id, 'sales_count', true ); echo '销售额:'.($units_sold+$soldty);
  • 所以我们需要订购相关产品$units_sold+$soldty,我们目前使用total_sales进行排序
  • 不只是使用 WordPress 自定义字段

标签: php wordpress woocommerce custom-fields product


【解决方案1】:

我不太了解 $soldty = get_post_meta( $product->id, 'sales_count', true ); 的用途,因为产品的默认 'total_sales' 已经计算了所有已售商品(每次售出的商品数量都会添加到计数中)。

无论如何,如果您真的需要此计算,最好的选择是创建一个函数,为每个产品创建/更新一个新的自定义字段,并使用该计算:

add_action( 'wp_loaded', 'products_update_total_sales_calculation' );
function products_update_total_sales_calculation(){
    $sales = array();
    $all_products = get_posts( array(
        'post_type'            => 'product',
        'post_status'          => 'publish',
        'posts_per_page'       => -1,
    ) );
    foreach($all_products as $item) {
        $units_sold = get_post_meta( $item->ID, 'total_sales', true );
        $soldty = get_post_meta( $item->ID, 'sales_count', true );
        if(empty($soldty)) $soldty = 0;
        $result = $units_sold + $soldty;
        update_post_meta( $item->ID, 'global_sales_count', $result );
    }
}

此函数计算产品销售额并创建/更新自定义字段'global_sales_count',其中包含计算值。

现在您可以根据这个新的自定义字段自定义查询 'orderby' 参数:

$args = apply_filters( 'woocommerce_related_products_args', array(
    'post_type'            => 'product',
    'ignore_sticky_posts'  => 1,
    'no_found_rows'        => 1,
    'posts_per_page'       => 4,
    'meta_key'             => 'global_sales_count', // <== <== the new custom field
    'orderby'              => 'meta_value_num',
    'order'                => 'DESC',
    'post__in'             => $related

) );
    $products = new WP_Query( $args );

如果您不需要计算,'meta_key' 值将更改为现有 'total_sales' 产品 meta_key,这样:

$args = apply_filters( 'woocommerce_related_products_args', array(
    'post_type'            => 'product',
    'ignore_sticky_posts'  => 1,
    'no_found_rows'        => 1,
    'posts_per_page'       => 4,
    'meta_key'             => 'total_sales', // <== <== the existing meta_key 
    'orderby'              => 'meta_value_num',
    'order'                => 'DESC',
    'post__in'             => $related

) );
    $products = new WP_Query( $args );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    相关资源
    最近更新 更多