本文整理汇总了PHP中WC_Cache_Helper类的典型用法代码示例。如果您正苦于以下问题:PHP WC_Cache_Helper类的具体用法?PHP WC_Cache_Helper怎么用?PHP WC_Cache_Helper使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WC_Cache_Helper类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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();
}
示例2: 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.');
}
示例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: remove_mock_zones
/**
* Remove all zones
*/
public static function remove_mock_zones()
{
global $wpdb;
$wpdb->query("TRUNCATE TABLE {$wpdb->prefix}woocommerce_shipping_zone_methods;");
$wpdb->query("TRUNCATE TABLE {$wpdb->prefix}woocommerce_shipping_zone_locations;");
$wpdb->query("TRUNCATE TABLE {$wpdb->prefix}woocommerce_shipping_zones;");
WC_Cache_Helper::incr_cache_prefix('shipping_zones');
}
示例5: 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");
}
}
示例6: 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>';
}
示例7: save
/**
* Save settings.
*/
public function save()
{
global $current_section, $wpdb;
if (!$current_section) {
$settings = $this->get_settings();
WC_Admin_Settings::save_fields($settings);
} elseif (!empty($_POST['tax_rate_country'])) {
$this->save_tax_rates();
}
WC_Cache_Helper::incr_cache_prefix('taxes');
}
示例8: 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;
}
示例9: 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;
}
示例10: 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);
}
示例11: 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;
}
示例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_customer_location
/**
* Locate user via AJAX
*/
public static function get_customer_location()
{
$location_hash = WC_Cache_Helper::geolocation_ajax_get_location_hash();
wp_send_json_success(array('hash' => $location_hash));
}
示例14: 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];
}
示例15: get_item_meta_array
/**
* Get all item meta data in array format in the order it was saved. Does not group meta by key like get_item_meta().
*
* @param mixed $order_item_id
* @return array of objects
*/
public function get_item_meta_array($order_item_id)
{
global $wpdb;
// Get cache key - uses get_cache_prefix to invalidate when needed
$cache_key = WC_Cache_Helper::get_cache_prefix('orders') . 'item_meta_array_' . $order_item_id;
$item_meta_array = wp_cache_get($cache_key, 'orders');
if (false === $item_meta_array) {
$item_meta_array = array();
$metadata = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value, meta_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d ORDER BY meta_id", absint($order_item_id)));
foreach ($metadata as $metadata_row) {
$item_meta_array[$metadata_row->meta_id] = (object) array('key' => $metadata_row->meta_key, 'value' => $metadata_row->meta_value);
}
wp_cache_set($cache_key, $item_meta_array, 'orders');
}
return $item_meta_array;
}