get_image_id() public method

Get main image ID.
Since: 2.7.0
public get_image_id ( string $context = 'view' ) : string
$context string
return string
 /**
  * Get the images for a product or product variation.
  *
  * @param WC_Product|WC_Product_Variation $product Product instance.
  * @return array
  */
 protected function get_images($product)
 {
     $images = array();
     $attachment_ids = array();
     // Add featured image.
     if (has_post_thumbnail($product->get_id())) {
         $attachment_ids[] = $product->get_image_id();
     }
     // Add gallery images.
     $attachment_ids = array_merge($attachment_ids, $product->get_gallery_image_ids());
     // Build image data.
     foreach ($attachment_ids as $position => $attachment_id) {
         $attachment_post = get_post($attachment_id);
         if (is_null($attachment_post)) {
             continue;
         }
         $attachment = wp_get_attachment_image_src($attachment_id, 'full');
         if (!is_array($attachment)) {
             continue;
         }
         $images[] = array('id' => (int) $attachment_id, 'date_created' => wc_rest_prepare_date_response($attachment_post->post_date_gmt), 'date_modified' => wc_rest_prepare_date_response($attachment_post->post_modified_gmt), 'src' => current($attachment), 'name' => get_the_title($attachment_id), 'alt' => get_post_meta($attachment_id, '_wp_attachment_image_alt', true), 'position' => (int) $position);
     }
     // Set a placeholder image if the product has no images set.
     if (empty($images)) {
         $images[] = array('id' => 0, 'date_created' => wc_rest_prepare_date_response(current_time('mysql')), 'date_modified' => wc_rest_prepare_date_response(current_time('mysql')), 'src' => wc_placeholder_img_src(), 'name' => __('Placeholder', 'woocommerce'), 'alt' => __('Placeholder', 'woocommerce'), 'position' => 0);
     }
     return $images;
 }
 /**
  * Get the images for a product or product variation
  *
  * @since 2.1
  * @param WC_Product|WC_Product_Variation $product
  * @return array
  */
 private function get_images($product)
 {
     $images = $attachment_ids = array();
     $product_image = $product->get_image_id();
     // Add featured image.
     if (!empty($product_image)) {
         $attachment_ids[] = $product_image;
     }
     // add gallery images.
     $attachment_ids = array_merge($attachment_ids, $product->get_gallery_image_ids());
     // Build image data.
     foreach ($attachment_ids as $position => $attachment_id) {
         $attachment_post = get_post($attachment_id);
         if (is_null($attachment_post)) {
             continue;
         }
         $attachment = wp_get_attachment_image_src($attachment_id, 'full');
         if (!is_array($attachment)) {
             continue;
         }
         $images[] = array('id' => (int) $attachment_id, 'created_at' => $this->server->format_datetime($attachment_post->post_date_gmt), 'updated_at' => $this->server->format_datetime($attachment_post->post_modified_gmt), 'src' => current($attachment), 'title' => get_the_title($attachment_id), 'alt' => get_post_meta($attachment_id, '_wp_attachment_image_alt', true), 'position' => $position);
     }
     // Set a placeholder image if the product has no images set.
     if (empty($images)) {
         $images[] = array('id' => 0, 'created_at' => $this->server->format_datetime(time()), 'updated_at' => $this->server->format_datetime(time()), 'src' => wc_placeholder_img_src(), 'title' => __('Placeholder', 'woocommerce'), 'alt' => __('Placeholder', 'woocommerce'), 'position' => 0);
     }
     return $images;
 }
Beispiel #3
0
 public function getProductsByCategory($id_category)
 {
     $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
     if ($mysqli->connect_errno) {
         printf("Connection error: %s\n", $mysqli->connect_error);
         exit;
     }
     $sql = "SELECT p.id, p.post_title\n\t\t\t\tFROM wp_posts p, wp_term_relationships wr\n\t\t\t\tWHERE p.post_parent = 0\n\t\t\t\tAND wr.object_id = p.id\n\t\t\t\tAND wr.term_taxonomy_id = " . $id_category;
     $result = $mysqli->query($sql);
     $i = 0;
     $data = array();
     while ($row = $result->fetch_array()) {
         $data[$i]['id'] = $row['id'];
         $data[$i]['name'] = utf8_decode($row['post_title']);
         $prod = new WC_Product($row['id']);
         $data[$i]['image'] = utf8_decode(wp_get_attachment_url($prod->get_image_id()));
         $data[$i]['price'] = $prod->get_price();
         $i++;
     }
     $datajson = '{"products": ' . json_encode($data) . ' }';
     return $datajson;
 }