本文整理汇总了PHP中WC_Product_Variation类的典型用法代码示例。如果您正苦于以下问题:PHP WC_Product_Variation类的具体用法?PHP WC_Product_Variation怎么用?PHP WC_Product_Variation使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了WC_Product_Variation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: test_varation_save_attributes
function test_varation_save_attributes()
{
// Create a variable product with a color attribute.
$product = new WC_Product_Variable();
$attribute = new WC_Product_Attribute();
$attribute->set_id(0);
$attribute->set_name('color');
$attribute->set_options(explode(WC_DELIMITER, 'green | red'));
$attribute->set_visible(true);
$attribute->set_variation(true);
$product->set_attributes(array($attribute));
$product->save();
// Create a new variation with the color 'green'.
$variation = new WC_Product_Variation();
$variation->set_parent_id($product->get_id());
$variation->set_attributes(array('color' => 'green'));
$variation->set_status('private');
$variation->save();
// Now update some value unrelated to attributes.
$variation = wc_get_product($variation->get_id());
$variation->set_status('publish');
$variation->save();
// Load up the updated variation and verify that the saved state is correct.
$loaded_variation = wc_get_product($variation->get_id());
$this->assertEquals('publish', $loaded_variation->get_status('edit'));
$_attribute = $loaded_variation->get_attributes('edit');
$this->assertEquals('green', $_attribute['color']);
}
示例2: add_sponsorship_project_to_cart
function add_sponsorship_project_to_cart()
{
global $post;
$args = array('post_type' => 'product_variation', 'post_status' => array('private', 'publish'), 'numberposts' => -1, 'orderby' => 'id', 'order' => 'asc', 'post_parent' => $post->ID);
$levels = get_posts($args);
do_action('woocommerce_before_add_to_cart_button');
?>
<div class="sp-levels">
<?php
foreach ($levels as $level) {
$level_product = new WC_Product_Variation($level->ID);
$level_data = get_post_custom($level->ID);
?>
<form id="level-<?php
echo $level->ID;
?>
-form" enctype="multipart/form-data" method="post" class="cart" action="<?php
echo $level_product->add_to_cart_url();
?>
">
<a class="sp-level" rel="<?php
echo $level->ID;
?>
">
<div class="sp-level-title">
<?php
echo get_the_title($level->ID);
?>
<div class="sp-level-amount">
<strong>$<?php
echo isset($level_data['_price'][0]) ? $level_data['_price'][0] : 0;
?>
</strong>
</div>
</div>
<div class="sp-level-description">
<?php
echo $level->post_content;
?>
</div>
</a>
</form>
<?php
}
?>
</div>
<script>
jQuery('.sp-level').click(function() {
var levelId = jQuery(this).attr('rel');
jQuery('#level-' + levelId + '-form').submit();
return false;
})
</script>
<?php
do_action('woocommerce_after_add_to_cart_button');
}
示例3: display_price_in_variation_option_name
function display_price_in_variation_option_name($term)
{
global $wpdb, $product;
$term_temp = $term;
$term = strtolower($term);
$term = str_replace(' ', '-', $term);
$result = $wpdb->get_col("SELECT slug FROM {$wpdb->prefix}terms WHERE name = '{$term}'");
$term_slug = !empty($result) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id\nFROM {$wpdb->prefix}postmeta AS postmeta\nLEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )\nWHERE postmeta.meta_key LIKE 'attribute_%'\nAND postmeta.meta_value = '{$term_slug}'\nAND products.post_parent = {$product->id}";
$variation_id = $wpdb->get_col($query);
$parent = wp_get_post_parent_id($variation_id[0]);
if ($parent > 0) {
$_product = new WC_Product_Variation($variation_id[0]);
$testVariable = $_product->get_variation_attributes();
$itemPrice = strip_tags(woocommerce_price($_product->get_price()));
$getPrice = $_product->get_price();
$itemPriceInt = (int) $getPrice;
$term = $term_temp;
//this is where you can actually customize how the price is displayed
if ($itemPriceInt > 0) {
return $term . ' (' . $itemPrice . ' incl. GST)';
} else {
return $term . ' (' . $itemPrice . ')';
}
}
return $term;
}
示例4: format_wc_variation_for_square_api
/**
* Convert a WC Product or Variation into a Square ItemVariation
* See: https://docs.connect.squareup.com/api/connect/v1/#datatype-itemvariation
*
* @param WC_Product|WC_Product_Variation $variation
* @param bool $include_inventory
* @return array Formatted as a Square ItemVariation
*/
public static function format_wc_variation_for_square_api($variation, $include_inventory = false)
{
$formatted = array('name' => null, 'pricing_type' => null, 'price_money' => null, 'sku' => null, 'track_inventory' => null, 'inventory_alert_type' => null, 'inventory_alert_threshold' => null, 'user_data' => null);
if ($variation instanceof WC_Product) {
$formatted['name'] = __('Regular', 'woocommerce-square');
$formatted['price_money'] = array('currency_code' => apply_filters('woocommerce_square_currency', get_woocommerce_currency()), 'amount' => $variation->get_display_price() * 100);
$formatted['sku'] = $variation->get_sku();
if ($include_inventory && $variation->managing_stock()) {
$formatted['track_inventory'] = true;
}
}
if ($variation instanceof WC_Product_Variation) {
$formatted['name'] = implode(', ', $variation->get_variation_attributes());
}
return array_filter($formatted);
}
示例5: create_vars
public function create_vars()
{
global $product;
if (!is_product() || !$product->is_type('variable')) {
return;
}
$att_data_hook = get_option('mp_wc_vdopp_data_hook');
// Hook data
$att_dom_sel = get_option('mp_wc_vdopp_dom_selector');
// DOM Selector
$att_data_sel = get_option('mp_wc_vdopp_data_selector');
// Data Selector
$att_before_size = apply_filters('mp_wc_vdopp_before_size', rtrim(get_option('mp_wc_vdopp_before_size'))) . ' ';
$att_before_weight = apply_filters('mp_wc_vdopp_before_weight', rtrim(get_option('mp_wc_vdopp_before_weight'))) . ' ';
$att_after_size = apply_filters('mp_wc_vdopp_after_size', ' ' . ltrim(get_option('mp_wc_vdopp_after_size')));
$att_after_weight = apply_filters('mp_wc_vdopp_after_weight', ' ' . ltrim(get_option('mp_wc_vdopp_after_weight')));
$children = $product->get_children($args = '', $output = OBJECT);
$i = 0;
foreach ($children as $value) {
$product_variatons = new WC_Product_Variation($value);
if ($product_variatons->exists() && $product_variatons->variation_is_visible()) {
$variations = $product_variatons->get_variation_attributes();
foreach ($variations as $key => $variation) {
$this->variations[$i][$key] = $variation;
}
$weight = $product_variatons->get_weight();
if ($weight) {
$weight .= get_option('woocommerce_weight_unit');
}
$this->variations[$i]['weight'] = $weight;
$this->variations[$i]['dimensions'] = str_replace(' ', '', $product_variatons->get_dimensions());
$i++;
}
}
$this->variations = wp_json_encode($this->variations);
$params = array('variations' => $this->variations, 'att_data_hook' => $att_data_hook, 'att_dom_sel' => $att_dom_sel, 'att_data_sel' => $att_data_sel, 'att_before_size' => $att_before_size, 'att_before_weight' => $att_before_weight, 'att_after_size' => $att_after_size, 'att_after_weight' => $att_after_weight, 'num_variations' => count($variations));
// enqueue the script
wp_enqueue_script('mp_wc_variation_details');
wp_localize_script('mp_wc_variation_details', 'mp_wc_variations', $params);
}
示例6: alert_box_function
function alert_box_function()
{
$child_id = $_POST['child_id'];
$display_stock_alert_form = 'false';
if ($child_id && !empty($child_id)) {
$child_obj = new WC_Product_Variation($child_id);
$dc_settings = get_dc_plugin_settings();
$stock_quantity = get_post_meta($child_id, '_stock', true);
$manage_stock = get_post_meta($child_id, '_manage_stock', true);
if (isset($stock_quantity) && $manage_stock == 'yes') {
if ($stock_quantity <= 0) {
if ($child_obj->backorders_allowed()) {
if (isset($dc_settings['is_enable_backorders']) && $dc_settings['is_enable_backorders'] == 'Enable') {
$display_stock_alert_form = 'true';
}
} else {
$display_stock_alert_form = 'true';
}
}
}
}
echo $display_stock_alert_form;
die;
}
示例7: foreach
<table class="uni-wishlist-table" cellspacing="0">
<thead>
<tr>
<th class="product-name"> </th>
<th class="product-price"> </th>
<th class="product-add-to-cart"> </th>
<th class="product-remove"> </th>
</tr>
</thead>
<tbody>
<?php
$i = 1;
foreach ($aUsersWishlist as $iProductId => $aWshlistItemData) {
if ($aWshlistItemData['type'] == 'variable' && !empty($aWshlistItemData['vid'])) {
foreach ($aWshlistItemData['vid'] as $iVariableProductId) {
$oProduct = new WC_Product_Variation($iVariableProductId, $iProductId);
if ($oProduct->exists()) {
include UniWishlist()->plugin_path() . '/includes/views/item-variation-table-row.php';
}
}
} else {
$oProduct = new WC_Product($iProductId);
if ($oProduct->exists()) {
include UniWishlist()->plugin_path() . '/includes/views/item-table-row.php';
}
}
$i++;
}
?>
</tbody>
</table>
示例8: WC_Product_Variation
* @author WooThemes
* @package WooCommerce/Templates
* @version 1.6.4
*/
if (!defined('ABSPATH')) {
exit;
// Exit if accessed directly
}
global $post, $product;
$sale_percent = 0;
if ($product->is_on_sale() && $product->product_type != 'grouped') {
if ($product->product_type == 'variable') {
$available_variations = $product->get_available_variations();
for ($i = 0; $i < count($available_variations); ++$i) {
$variation_id = $available_variations[$i]['variation_id'];
$variable_product1 = new WC_Product_Variation($variation_id);
$regular_price = $variable_product1->get_regular_price();
$sales_price = $variable_product1->get_sale_price();
$price = $variable_product1->get_price();
if ($sales_price != $regular_price && $sales_price == $price) {
$percentage = round(($regular_price - $sales_price) / $regular_price * 100, 1);
if ($percentage > $sale_percent) {
$sale_percent = $percentage;
}
}
}
} else {
$sale_percent = round(($product->get_regular_price() - $product->get_sale_price()) / $product->get_regular_price() * 100, 1);
}
}
?>
示例9: filter_woocommerce_add_to_cart_validation
/**
*
*/
public function filter_woocommerce_add_to_cart_validation($valid, $product_id, $quantity, $variation_id = '', $variations = '')
{
global $woocommerce;
$product = wc_get_product($product_id);
if ($product->product_type === "variable") {
$deductornot = get_post_meta($variation_id, '_deductornot', true);
$deductamount = get_post_meta($variation_id, '_deductamount', true);
$getvarclass = new WC_Product_Variation($variation_id);
//reset($array);
$aatrs = $getvarclass->get_variation_attributes();
foreach ($aatrs as $key => $value) {
$slug = $value;
$cat = str_replace('attribute_', '', $key);
}
$titlevaria = get_term_by('slug', $slug, $cat);
$backorder = get_post_meta($product->post->ID, '_backorders', true);
$string = WC_Cart::get_item_data($cart_item, $flat);
//var_dump($string);
if ($backorder == 'no') {
if ($deductornot == "yes") {
$currentstock = $product->get_stock_quantity();
$reduceamount = intval($quantity) * intval($deductamount);
$currentavail = intval($currentstock / $deductamount);
if ($reduceamount > $currentstock) {
$valid = false;
wc_add_notice('' . __('You that goes over our availble stock amount.', 'woocommerce') . __('We have: ', 'woocommerce') . $currentavail . ' ' . $product->post->post_title . ' ' . $titlevaria->name . '\'s ' . __(' available.', 'woocommerce'), 'error');
return $valid;
} else {
$valid = true;
return $valid;
}
} else {
return true;
}
}
}
return true;
}
示例10: save_variations_data
/**
* Save variations.
*
* @throws WC_REST_Exception REST API exceptions.
* @param WC_Product $product Product instance.
* @param WP_REST_Request $request Request data.
* @param bool $single_variation True if saving only a single variation.
* @return bool
*/
protected function save_variations_data($product, $request, $single_variation = false)
{
global $wpdb;
if ($single_variation) {
$variations = array($request);
} else {
$variations = $request['variations'];
}
foreach ($variations as $menu_order => $data) {
$variation_id = isset($data['id']) ? absint($data['id']) : 0;
$variation = new WC_Product_Variation($variation_id);
// Create initial name and status.
if (!$variation->get_slug()) {
/* translators: 1: variation id 2: product name */
$variation->set_name(sprintf(__('Variation #%1$s of %2$s', 'woocommerce'), $variation->get_id(), $product->get_name()));
$variation->set_status(isset($data['visible']) && false === $data['visible'] ? 'private' : 'publish');
}
// Parent ID.
$variation->set_parent_id($product->get_id());
// Menu order.
$variation->set_menu_order($menu_order);
// Status.
if (isset($data['visible'])) {
$variation->set_status(false === $data['visible'] ? 'private' : 'publish');
}
// SKU.
if (isset($data['sku'])) {
$variation->set_sku(wc_clean($data['sku']));
}
// Thumbnail.
if (isset($data['image']) && is_array($data['image'])) {
$image = $data['image'];
$image = current($image);
if (is_array($image)) {
$image['position'] = 0;
}
$variation = $this->save_product_images($variation, array($image));
}
// Virtual variation.
if (isset($data['virtual'])) {
$variation->set_virtual($data['virtual']);
}
// Downloadable variation.
if (isset($data['downloadable'])) {
$variation->set_downloadable($data['downloadable']);
}
// Downloads.
if ($variation->get_downloadable()) {
// Downloadable files.
if (isset($data['downloads']) && is_array($data['downloads'])) {
$variation = $this->save_downloadable_files($variation, $data['downloads']);
}
// Download limit.
if (isset($data['download_limit'])) {
$variation->set_download_limit($data['download_limit']);
}
// Download expiry.
if (isset($data['download_expiry'])) {
$variation->set_download_expiry($data['download_expiry']);
}
}
// Shipping data.
$variation = $this->save_product_shipping_data($variation, $data);
// Stock handling.
if (isset($data['manage_stock'])) {
$variation->set_manage_stock($data['manage_stock']);
}
if (isset($data['in_stock'])) {
$variation->set_stock_status(true === $data['in_stock'] ? 'instock' : 'outofstock');
}
if (isset($data['backorders'])) {
$variation->set_backorders($data['backorders']);
}
if ($variation->get_manage_stock()) {
if (isset($data['stock_quantity'])) {
$variation->set_stock_quantity($data['stock_quantity']);
} elseif (isset($data['inventory_delta'])) {
$stock_quantity = wc_stock_amount($variation->get_stock_amount());
$stock_quantity += wc_stock_amount($data['inventory_delta']);
$variation->set_stock_quantity($stock_quantity);
}
} else {
$variation->set_backorders('no');
$variation->set_stock_quantity('');
}
// Regular Price.
if (isset($data['regular_price'])) {
$variation->set_regular_price($data['regular_price']);
}
// Sale Price.
if (isset($data['sale_price'])) {
//.........这里部分代码省略.........
示例11: bulk_order_variation_search
function bulk_order_variation_search()
{
// Query for suggestions
$term = $_REQUEST['term'];
$excluded_products = array();
$excluded_products = apply_filters('wc_bulk_order_excluded_products', $excluded_products);
$included_products = array();
$included_products = apply_filters('wc_bulk_order_included_products', $included_products);
if (empty($term)) {
die;
}
$products1 = array('post_type' => array('product_variation'), 'post_status' => array('publish'), 'post_parent' => $term, 'fields' => 'ids', 'post__not_in' => $excluded_products, 'post__in' => $included_products, 'suppress_filters' => false, 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false, 'cache_results' => false);
$products = get_posts($products1);
// JSON encode and echo
// Initialise suggestions array
global $post, $woocommerce, $product;
$suggestions = '';
foreach ($products as $prod) {
$post_type = get_post_type($prod);
if ('product_variation' == $post_type) {
$product = new WC_Product_Variation($prod);
$parent = wc_get_product($prod);
$id = $product->variation_id;
$price = number_format((double) $product->price, 2, '.', '');
$price_html = $product->get_price_html();
if (preg_match('/<ins>(.*?)<\\/ins>/', $price_html)) {
preg_match('/<ins>(.*?)<\\/ins>/', $price_html, $matches);
$price_html = $matches[1];
}
$price_html = strip_tags($price_html);
$price = $price_html;
$price = apply_filters('wc_bulk_order_form_price', $price, $product);
$sku = $product->get_sku();
$title = '';
$attributes = $product->get_variation_attributes();
$img = apply_filters('woocommerce_placeholder_img_src', WC_Bulk_Order_Form_Compatibility::WC()->plugin_url() . '/assets/images/placeholder.png');
foreach ($attributes as $name => $value) {
$name = str_ireplace("attribute_", "", $name);
$terms = get_the_terms($product->id, $name);
foreach ($terms as $term) {
if (strtolower($term->name) == $value) {
$value = $term->name;
}
}
$attr_name = $name;
$attr_value = str_replace('-', ' ', $value);
if ($this->options['attribute_style'] === 'true') {
$title .= $attr_value . ' ';
} else {
if (strstr($attr_name, 'pa_')) {
$atts = get_the_terms($parent->id, $attr_name);
$attr_name_clean = WC_Bulk_Order_Form_Compatibility::wc_attribute_label($attr_name);
} else {
$np = explode("-", str_replace("attribute_", "", $attr_name));
$attr_name_clean = ucwords(implode(" ", $np));
}
$attr_name_clean = str_replace("attribute_pa_", "", $attr_name_clean);
$attr_name_clean = str_replace("Attribute_pa_", "", $attr_name_clean);
$title .= ' ' . $attr_name_clean . ": " . $attr_value;
}
$title = html_entity_decode($title, ENT_COMPAT, 'UTF-8');
}
$parent_image = wp_get_attachment_image_src(get_post_thumbnail_id($id), 'thumbnail');
$parent_image = $parent_image[0];
$img = wp_get_attachment_image_src(get_post_thumbnail_id($parent->id), 'thumbnail');
$img = $img[0];
if (!empty($img)) {
$img = $img;
} elseif (!empty($parent_image)) {
$img = $parent_image;
} else {
$img = apply_filters('woocommerce_placeholder_img_src', WC_Bulk_Order_Form_Compatibility::WC()->plugin_url() . '/assets/images/placeholder.png');
}
}
if (!empty($id)) {
$symbol = get_woocommerce_currency_symbol();
$symbol = html_entity_decode($symbol, ENT_COMPAT, 'UTF-8');
$price = html_entity_decode($price, ENT_COMPAT, 'UTF-8');
// Initialise suggestion array
$suggestion = array();
$variation_switch_data = isset($this->options['variation_search_format']) ? $this->options['variation_search_format'] : '1';
switch ($variation_switch_data) {
case 1:
if (!empty($sku)) {
$label = $sku . ' - ' . $title . ' - ' . $price;
} else {
$label = $title . ' - ' . $price;
}
break;
case 2:
if (!empty($sku)) {
$label = $title . ' - ' . $price . ' - ' . $sku;
} else {
$label = $title . ' - ' . $price;
}
break;
case 3:
$label = $title . ' - ' . $price;
break;
case 4:
//.........这里部分代码省略.........
示例12: product_page_pricing_table
/**
* Maybe display pricing table or anchor to display pricing table in modal
*
* @access public
* @return void
*/
public function product_page_pricing_table()
{
if ($this->opt['settings']['display_table'] == 'hide' && (!isset($this->opt['settings']['display_offers']) || $this->opt['settings']['display_offers'] == 'hide')) {
return;
}
global $product;
if (!$product) {
return;
}
// Load required classes
require_once RP_WCDPD_PLUGIN_PATH . 'includes/classes/Pricing.php';
$selected_rule = null;
// Iterate over pricing rules and use the first one that has this product in conditions (or does not have if condition "not in list")
if (isset($this->opt['pricing']['sets']) && count($this->opt['pricing']['sets'])) {
foreach ($this->opt['pricing']['sets'] as $rule_key => $rule) {
if ($rule['method'] == 'quantity' && ($validated_rule = RP_WCDPD_Pricing::validate_rule($rule))) {
if ($validated_rule['selection_method'] == 'all' && $this->user_matches_rule($validated_rule)) {
$selected_rule = $validated_rule;
break;
}
if ($validated_rule['selection_method'] == 'categories_include' && count(array_intersect($this->get_product_categories($product->id), $validated_rule['categories'])) > 0 && $this->user_matches_rule($validated_rule)) {
$selected_rule = $validated_rule;
break;
}
if ($validated_rule['selection_method'] == 'categories_exclude' && count(array_intersect($this->get_product_categories($product->id), $validated_rule['categories'])) == 0 && $this->user_matches_rule($validated_rule)) {
$selected_rule = $validated_rule;
break;
}
if ($validated_rule['selection_method'] == 'products_include' && in_array($product->id, $validated_rule['products']) && $this->user_matches_rule($validated_rule)) {
$selected_rule = $validated_rule;
break;
}
if ($validated_rule['selection_method'] == 'products_exclude' && !in_array($product->id, $validated_rule['products']) && $this->user_matches_rule($validated_rule)) {
$selected_rule = $validated_rule;
break;
}
}
}
}
if (is_array($selected_rule)) {
// Quantity
if ($selected_rule['method'] == 'quantity' && in_array($this->opt['settings']['display_table'], array('modal', 'inline')) && isset($selected_rule['pricing'])) {
if ($product->product_type == 'variable') {
$product_variations = $product->get_available_variations();
}
// For variable products only - check if prices differ for different variations
$multiprice_variable_product = false;
if ($product->product_type == 'variable' && !empty($product_variations)) {
$last_product_variation = array_slice($product_variations, -1);
$last_product_variation_object = new WC_Product_Variable($last_product_variation[0]['variation_id']);
$last_product_variation_price = $last_product_variation_object->get_price();
foreach ($product_variations as $variation) {
$variation_object = new WC_Product_Variable($variation['variation_id']);
if ($variation_object->get_price() != $last_product_variation_price) {
$multiprice_variable_product = true;
}
}
}
if ($multiprice_variable_product) {
$variation_table_data = array();
foreach ($product_variations as $variation) {
$variation_product = new WC_Product_Variation($variation['variation_id']);
$variation_table_data[$variation['variation_id']] = $this->pricing_table_calculate_adjusted_prices($selected_rule['pricing'], $variation_product->get_price());
}
require_once RP_WCDPD_PLUGIN_PATH . 'includes/views/frontend/table-variable.php';
} else {
if ($product->product_type == 'variable' && !empty($product_variations)) {
$variation_product = new WC_Product_Variation($last_product_variation[0]['variation_id']);
$table_data = $this->pricing_table_calculate_adjusted_prices($selected_rule['pricing'], $variation_product->get_price());
} else {
$table_data = $this->pricing_table_calculate_adjusted_prices($selected_rule['pricing'], $product->get_price());
}
require_once RP_WCDPD_PLUGIN_PATH . 'includes/views/frontend/table-' . $this->opt['settings']['display_table'] . '-' . $this->opt['settings']['pricing_table_style'] . '.php';
}
}
}
}
示例13: post_process
//.........这里部分代码省略.........
if ($im != '' && strpos($im, "mshots/v1") === false && w_isset_def($performance_options['image_cropping'], 0) == 1) {
if (w_isset_def($image_settings['image_transparency'], 1) == 1) {
$bfi_params = array('width' => $image_settings['image_width'], 'height' => $image_settings['image_height'], 'crop' => true);
} else {
$bfi_params = array('width' => $image_settings['image_width'], 'height' => $image_settings['image_height'], 'crop' => true, 'color' => wpdreams_rgb2hex($image_settings['image_bg_color']));
}
$r->image = bfi_thumb($im, $bfi_params);
} else {
$r->image = $im;
}
}
if (!isset($searchData['titlefield']) || $searchData['titlefield'] == "0" || is_array($searchData['titlefield'])) {
$r->title = get_the_title($r->id);
} else {
if ($searchData['titlefield'] == "1") {
if (strlen($r->excerpt) >= 200) {
$r->title = wd_substr_at_word($r->excerpt, 200);
} else {
$r->title = $r->excerpt;
}
} else {
$mykey_values = get_post_custom_values($searchData['titlefield'], $r->id);
if (isset($mykey_values[0])) {
$r->title = $mykey_values[0];
} else {
$r->title = get_the_title($r->id);
}
}
}
//remove the search shortcodes properly
add_shortcode('wpdreams_ajaxsearchpro', array($this, 'return_empty_string'));
add_shortcode('wpdreams_ajaxsearchlite', array($this, 'return_empty_string'));
if (!isset($searchData['striptagsexclude'])) {
$searchData['striptagsexclude'] = "<a><span>";
}
if (!isset($searchData['descriptionfield']) || $searchData['descriptionfield'] == "0" || is_array($searchData['descriptionfield'])) {
if (w_isset_def($searchData['strip_shortcodes'], 0) == 1) {
$r->content = strip_shortcodes($r->content);
}
if (function_exists('qtrans_getLanguage')) {
$r->content = apply_filters('the_content', $r->content);
}
$_content = strip_tags($r->content);
} else {
if ($searchData['descriptionfield'] == "1") {
$_content = strip_tags($r->excerpt);
} else {
if ($searchData['descriptionfield'] == "2") {
$_content = strip_tags(get_the_title($r->id));
} else {
$mykey_values = get_post_custom_values($searchData['descriptionfield'], $r->id);
if (isset($mykey_values[0])) {
$_content = strip_tags($mykey_values[0]);
} else {
$_content = strip_tags($r->content);
}
}
}
}
if ($_content == "" && $r->content != '') {
$_content = $r->content;
}
if ($_content != "") {
$_content = str_replace('[wpdreams_ajaxsearchlite]', "", $_content);
}
if ($_content != "") {
$_content = apply_filters('the_content', $_content);
}
// Remove styles and scripts
$_content = preg_replace(array('#<script(.*?)>(.*?)</script>#is', '#<style(.*?)>(.*?)</style>#is'), '', $_content);
$_content = strip_tags($_content);
// Get the words from around the search phrase, or just the description
if (w_isset_def($searchData['description_context'], 1) == 1 && count($_s) > 0) {
$_content = $this->context_find($_content, $_s[0], floor($searchData['descriptionlength'] / 6), $searchData['descriptionlength']);
} else {
if ($_content != '' && strlen($_content) > $searchData['descriptionlength']) {
$_content = wd_substr_at_word($_content, $searchData['descriptionlength']) . "...";
}
}
$_content = wd_closetags($_content);
$r->content = $_content;
// -------------------------- Woocommerce Fixes -----------------------------
// A trick to fix the url
if ($r->post_type == 'product_variation' && class_exists('WC_Product_Variation')) {
$r->title = preg_replace("/(Variation) \\#(\\d+) (of)/si", '', $r->title);
$wc_prod_var_o = new WC_Product_Variation($r->id);
$r->link = $wc_prod_var_o->get_permalink();
}
// --------------------------------------------------------------------------
$r->title = apply_filters('asl_result_title_after_prostproc', $r->title, $r->id);
$r->content = apply_filters('asl_result_content_after_prostproc', $r->content, $r->id);
$r->image = apply_filters('asl_result_image_after_prostproc', $r->image, $r->id);
$r->author = apply_filters('asl_result_author_after_prostproc', $r->author, $r->id);
$r->date = apply_filters('asl_result_date_after_prostproc', $r->date, $r->id);
}
/* !Images, title, desc */
//var_dump($pageposts); die();
$this->results = $pageposts;
return $pageposts;
}
示例14: post_process
/**
* Post-processes the results
*
* @return array of results
*/
protected function post_process()
{
$pageposts = is_array($this->results) ? $this->results : array();
$options = $this->options;
$searchId = $this->searchId;
$searchData = $this->searchData;
$s = $this->s;
$_s = $this->_s;
// No post processing is needed on non-ajax search
if (isset($options['non_ajax_search'])) {
$this->results = $pageposts;
return $pageposts;
}
if (is_multisite()) {
$home_url = network_home_url();
} else {
$home_url = home_url();
}
foreach ($pageposts as $k => $v) {
$r =& $pageposts[$k];
$r->title = w_isset_def($r->title, null);
$r->content = w_isset_def($r->content, null);
$r->image = w_isset_def($r->image, null);
$r->author = w_isset_def($r->author, null);
$r->date = w_isset_def($r->date, null);
}
aspDebug::start('--searchContent-posptrocess');
/* Images, title, desc */
foreach ($pageposts as $k => $v) {
// Let's simplify things
$r =& $pageposts[$k];
if (isset($options['switch_on_preprocess']) && is_multisite()) {
switch_to_blog($r->blogid);
}
$r = apply_filters('asp_result_before_prostproc', $r, $searchId);
$r->title = apply_filters('asp_result_title_before_prostproc', $r->title, $r->id, $searchId);
$r->content = apply_filters('asp_result_content_before_prostproc', $r->content, $r->id, $searchId);
$r->image = apply_filters('asp_result_image_before_prostproc', $r->image, $r->id, $searchId);
$r->author = apply_filters('asp_result_author_before_prostproc', $r->author, $r->id, $searchId);
$r->date = apply_filters('asp_result_date_before_prostproc', $r->date, $r->id, $searchId);
$r->link = get_permalink($v->id);
// ---- URL FIX for WooCommerce product variations
if ($r->post_type == 'product_variation' && class_exists('WC_Product_Variation')) {
$wc_prod_var_o = new WC_Product_Variation($r->id);
$r->link = $wc_prod_var_o->get_permalink();
}
$caching_options = w_false_def(get_option('asp_caching'), get_option('asp_caching_def'));
$use_bfi = w_isset_def($caching_options['use_bfi_thumb'], 1);
$image_settings = $searchData['image_options'];
if ($image_settings['show_images'] != 0) {
if ($image_settings['image_cropping'] == 0) {
// Use the BFI parser, but no caching
$im = $this->getBFIimage($r);
if ($im != '') {
$r->image = $im;
}
} else {
if ($use_bfi == 0) {
$im = $this->getCachedImage($r);
if ($im != '') {
$r->image = $im;
}
} else {
$im = $this->getBFIimage($r);
if ($im != '' && strpos($im, "mshots/v1") === false) {
if (w_isset_def($image_settings['image_transparency'], 1) == 1) {
$bfi_params = array('width' => $image_settings['image_width'], 'height' => $image_settings['image_height'], 'crop' => true);
} else {
$bfi_params = array('width' => $image_settings['image_width'], 'height' => $image_settings['image_height'], 'crop' => true, 'color' => wpdreams_rgb2hex($image_settings['image_bg_color']));
}
$r->image = bfi_thumb($im, $bfi_params);
} else {
$r->image = $im;
}
}
}
}
if (!isset($searchData['titlefield']) || $searchData['titlefield'] == "0" || is_array($searchData['titlefield'])) {
$r->title = get_the_title($r->id);
} else {
if ($searchData['titlefield'] == "1") {
if (strlen($r->excerpt) >= 200) {
$r->title = wd_substr_at_word($r->excerpt, 200);
} else {
$r->title = $r->excerpt;
}
} else {
$mykey_values = get_post_custom_values($searchData['titlefield'], $r->id);
if (isset($mykey_values[0])) {
$r->title = $mykey_values[0];
} else {
$r->title = get_the_title($r->id);
}
}
}
//.........这里部分代码省略.........
示例15: wcsCart
function wcsCart()
{
global $woocommerce;
$message = '';
if (isset($_POST['remove_coupn']) && $_POST['remove_coupn'] != '') {
$coupon_code = $_POST['remove_coupn'];
// Coupon is no longer valid, based on date. Remove it.
if ($woocommerce->cart->has_discount(sanitize_text_field($coupon_code))) {
if ($woocommerce->cart->remove_coupons(sanitize_text_field($coupon_code))) {
$woocommerce->clear_messages();
}
$message = $coupon_code . " code remove succesfully";
// Manually recalculate totals. If you do not do this, a refresh is required before user will see updated totals when discount is removed.
$woocommerce->cart->calculate_totals();
}
}
if (isset($_POST['add_coupon']) && $_POST['add_coupon'] != '') {
$coupon_code = $_POST['add_coupon'];
//if ( $woocommerce->cart->has_discount( $coupon_code ) ) return;
$woocommerce->cart->add_discount($coupon_code);
if ($woocommerce->cart->applied_coupons) {
$message = $coupon_code . " code successfully applied";
} else {
$message = $coupon_code . " code does not exist";
}
$woocommerce->cart->calculate_totals();
}
if ($woocommerce->cart) {
$items_in_cart = $woocommerce->cart->cart_contents_count;
$prod_ids_in_cart = array();
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
$product_id = apply_filters('woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key);
array_push($prod_ids_in_cart, $product_id);
}
$test01 = 0;
$test02 = 0;
$test03 = 0;
$test04 = 0;
if (in_array("566", $prod_ids_in_cart)) {
$test01 = 1;
}
if (in_array("568", $prod_ids_in_cart)) {
$test02 = 1;
}
if (in_array("570", $prod_ids_in_cart)) {
$test03 = 1;
}
if (in_array("572", $prod_ids_in_cart)) {
$test04 = 1;
}
$how_many_tests_in_cart = $test01 + $test02 + $test03 + $test04;
if (in_array("574", $prod_ids_in_cart)) {
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
if ($cart_item['product_id'] == 574) {
$woocommerce->cart->set_quantity($cart_item_key, 0);
}
}
if ($how_many_tests_in_cart == 0) {
}
if ($how_many_tests_in_cart == 1) {
$woocommerce->cart->add_to_cart('574', '1', '719', '1');
$_product = new WC_Product_Variation(719);
$price_urgent = $_product->get_price_html();
}
if ($how_many_tests_in_cart == 2) {
$woocommerce->cart->add_to_cart('574', '1', '720', '2');
$_product = new WC_Product_Variation(720);
$price_urgent = $_product->get_price_html();
}
if ($how_many_tests_in_cart == 3) {
$woocommerce->cart->add_to_cart('574', '1', '721', '3');
$_product = new WC_Product_Variation(721);
$price_urgent = $_product->get_price_html();
}
if ($how_many_tests_in_cart == 4) {
$woocommerce->cart->add_to_cart('574', '1', '722', '4');
$_product = new WC_Product_Variation(722);
$price_urgent = $_product->get_price_html();
}
}
// end if there is processing in cart
/*$product = new WC_Product( $product_id );
if($product_id != '574') {
$price = $product->price;
}*/
$html = '';
if ($woocommerce->cart->get_cart_contents_count() > 0) {
//$html = '<ul class="cart_list product_list_widget">';
foreach ($woocommerce->cart->get_cart() as $cart_item_key => $cart_item) {
$product_id = apply_filters('woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key);
$product = new WC_Product($product_id);
if ($product_id == 574) {
$price = $price_urgent;
} else {
$price = $product->get_price_html();
}
$product = new WC_Product($product_id);
//$html .='<li class="mini_cart_item" id="product_id_'.$product_id.'"><a href="javascript:void(0);">'.$product->post->post_title.' ( '. $price.') </a> </li>';
$html .= '<tr id="product_id_' . $product_id . '"><td class="item_name">' . $product->post->post_title . '</td><td class="item_price">' . $price . '</td></tr>';
}
//.........这里部分代码省略.........