当前位置: 首页>>代码示例>>PHP>>正文


PHP WC_Product::set_image_id方法代码示例

本文整理汇总了PHP中WC_Product::set_image_id方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product::set_image_id方法的具体用法?PHP WC_Product::set_image_id怎么用?PHP WC_Product::set_image_id使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在WC_Product的用法示例。


在下文中一共展示了WC_Product::set_image_id方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: save_product_images

 /**
  * Save product images
  *
  * @since  2.2
  * @param WC_Product $product
  * @param  array $images
  * @throws WC_API_Exception
  */
 protected function save_product_images($product, $images)
 {
     if (is_array($images)) {
         $gallery = array();
         foreach ($images as $image) {
             if (isset($image['position']) && 0 == $image['position']) {
                 $attachment_id = isset($image['id']) ? absint($image['id']) : 0;
                 if (0 === $attachment_id && isset($image['src'])) {
                     $upload = $this->upload_product_image(esc_url_raw($image['src']));
                     if (is_wp_error($upload)) {
                         throw new WC_API_Exception('woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), 400);
                     }
                     $attachment_id = $this->set_product_image_as_attachment($upload, $product->get_id());
                 }
                 $product->set_image_id($attachment_id);
             } else {
                 $attachment_id = isset($image['id']) ? absint($image['id']) : 0;
                 if (0 === $attachment_id && isset($image['src'])) {
                     $upload = $this->upload_product_image(esc_url_raw($image['src']));
                     if (is_wp_error($upload)) {
                         throw new WC_API_Exception('woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), 400);
                     }
                     $gallery[] = $this->set_product_image_as_attachment($upload, $id);
                 } else {
                     $gallery[] = $attachment_id;
                 }
             }
         }
         if (!empty($gallery)) {
             $product->set_gallery_image_ids($gallery);
         }
     } else {
         $product->set_image_id('');
         $product->set_gallery_image_ids(array());
     }
     return $product;
 }
开发者ID:woocommerce,项目名称:woocommerce,代码行数:45,代码来源:class-wc-api-products.php

示例2: save_product_images

 /**
  * Save product images.
  *
  * @throws WC_REST_Exception REST API exceptions.
  * @param WC_Product $product Product instance.
  * @param array      $images  Images data.
  * @return WC_Product
  */
 protected function save_product_images($product, $images)
 {
     if (is_array($images)) {
         $gallery = array();
         foreach ($images as $image) {
             $attachment_id = isset($image['id']) ? absint($image['id']) : 0;
             if (0 === $attachment_id && isset($image['src'])) {
                 $upload = wc_rest_upload_image_from_url(esc_url_raw($image['src']));
                 if (is_wp_error($upload)) {
                     if (!apply_filters('woocommerce_rest_suppress_image_upload_error', false, $upload, $product->get_id(), $images)) {
                         throw new WC_REST_Exception('woocommerce_product_image_upload_error', $upload->get_error_message(), 400);
                     } else {
                         continue;
                     }
                 }
                 $attachment_id = wc_rest_set_uploaded_image_as_attachment($upload, $product->get_id());
             }
             if (isset($image['position']) && 0 === $image['position']) {
                 $product->set_image_id($attachment_id);
             } else {
                 $gallery[] = $attachment_id;
             }
             // Set the image alt if present.
             if (!empty($image['alt'])) {
                 update_post_meta($attachment_id, '_wp_attachment_image_alt', wc_clean($image['alt']));
             }
             // Set the image name if present.
             if (!empty($image['name'])) {
                 wp_update_post(array('ID' => $attachment_id, 'post_title' => $image['name']));
             }
         }
         if (!empty($gallery)) {
             $product->set_gallery_image_ids($gallery);
         }
     } else {
         $product->set_image_id('');
         $product->set_gallery_image_ids(array());
     }
     return $product;
 }
开发者ID:shivapoudel,项目名称:woocommerce,代码行数:48,代码来源:class-wc-rest-products-controller.php


注:本文中的WC_Product::set_image_id方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。