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


PHP WC_Product::set_featured方法代码示例

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


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

示例1: save_product_meta

 /**
  * Save product meta
  *
  * @since  2.2
  * @param  WC_Product $product
  * @param  array $data
  * @return WC_Product
  * @throws WC_API_Exception
  */
 protected function save_product_meta($product, $data)
 {
     global $wpdb;
     // Virtual
     if (isset($data['virtual'])) {
         $product->set_virtual($data['virtual']);
     }
     // Tax status
     if (isset($data['tax_status'])) {
         $product->set_tax_status(wc_clean($data['tax_status']));
     }
     // Tax Class
     if (isset($data['tax_class'])) {
         $product->set_tax_class(wc_clean($data['tax_class']));
     }
     // Catalog Visibility
     if (isset($data['catalog_visibility'])) {
         $product->set_catalog_visibility(wc_clean($data['catalog_visibility']));
     }
     // Purchase Note
     if (isset($data['purchase_note'])) {
         $product->set_purchase_note(wc_clean($data['purchase_note']));
     }
     // Featured Product
     if (isset($data['featured'])) {
         $product->set_featured($data['featured']);
     }
     // Shipping data
     $product = $this->save_product_shipping_data($product, $data);
     // SKU
     if (isset($data['sku'])) {
         $sku = $product->get_sku();
         $new_sku = wc_clean($data['sku']);
         if ('' == $new_sku) {
             $product->set_sku('');
         } elseif ($new_sku !== $sku) {
             if (!empty($new_sku)) {
                 $unique_sku = wc_product_has_unique_sku($product->get_id(), $new_sku);
                 if (!$unique_sku) {
                     throw new WC_API_Exception('woocommerce_api_product_sku_already_exists', __('The SKU already exists on another product.', 'woocommerce'), 400);
                 } else {
                     $product->set_sku($new_sku);
                 }
             } else {
                 $product->set_sku('');
             }
         }
     }
     // Attributes
     if (isset($data['attributes'])) {
         $attributes = array();
         foreach ($data['attributes'] as $attribute) {
             $is_taxonomy = 0;
             $taxonomy = 0;
             if (!isset($attribute['name'])) {
                 continue;
             }
             $attribute_slug = sanitize_title($attribute['name']);
             if (isset($attribute['slug'])) {
                 $taxonomy = $this->get_attribute_taxonomy_by_slug($attribute['slug']);
                 $attribute_slug = sanitize_title($attribute['slug']);
             }
             if ($taxonomy) {
                 $is_taxonomy = 1;
             }
             if ($is_taxonomy) {
                 $attribute_id = wc_attribute_taxonomy_id_by_name($attribute['name']);
                 if (isset($attribute['options'])) {
                     $options = $attribute['options'];
                     if (!is_array($attribute['options'])) {
                         // Text based attributes - Posted values are term names
                         $options = explode(WC_DELIMITER, $options);
                     }
                     $values = array_map('wc_sanitize_term_text_based', $options);
                     $values = array_filter($values, 'strlen');
                 } else {
                     $values = array();
                 }
                 // Update post terms
                 if (taxonomy_exists($taxonomy)) {
                     wp_set_object_terms($product->get_id(), $values, $taxonomy);
                 }
                 if (!empty($values)) {
                     // Add attribute to array, but don't set values.
                     $attribute_object = new WC_Product_Attribute();
                     $attribute_object->set_id($attribute_id);
                     $attribute_object->set_name($taxonomy);
                     $attribute_object->set_options($values);
                     $attribute_object->set_position(isset($attribute['position']) ? absint($attribute['position']) : 0);
                     $attribute_object->set_visible(isset($attribute['visible']) && $attribute['visible'] ? 1 : 0);
                     $attribute_object->set_variation(isset($attribute['variation']) && $attribute['variation'] ? 1 : 0);
//.........这里部分代码省略.........
开发者ID:woocommerce,项目名称:woocommerce,代码行数:101,代码来源:class-wc-api-products.php

示例2: save_product_meta

 /**
  * Save product meta.
  *
  * @throws WC_REST_Exception REST API exceptions.
  * @param WC_Product      $product Product instance.
  * @param WP_REST_Request $request Request data.
  * @return WC_Product
  */
 protected function save_product_meta($product, $request)
 {
     global $wpdb;
     // Virtual.
     if (isset($request['virtual'])) {
         $product->set_virtual($request['virtual']);
     }
     // Tax status.
     if (isset($request['tax_status'])) {
         $product->set_tax_status($request['tax_status']);
     }
     // Tax Class.
     if (isset($request['tax_class'])) {
         $product->set_tax_class($request['tax_class']);
     }
     // Catalog Visibility.
     if (isset($request['catalog_visibility'])) {
         $product->set_catalog_visibility($request['catalog_visibility']);
     }
     // Purchase Note.
     if (isset($request['purchase_note'])) {
         $product->set_purchase_note(wc_clean($request['purchase_note']));
     }
     // Featured Product.
     if (isset($request['featured'])) {
         $product->set_featured($request['featured']);
     }
     // Shipping data.
     $product = $this->save_product_shipping_data($product, $request);
     // SKU.
     if (isset($request['sku'])) {
         $product->set_sku(wc_clean($request['sku']));
     }
     // Attributes.
     if (isset($request['attributes'])) {
         $attributes = array();
         foreach ($request['attributes'] as $attribute) {
             $attribute_id = 0;
             $attribute_name = '';
             // Check ID for global attributes or name for product attributes.
             if (!empty($attribute['id'])) {
                 $attribute_id = absint($attribute['id']);
                 $attribute_name = wc_attribute_taxonomy_name_by_id($attribute_id);
             } elseif (!empty($attribute['name'])) {
                 $attribute_name = wc_clean($attribute['name']);
             }
             if (!$attribute_id && !$attribute_name) {
                 continue;
             }
             if ($attribute_id) {
                 if (isset($attribute['options'])) {
                     $options = $attribute['options'];
                     if (!is_array($attribute['options'])) {
                         // Text based attributes - Posted values are term names.
                         $options = explode(WC_DELIMITER, $options);
                     }
                     $values = array_map('wc_sanitize_term_text_based', $options);
                     $values = array_filter($values, 'strlen');
                 } else {
                     $values = array();
                 }
                 if (!empty($values)) {
                     // Add attribute to array, but don't set values.
                     $attribute_object = new WC_Product_Attribute();
                     $attribute_object->set_id($attribute_id);
                     $attribute_object->set_name($attribute_name);
                     $attribute_object->set_options($values);
                     $attribute_object->set_position(isset($attribute['position']) ? (string) absint($attribute['position']) : '0');
                     $attribute_object->set_visible(isset($attribute['visible']) && $attribute['visible'] ? 1 : 0);
                     $attribute_object->set_variation(isset($attribute['variation']) && $attribute['variation'] ? 1 : 0);
                     $attributes[] = $attribute_object;
                 }
             } elseif (isset($attribute['options'])) {
                 // Custom attribute - Add attribute to array and set the values.
                 if (is_array($attribute['options'])) {
                     $values = $attribute['options'];
                 } else {
                     $values = explode(WC_DELIMITER, $attribute['options']);
                 }
                 $attribute_object = new WC_Product_Attribute();
                 $attribute_object->set_name($attribute_name);
                 $attribute_object->set_options($values);
                 $attribute_object->set_position(isset($attribute['position']) ? (string) absint($attribute['position']) : '0');
                 $attribute_object->set_visible(isset($attribute['visible']) && $attribute['visible'] ? 1 : 0);
                 $attribute_object->set_variation(isset($attribute['variation']) && $attribute['variation'] ? 1 : 0);
                 $attributes[] = $attribute_object;
             }
         }
         $product->set_attributes($attributes);
     }
     // Sales and prices.
     if (in_array($product->get_type(), array('variable', 'grouped'), true)) {
//.........这里部分代码省略.........
开发者ID:shivapoudel,项目名称:woocommerce,代码行数:101,代码来源:class-wc-rest-products-controller.php

示例3: bulk_edit_save

 /**
  * Bulk edit.
  *
  * @param integer $post_id
  * @param WC_Product $product
  */
 public function bulk_edit_save($post_id, $product)
 {
     $old_regular_price = $product->get_regular_price();
     $old_sale_price = $product->get_sale_price();
     // Save fields
     if (!empty($_REQUEST['change_weight']) && isset($_REQUEST['_weight'])) {
         $product->set_weight(wc_clean(stripslashes($_REQUEST['_weight'])));
     }
     if (!empty($_REQUEST['change_dimensions'])) {
         if (isset($_REQUEST['_length'])) {
             $product->set_length(wc_clean(stripslashes($_REQUEST['_length'])));
         }
         if (isset($_REQUEST['_width'])) {
             $product->set_width(wc_clean(stripslashes($_REQUEST['_width'])));
         }
         if (isset($_REQUEST['_height'])) {
             $product->set_height(wc_clean(stripslashes($_REQUEST['_height'])));
         }
     }
     if (!empty($_REQUEST['_tax_status'])) {
         $product->set_tax_status(wc_clean($_REQUEST['_tax_status']));
     }
     if (!empty($_REQUEST['_tax_class'])) {
         $tax_class = wc_clean($_REQUEST['_tax_class']);
         if ('standard' == $tax_class) {
             $tax_class = '';
         }
         $product->set_tax_class($tax_class);
     }
     if (!empty($_REQUEST['_shipping_class'])) {
         $shipping_class = '_no_shipping_class' == $_REQUEST['_shipping_class'] ? '' : wc_clean($_REQUEST['_shipping_class']);
         $shipping_class_id = $data_store->get_shipping_class_id_by_slug($shipping_class);
         if ($shipping_class_id) {
             $product->set_shipping_class_id($shipping_class_id);
         }
     }
     if (!empty($_REQUEST['_visibility'])) {
         $product->set_catalog_visibility(wc_clean($_REQUEST['_visibility']));
     }
     if (!empty($_REQUEST['_featured'])) {
         $product->set_featured(stripslashes($_REQUEST['_featured']));
     }
     // Sold Individually
     if (!empty($_REQUEST['_sold_individually'])) {
         if ('yes' === $_REQUEST['_sold_individually']) {
             $product->set_sold_individually('yes');
         } else {
             $product->set_sold_individually('');
         }
     }
     // Handle price - remove dates and set to lowest
     $change_price_product_types = apply_filters('woocommerce_bulk_edit_save_price_product_types', array('simple', 'external'));
     $can_product_type_change_price = false;
     foreach ($change_price_product_types as $product_type) {
         if ($product->is_type($product_type)) {
             $can_product_type_change_price = true;
             break;
         }
     }
     if ($can_product_type_change_price) {
         $price_changed = false;
         if (!empty($_REQUEST['change_regular_price'])) {
             $change_regular_price = absint($_REQUEST['change_regular_price']);
             $regular_price = esc_attr(stripslashes($_REQUEST['_regular_price']));
             switch ($change_regular_price) {
                 case 1:
                     $new_price = $regular_price;
                     break;
                 case 2:
                     if (strstr($regular_price, '%')) {
                         $percent = str_replace('%', '', $regular_price) / 100;
                         $new_price = $old_regular_price + round($old_regular_price * $percent, wc_get_price_decimals());
                     } else {
                         $new_price = $old_regular_price + $regular_price;
                     }
                     break;
                 case 3:
                     if (strstr($regular_price, '%')) {
                         $percent = str_replace('%', '', $regular_price) / 100;
                         $new_price = max(0, $old_regular_price - round($old_regular_price * $percent, wc_get_price_decimals()));
                     } else {
                         $new_price = max(0, $old_regular_price - $regular_price);
                     }
                     break;
                 default:
                     break;
             }
             if (isset($new_price) && $new_price != $old_regular_price) {
                 $price_changed = true;
                 $new_price = round($new_price, wc_get_price_decimals());
                 $product->set_regular_price($new_price);
             }
         }
         if (!empty($_REQUEST['change_sale_price'])) {
//.........这里部分代码省略.........
开发者ID:shivapoudel,项目名称:woocommerce,代码行数:101,代码来源:class-wc-admin-post-types.php


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