Noptin_WooCommerce::get_product_details( $product_id )
Source Source
File: includes/integrations/class-noptin-woocommerce.php
public function get_product_details( $product_id ) { $product = wc_get_product( $product_id ); if ( empty( $product ) || ! $product->is_purchasable() ) { return array(); } $details = array( 'id' => $product->get_id(), 'name' => $product->get_name(), 'description' => $product->get_short_description(), 'url' => $product->get_permalink(), 'price' => $product->get_price(), 'type' => $product->get_type(), 'sku' => $product->get_sku(), 'inventory_quantity' => 0, 'variations' => array(), ); // Gallery images. $images = $product->get_gallery_image_ids(); // Add featured image to the beginning. array_unshift( $images, $product->get_image_id() ); // Remove duplicate and empty values. $images = array_unique( array_filter( $images ) ); // Convert image ids to urls. $details['images'] = array_filter( array_map( 'wp_get_attachment_url', $images ) ); // Variations. $variations = $product->get_children(); foreach ( $variations as $variation ) { $variation = $this->get_product_details( $variation ); if ( empty( $variation ) ) { continue; } $details['variations'][] = $variation; } if ( empty( $details['variations'] ) ) { unset( $details['variations'] ); $details['inventory_quantity'] = $product->get_stock_quantity(); } return $details; }
Expand full source code Collapse full source code View on GitHub