本文整理汇总了PHP中WC_Cache_Helper::get_transient_version方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Cache_Helper::get_transient_version方法的具体用法?PHP WC_Cache_Helper::get_transient_version怎么用?PHP WC_Cache_Helper::get_transient_version使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Cache_Helper
的用法示例。
在下文中一共展示了WC_Cache_Helper::get_transient_version方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: clear_transients
/**
* Clear the product/shop transients cache.
*
* ## EXAMPLES
*
* wp wc tool clear_transients
*
* @since 2.5.0
*/
public function clear_transients($args, $assoc_args)
{
wc_delete_product_transients();
wc_delete_shop_order_transients();
WC_Cache_Helper::get_transient_version('shipping', true);
WP_CLI::success('Product transients and shop order transients were cleared.');
}
示例2: delete_simple_flat_rate
/**
* Delete the simple flat rate.
*
* @since 2.3
*/
public static function delete_simple_flat_rate()
{
delete_option('woocommerce_flat_rate_settings');
delete_option('woocommerce_flat_rate');
WC_Cache_Helper::get_transient_version('shipping', true);
WC()->shipping->unregister_shipping_methods();
}
示例3: cancel_booking
/**
* Cancel a booking.
*/
public static function cancel_booking()
{
if (isset($_GET['cancel_booking']) && isset($_GET['booking_id'])) {
$booking_id = absint($_GET['booking_id']);
$booking = get_wc_booking($booking_id);
$booking_can_cancel = $booking->has_status(apply_filters('woocommerce_valid_booking_statuses_for_cancel', array('unpaid', 'pending-confirmation', 'confirmed', 'paid')));
$redirect = $_GET['redirect'];
if ($booking->has_status('cancelled')) {
// Already cancelled - take no action
} elseif ($booking_can_cancel && $booking->id == $booking_id && isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'woocommerce-bookings-cancel_booking')) {
// Cancel the booking
$booking->update_status('cancelled');
WC_Cache_Helper::get_transient_version('bookings', true);
// Message
wc_add_notice(apply_filters('woocommerce_booking_cancelled_notice', __('Your booking was cancelled.', 'woocommerce-bookings')), apply_filters('woocommerce_booking_cancelled_notice_type', 'notice'));
do_action('woocommerce_bookings_cancelled_booking', $booking->id);
} elseif (!$booking_can_cancel) {
wc_add_notice(__('Your booking can no longer be cancelled. Please contact us if you need assistance.', 'woocommerce-bookings'), 'error');
} else {
wc_add_notice(__('Invalid booking.', 'woocommerce-bookings'), 'error');
}
if ($redirect) {
wp_safe_redirect($redirect);
exit;
}
}
}
示例4: delete_product_query_transients
/**
* Delete product view transients when needed e.g. when post status changes, or visibility/stock status is modified.
*/
public static function delete_product_query_transients()
{
// Increments the transient version to invalidate cache
WC_Cache_Helper::get_transient_version('product_query', true);
// If not using an external caching system, we can clear the transients out manually and avoid filling our DB
if (!wp_using_ext_object_cache()) {
global $wpdb;
$wpdb->query("\n\t\t\t\tDELETE FROM `{$wpdb->options}`\n\t\t\t\tWHERE `option_name` LIKE ('\\_transient\\_wc\\_uf\\_pid\\_%')\n\t\t\t\tOR `option_name` LIKE ('\\_transient\\_timeout\\_wc\\_uf\\_pid\\_%')\n\t\t\t\tOR `option_name` LIKE ('\\_transient\\_wc\\_products\\_will\\_display\\_%')\n\t\t\t\tOR `option_name` LIKE ('\\_transient\\_timeout\\_wc\\_products\\_will\\_display\\_%')\n\t\t\t");
}
}
示例5: product_loop
/**
* Loop over found products.
* @param array $query_args
* @param array $atts
* @param string $loop_name
* @return string
*/
private static function product_loop($query_args, $atts, $loop_name)
{
global $woocommerce_loop;
$columns = absint($atts['columns']);
$woocommerce_loop['columns'] = $columns;
$woocommerce_loop['name'] = $loop_name;
$transient_name = 'wc_loop' . substr(md5(json_encode($query_args) . $loop_name), 28) . WC_Cache_Helper::get_transient_version('product_query');
$products = get_transient($transient_name);
if (false === $products || !is_a($products, 'WP_Query')) {
$products = new WP_Query(apply_filters('woocommerce_shortcode_products_query', $query_args, $atts, $loop_name));
set_transient($transient_name, $products, DAY_IN_SECONDS * 30);
}
ob_start();
if ($products->have_posts()) {
?>
<?php
do_action("woocommerce_shortcode_before_{$loop_name}_loop", $atts);
?>
<?php
woocommerce_product_loop_start();
?>
<?php
while ($products->have_posts()) {
$products->the_post();
?>
<?php
wc_get_template_part('content', 'product');
?>
<?php
}
// end of the loop.
?>
<?php
woocommerce_product_loop_end();
?>
<?php
do_action("woocommerce_shortcode_after_{$loop_name}_loop", $atts);
?>
<?php
} else {
do_action("woocommerce_shortcode_{$loop_name}_loop_no_results", $atts);
}
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
示例6: update
/**
* Update zone in the database.
*
* @since 2.7.0
* @param WC_Shipping_Zone
*/
public function update(&$zone)
{
global $wpdb;
if ($zone->get_id()) {
$wpdb->update($wpdb->prefix . 'woocommerce_shipping_zones', array('zone_name' => $zone->get_zone_name(), 'zone_order' => $zone->get_zone_order()), array('zone_id' => $zone->get_id()));
}
$zone->save_meta_data();
$this->save_locations($zone);
$zone->apply_changes();
WC_Cache_Helper::incr_cache_prefix('shipping_zones');
WC_Cache_Helper::get_transient_version('shipping', true);
}
示例7: get_bookings_for_objects
/**
* Gets bookings for product ids and resource ids
* @param array $ids
* @param array $status
* @return array of WC_Booking objects
*/
public static function get_bookings_for_objects($ids = array(), $status = array('confirmed', 'paid'))
{
$transient_name = 'book_fo_' . md5(http_build_query(array($ids, $status, WC_Cache_Helper::get_transient_version('bookings'))));
if (false === ($booking_ids = get_transient($transient_name))) {
$booking_ids = self::get_bookings_for_objects_query($ids, $status);
set_transient($transient_name, $booking_ids, DAY_IN_SECONDS * 30);
}
$bookings = array();
foreach ($booking_ids as $booking_id) {
$bookings[] = get_wc_booking($booking_id);
}
return $bookings;
}
示例8: get_children
/**
* Return the products children posts.
*
* @access public
* @return array
*/
public function get_children()
{
if (!is_array($this->children) || empty($this->children)) {
$transient_name = 'wc_product_children_ids_' . $this->id . WC_Cache_Helper::get_transient_version('product');
$this->children = get_transient($transient_name);
if (empty($this->children)) {
$args = apply_filters('woocommerce_grouped_children_args', array('post_parent' => $this->id, 'post_type' => 'product', 'orderby' => 'menu_order', 'order' => 'ASC', 'fields' => 'ids', 'post_status' => 'publish', 'numberposts' => -1));
$this->children = get_posts($args);
set_transient($transient_name, $this->children, YEAR_IN_SECONDS);
}
}
return (array) $this->children;
}
示例9: get_total_stock
/**
* Get total stock.
*
* This is the stock of parent and children combined.
*
* @access public
* @return int
*/
public function get_total_stock()
{
if (empty($this->total_stock)) {
$transient_name = 'wc_product_total_stock_' . $this->id . WC_Cache_Helper::get_transient_version('product');
if (false === ($this->total_stock = get_transient($transient_name))) {
$this->total_stock = $this->stock;
if (sizeof($this->get_children()) > 0) {
foreach ($this->get_children() as $child_id) {
$stock = get_post_meta($child_id, '_stock', true);
if ($stock != '') {
$this->total_stock += wc_stock_amount($stock);
}
}
}
set_transient($transient_name, $this->total_stock, DAY_IN_SECONDS * 30);
}
}
return wc_stock_amount($this->total_stock);
}
示例10: get_related
/**
* Returns array that conintains ids of related tours.
*
* @param int $limit
* @return array
*/
public function get_related($limit = 5)
{
$transient_name = 'wc_related_' . $limit . '_' . $this->id . WC_Cache_Helper::get_transient_version('product');
if (false === ($related_posts = get_transient($transient_name))) {
global $wpdb;
// Related products are found from category and tag
$tags_array = $this->get_related_terms('product_tag');
$tour_cats_array = $this->get_related_terms('tour_category');
// Don't bother if none are set
if (sizeof($tour_cats_array) == 1 && sizeof($tags_array) == 1) {
$related_posts = array();
} else {
// Sanitize
$exclude_ids = array_map('absint', array_merge(array(0, $this->id), $this->get_upsells()));
// Generate query
$query = $this->build_related_tours_query($tour_cats_array, $tags_array, $exclude_ids, $limit);
// Get the posts
$related_posts = $wpdb->get_col(implode(' ', $query));
}
set_transient($transient_name, $related_posts, DAY_IN_SECONDS * 30);
}
shuffle($related_posts);
return $related_posts;
}
示例11: read_price_data
/**
* Get an array of all sale and regular prices from all variations. This is used for example when displaying the price range at variable product level or seeing if the variable product is on sale.
*
* Can be filtered by plugins which modify costs, but otherwise will include the raw meta costs unlike get_price() which runs costs through the woocommerce_get_price filter.
* This is to ensure modified prices are not cached, unless intended.
*
* @since 2.7.0
* @param WC_Product
* @param bool $include_taxes If taxes should be calculated or not.
*/
private function read_price_data(&$product, $include_taxes = false)
{
global $wp_filter;
/**
* Transient name for storing prices for this product (note: Max transient length is 45)
* @since 2.5.0 a single transient is used per product for all prices, rather than many transients per product.
*/
$transient_name = 'wc_var_prices_' . $product->get_id();
/**
* Create unique cache key based on the tax location (affects displayed/cached prices), product version and active price filters.
* DEVELOPERS should filter this hash if offering conditonal pricing to keep it unique.
* @var string
*/
$price_hash = $include_taxes ? array(get_option('woocommerce_tax_display_shop', 'excl'), WC_Tax::get_rates()) : array(false);
$filter_names = array('woocommerce_variation_prices_price', 'woocommerce_variation_prices_regular_price', 'woocommerce_variation_prices_sale_price');
foreach ($filter_names as $filter_name) {
if (!empty($wp_filter[$filter_name])) {
$price_hash[$filter_name] = array();
foreach ($wp_filter[$filter_name] as $priority => $callbacks) {
$price_hash[$filter_name][] = array_values(wp_list_pluck($callbacks, 'function'));
}
}
}
$price_hash[] = WC_Cache_Helper::get_transient_version('product');
$price_hash = md5(json_encode(apply_filters('woocommerce_get_variation_prices_hash', $price_hash, $product, $include_taxes)));
/**
* $this->prices_array is an array of values which may have been modified from what is stored in transients - this may not match $transient_cached_prices_array.
* If the value has already been generated, we don't need to grab the values again so just return them. They are already filtered.
*/
if (!empty($this->prices_array[$price_hash])) {
if ($include_taxes) {
$product->set_variation_prices_including_taxes($this->prices_array[$price_hash]);
} else {
$product->set_variation_prices($this->prices_array[$price_hash]);
}
/**
* No locally cached value? Get the data from the transient or generate it.
*/
} else {
// Get value of transient
$transient_cached_prices_array = array_filter((array) json_decode(strval(get_transient($transient_name)), true));
// If the product version has changed since the transient was last saved, reset the transient cache.
if (empty($transient_cached_prices_array['version']) || WC_Cache_Helper::get_transient_version('product') !== $transient_cached_prices_array['version']) {
$transient_cached_prices_array = array('version' => WC_Cache_Helper::get_transient_version('product'));
}
// If the prices are not stored for this hash, generate them and add to the transient.
if (empty($transient_cached_prices_array[$price_hash])) {
$prices = array();
$regular_prices = array();
$sale_prices = array();
$variation_ids = $product->get_visible_children();
foreach ($variation_ids as $variation_id) {
if ($variation = wc_get_product($variation_id)) {
$price = apply_filters('woocommerce_variation_prices_price', $variation->get_price('edit'), $variation, $product);
$regular_price = apply_filters('woocommerce_variation_prices_regular_price', $variation->get_regular_price('edit'), $variation, $product);
$sale_price = apply_filters('woocommerce_variation_prices_sale_price', $variation->get_sale_price('edit'), $variation, $product);
// Skip empty prices
if ('' === $price) {
continue;
}
// If sale price does not equal price, the product is not yet on sale
if ($sale_price === $regular_price || $sale_price !== $price) {
$sale_price = $regular_price;
}
// If we are getting prices for display, we need to account for taxes
if ($include_taxes) {
if ('incl' === get_option('woocommerce_tax_display_shop')) {
$price = '' === $price ? '' : wc_get_price_including_tax($variation, array('qty' => 1, 'price' => $price));
$regular_price = '' === $regular_price ? '' : wc_get_price_including_tax($variation, array('qty' => 1, 'price' => $regular_price));
$sale_price = '' === $sale_price ? '' : wc_get_price_including_tax($variation, array('qty' => 1, 'price' => $sale_price));
} else {
$price = '' === $price ? '' : wc_get_price_excluding_tax($variation, array('qty' => 1, 'price' => $price));
$regular_price = '' === $regular_price ? '' : wc_get_price_excluding_tax($variation, array('qty' => 1, 'price' => $regular_price));
$sale_price = '' === $sale_price ? '' : wc_get_price_excluding_tax($variation, array('qty' => 1, 'price' => $sale_price));
}
}
$prices[$variation_id] = wc_format_decimal($price, wc_get_price_decimals());
$regular_prices[$variation_id] = wc_format_decimal($regular_price, wc_get_price_decimals());
$sale_prices[$variation_id] = wc_format_decimal($sale_price . '.00', wc_get_price_decimals());
}
}
asort($prices);
asort($regular_prices);
asort($sale_prices);
$transient_cached_prices_array[$price_hash] = array('price' => $prices, 'regular_price' => $regular_prices, 'sale_price' => $sale_prices);
set_transient($transient_name, json_encode($transient_cached_prices_array), DAY_IN_SECONDS * 30);
}
/**
* Give plugins one last chance to filter the variation prices array which has been generated and store locally to the class.
* This value may differ from the transient cache. It is filtered once before storing locally.
//.........这里部分代码省略.........
示例12: wc_scheduled_sales
/**
* Function which handles the start and end of scheduled sales via cron.
*
* @access public
*/
function wc_scheduled_sales()
{
global $wpdb;
// Sales which are due to start
$product_ids = $wpdb->get_col($wpdb->prepare("\n\t\tSELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta\n\t\tLEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id\n\t\tLEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id\n\t\tWHERE postmeta.meta_key = '_sale_price_dates_from'\n\t\tAND postmeta_2.meta_key = '_price'\n\t\tAND postmeta_3.meta_key = '_sale_price'\n\t\tAND postmeta.meta_value > 0\n\t\tAND postmeta.meta_value < %s\n\t\tAND postmeta_2.meta_value != postmeta_3.meta_value\n\t", current_time('timestamp')));
if ($product_ids) {
foreach ($product_ids as $product_id) {
$sale_price = get_post_meta($product_id, '_sale_price', true);
if ($sale_price) {
update_post_meta($product_id, '_price', $sale_price);
} else {
// No sale price!
update_post_meta($product_id, '_sale_price_dates_from', '');
update_post_meta($product_id, '_sale_price_dates_to', '');
}
$parent = wp_get_post_parent_id($product_id);
// Sync parent
if ($parent) {
// We can force variable product prices to sync up by removing their min price meta
delete_post_meta($parent, '_min_price_variation_id');
// Grouped products need syncing via a function
$this_product = wc_get_product($product_id);
if ($this_product->is_type('simple')) {
$this_product->grouped_product_sync();
}
}
}
delete_transient('wc_products_onsale');
}
// Sales which are due to end
$product_ids = $wpdb->get_col($wpdb->prepare("\n\t\tSELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta\n\t\tLEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id\n\t\tLEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id\n\t\tWHERE postmeta.meta_key = '_sale_price_dates_to'\n\t\tAND postmeta_2.meta_key = '_price'\n\t\tAND postmeta_3.meta_key = '_regular_price'\n\t\tAND postmeta.meta_value > 0\n\t\tAND postmeta.meta_value < %s\n\t\tAND postmeta_2.meta_value != postmeta_3.meta_value\n\t", current_time('timestamp')));
if ($product_ids) {
foreach ($product_ids as $product_id) {
$regular_price = get_post_meta($product_id, '_regular_price', true);
update_post_meta($product_id, '_price', $regular_price);
update_post_meta($product_id, '_sale_price', '');
update_post_meta($product_id, '_sale_price_dates_from', '');
update_post_meta($product_id, '_sale_price_dates_to', '');
$parent = wp_get_post_parent_id($product_id);
// Sync parent
if ($parent) {
// Grouped products need syncing via a function
$this_product = wc_get_product($product_id);
if ($this_product->is_type('simple')) {
$this_product->grouped_product_sync();
}
}
}
WC_Cache_Helper::get_transient_version('product', true);
delete_transient('wc_products_onsale');
}
}
示例13: get_variation_prices
/**
* Get an array of all sale and regular prices from all variations. This is used for example when displaying the price range at variable product level or seeing if the variable product is on sale.
*
* Can be filtered by plugins which modify costs, but otherwise will include the raw meta costs unlike get_price() which runs costs through the woocommerce_get_price filter.
* This is to ensure modified prices are not cached, unless intended.
*
* @param bool $display Are prices for display? If so, taxes will be calculated.
* @return array() Array of RAW prices, regular prices, and sale prices with keys set to variation ID.
*/
public function get_variation_prices($display = false)
{
global $wp_filter;
/**
* Transient name for storing prices for this product (note: Max transient length is 45)
* @since 2.5.0 a single transient is used per product for all prices, rather than many transients per product.
*/
$transient_name = 'wc_var_prices_' . $this->id;
/**
* Create unique cache key based on the tax location (affects displayed/cached prices), product version and active price filters.
* DEVELOPERS should filter this hash if offering conditonal pricing to keep it unique.
* @var string
*/
if ($display) {
$price_hash = array(get_option('woocommerce_tax_display_shop', 'excl'), WC_Tax::get_rates());
} else {
$price_hash = array(false);
}
$filter_names = array('woocommerce_variation_prices_price', 'woocommerce_variation_prices_regular_price', 'woocommerce_variation_prices_sale_price');
foreach ($filter_names as $filter_name) {
if (!empty($wp_filter[$filter_name])) {
$price_hash[$filter_name] = array();
foreach ($wp_filter[$filter_name] as $priority => $callbacks) {
$price_hash[$filter_name][] = array_values(wp_list_pluck($callbacks, 'function'));
}
}
}
$price_hash = md5(json_encode(apply_filters('woocommerce_get_variation_prices_hash', $price_hash, $this, $display)));
// If the value has already been generated, we don't need to grab the values again.
if (empty($this->prices_array[$price_hash])) {
// Get value of transient
$prices_array = array_filter((array) json_decode(strval(get_transient($transient_name)), true));
// If the product version has changed, reset cache
if (empty($prices_array['version']) || $prices_array['version'] !== WC_Cache_Helper::get_transient_version('product')) {
$this->prices_array = array('version' => WC_Cache_Helper::get_transient_version('product'));
}
// If the prices are not stored for this hash, generate them
if (empty($prices_array[$price_hash])) {
$prices = array();
$regular_prices = array();
$sale_prices = array();
$variation_ids = $this->get_children(true);
foreach ($variation_ids as $variation_id) {
if ($variation = $this->get_child($variation_id)) {
$price = apply_filters('woocommerce_variation_prices_price', $variation->price, $variation, $this);
$regular_price = apply_filters('woocommerce_variation_prices_regular_price', $variation->regular_price, $variation, $this);
$sale_price = apply_filters('woocommerce_variation_prices_sale_price', $variation->sale_price, $variation, $this);
// Skip empty prices
if ('' === $price) {
continue;
}
// If sale price does not equal price, the product is not yet on sale
if ($sale_price === $regular_price || $sale_price !== $price) {
$sale_price = $regular_price;
}
// If we are getting prices for display, we need to account for taxes
if ($display) {
if ('incl' === get_option('woocommerce_tax_display_shop')) {
$price = '' === $price ? '' : $variation->get_price_including_tax(1, $price);
$regular_price = '' === $regular_price ? '' : $variation->get_price_including_tax(1, $regular_price);
$sale_price = '' === $sale_price ? '' : $variation->get_price_including_tax(1, $sale_price);
} else {
$price = '' === $price ? '' : $variation->get_price_excluding_tax(1, $price);
$regular_price = '' === $regular_price ? '' : $variation->get_price_excluding_tax(1, $regular_price);
$sale_price = '' === $sale_price ? '' : $variation->get_price_excluding_tax(1, $sale_price);
}
}
$prices[$variation_id] = wc_format_decimal($price, wc_get_price_decimals());
$regular_prices[$variation_id] = wc_format_decimal($regular_price, wc_get_price_decimals());
$sale_prices[$variation_id] = wc_format_decimal($sale_price . '.00', wc_get_price_decimals());
}
}
asort($prices);
asort($regular_prices);
asort($sale_prices);
$prices_array[$price_hash] = array('price' => $prices, 'regular_price' => $regular_prices, 'sale_price' => $sale_prices);
set_transient($transient_name, json_encode($prices_array), DAY_IN_SECONDS * 30);
}
/**
* Give plugins one last chance to filter the variation prices array which has been generated.
*/
$this->prices_array[$price_hash] = apply_filters('woocommerce_variation_prices', $prices_array[$price_hash], $this, $display);
}
/**
* Return the values.
*/
return $this->prices_array[$price_hash];
}
示例14: wc_delete_shop_order_transients
/**
* Clear all transients cache for order data.
*
* @param int $post_id (default: 0)
*/
function wc_delete_shop_order_transients($post_id = 0)
{
$post_id = absint($post_id);
$transients_to_clear = array();
// Clear report transients
$reports = WC_Admin_Reports::get_reports();
foreach ($reports as $report_group) {
foreach ($report_group['reports'] as $report_key => $report) {
$transients_to_clear[] = 'wc_report_' . $report_key;
}
}
// clear API report transient
$transients_to_clear[] = 'wc_admin_report';
// Clear transients where we have names
foreach ($transients_to_clear as $transient) {
delete_transient($transient);
}
// Increments the transient version to invalidate cache
WC_Cache_Helper::get_transient_version('orders', true);
do_action('woocommerce_delete_shop_order_transients', $post_id);
}
示例15: wc_delete_product_transients
/**
* Clear all transients cache for product data.
*
* @param int $post_id (default: 0)
*/
function wc_delete_product_transients($post_id = 0)
{
// Core transients
$transients_to_clear = array('wc_products_onsale', 'wc_featured_products', 'wc_outofstock_count', 'wc_low_stock_count');
// Transients that include an ID
$post_transient_names = array('wc_product_children_', 'wc_product_total_stock_');
if ($post_id > 0) {
foreach ($post_transient_names as $transient) {
$transients_to_clear[] = $transient . $post_id;
}
}
// Delete transients
foreach ($transients_to_clear as $transient) {
delete_transient($transient);
}
// Increments the transient version to invalidate cache
WC_Cache_Helper::get_transient_version('product', true);
do_action('woocommerce_delete_product_transients', $post_id);
}