get_wc_query_args( 'publish', -1, array( 'simple', 'variable', 'subscription', 'variable-subscription' ), 'ids' );
$product_ids = wc_get_products( $args );
foreach ( $product_ids as $product_id ) {
$prod_var = new WC_Product_Variable( $product_id );
$variations = $prod_var->get_available_variations( 'objects' );
if ( ! empty( $variations ) ) {
foreach ( $variations as $variation ) {
$product_lists[] = $variation->get_id();
}
} else {
$product_lists[] = $product_id;
}
}
return $product_lists;
}
/**
* Gets the necessarry product data to display in list table.
*
* @param array $args Query arguments for wc_get_products.
* @return array
*/
public function get_all_wc_products_data( $args ) {
$product_list = array();
$products = wc_get_products( $args );
foreach ( $products as $product ) {
// Get product details.
$prod_id = $product->get_id();
$prod_link = $product->get_permalink();
$prod_thumbnail = $product->get_image( array( 50, 50 ) );
$prod_title = $product->get_title();
$category = wc_get_product_category_list( $prod_id );
$prod_sku = $product->get_sku();
$prod_var = new WC_Product_Variable( $prod_id );
$variations = $prod_var->get_available_variations( 'objects' );
if ( count( $variations ) > 1 ) {
foreach ( $variations as $vars ) {
// Get specific variation details.
$var_id = $vars->get_id();
$var_sku = $vars->get_sku();
$var_thumbnail = $vars->get_image( array( 50, 50 ) );
$var_attributes = wc_get_formatted_variation( $vars, 'true' );
$product_list[] = array(
'ID' => esc_attr( $prod_id ),
'variation_id' => esc_attr( $var_id ),
'post_title' => esc_attr( $product->get_formatted_name() ),
'sku' => ! empty( $var_sku ) ? esc_attr( $var_sku ) : '–',
'attrib' => esc_attr( $var_attributes ),
'thumb' => $var_thumbnail,
'category' => $category,
'guid' => esc_attr( $prod_link ),
);
}
} else {
$product_list[] = array(
'ID' => esc_attr( $prod_id ),
'post_title' => esc_attr( $prod_title ),
'sku' => ! empty( esc_attr( $prod_sku ) ) ? esc_attr( $prod_sku ) : '–',
'attrib' => '–',
'thumb' => $prod_thumbnail,
'category' => $category,
'guid' => esc_attr( $prod_link ),
);
}
}
return $product_list;
}
/**
* Gets the necessarry product data to display on product meta box.
*
* @return array
*/
public function get_metabox_product_data() {
$product_list = array();
$product = wc_get_product( get_the_ID() );
$product_variable = new WC_Product_Variable( $product );
$variations = $product_variable->get_available_variations( 'objects' );
if ( ! empty( $variations ) ) {
foreach ( $variations as $vars ) {
$product_list[] = array(
'ID' => esc_attr( $vars->get_id() ),
'post_title' => esc_attr( $product->get_formatted_name() ),
'sku' => esc_attr( $vars->get_sku() ),
'attrib' => esc_attr( wc_get_formatted_variation( $vars, 'true' ) ),
'thumb' => $vars->get_image( array( 50, 50 ) ),
'guid' => esc_attr( $product->get_permalink() ),
);
}
} else {
$product_list[] = array(
'ID' => esc_attr( get_the_ID() ),
'post_title' => esc_attr( get_the_title() ),
'sku' => esc_attr( $product->get_sku() ),
'attrib' => '–',
'thumb' => $product->get_image( array( 100, 100 ) ),
'guid' => esc_attr( $product->get_permalink() ),
);
}
return $product_list;
}
/**
* Returns the ID of the most recent product.
*
* @return string Product ID.
*/
public function get_single_wc_product_id() {
$args = $this->get_wc_query_args( 'publish', 1, array( 'simple', 'variable', 'subscription', 'variable-subscription' ), 'ids' );
$product = wc_get_products( $args );
return $product;
}
/**
* Returns the query args for woocommerce products.
*
* @param string $status The post status.
* @param string $limit The number of products to be returned.
* @param array $type The type of products to be returned.
* @param string $return The return type.
* @return array
*/
public function get_wc_query_args( $status, $limit, $type = array(), $return = 'objects' ) {
return array(
'status' => $status,
'limit' => $limit,
'type' => $type,
'return' => $return,
);
}
}
}