WooCommerce:按产品ID计算销售量(短代码)

仿牌独立站出海专家
PHP代码段:在任何地方显示产品ID的销售总额(短代码)
只需使用[sales id =“123”],其中“ 123”是您的产品ID,即可显示产品销售数量


add_shortcode( 'sales', 'bbloomer_sales_by_product_id' );
   
function bbloomer_sales_by_product_id( $atts ) {
        
   $atts = shortcode_atts( array(
      'id' => ''
   ), $atts );
    
   $units_sold = get_post_meta( $atts['id'], 'total_sales', true );
     
   return $units_sold;