本文整理汇总了PHP中wc_get_text_attributes函数的典型用法代码示例。如果您正苦于以下问题:PHP wc_get_text_attributes函数的具体用法?PHP wc_get_text_attributes怎么用?PHP wc_get_text_attributes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wc_get_text_attributes函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: read_variation_attributes
/**
* Loads an array of attributes used for variations, as well as their possible values.
*
* @param WC_Product
*/
private function read_variation_attributes(&$product)
{
global $wpdb;
$variation_attributes = array();
$attributes = $product->get_attributes();
$child_ids = $product->get_children();
if (!empty($child_ids) && !empty($attributes)) {
foreach ($attributes as $attribute) {
if (empty($attribute['is_variation'])) {
continue;
}
// Get possible values for this attribute, for only visible variations.
$values = array_unique($wpdb->get_col($wpdb->prepare("SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s AND post_id IN (" . implode(',', array_map('esc_sql', $child_ids)) . ")", wc_variation_attribute_name($attribute['name']))));
// Empty value indicates that all options for given attribute are available.
if (in_array('', $values) || empty($values)) {
$values = $attribute['is_taxonomy'] ? wc_get_object_terms($product->get_id(), $attribute['name'], 'slug') : wc_get_text_attributes($attribute['value']);
// Get custom attributes (non taxonomy) as defined.
} elseif (!$attribute['is_taxonomy']) {
$text_attributes = wc_get_text_attributes($attribute['value']);
$assigned_text_attributes = $values;
$values = array();
// Pre 2.4 handling where 'slugs' were saved instead of the full text attribute
if (version_compare(get_post_meta($product->get_id(), '_product_version', true), '2.4.0', '<')) {
$assigned_text_attributes = array_map('sanitize_title', $assigned_text_attributes);
foreach ($text_attributes as $text_attribute) {
if (in_array(sanitize_title($text_attribute), $assigned_text_attributes)) {
$values[] = $text_attribute;
}
}
} else {
foreach ($text_attributes as $text_attribute) {
if (in_array($text_attribute, $assigned_text_attributes)) {
$values[] = $text_attribute;
}
}
}
}
$variation_attributes[$attribute['name']] = array_unique($values);
}
}
$product->set_variation_attributes($variation_attributes);
}
示例2: add_to_cart_action
/**
* Add to cart action
*
* Checks for a valid request, does validation (via hooks) and then redirects if valid.
*
* @param bool $url (default: false)
*/
public static function add_to_cart_action($url = false)
{
if (empty($_REQUEST['add-to-cart']) || !is_numeric($_REQUEST['add-to-cart'])) {
return;
}
$product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_REQUEST['add-to-cart']));
$was_added_to_cart = false;
$added_to_cart = array();
$adding_to_cart = wc_get_product($product_id);
$add_to_cart_handler = apply_filters('woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart);
// Check if the product is published
if ('publish' !== $adding_to_cart->post->post_status) {
wc_add_notice(__('Sorry, this product is unavailable.', 'woocommerce'), 'error');
return;
}
// Variable product handling
if ('variable' === $add_to_cart_handler) {
$variation_id = empty($_REQUEST['variation_id']) ? '' : absint($_REQUEST['variation_id']);
$quantity = empty($_REQUEST['quantity']) ? 1 : wc_stock_amount($_REQUEST['quantity']);
$missing_attributes = array();
$variations = array();
$attributes = $adding_to_cart->get_attributes();
$variation = wc_get_product($variation_id);
// Verify all attributes
foreach ($attributes as $attribute) {
if (!$attribute['is_variation']) {
continue;
}
$taxonomy = 'attribute_' . sanitize_title($attribute['name']);
if (isset($_REQUEST[$taxonomy])) {
// Get value from post data
if ($attribute['is_taxonomy']) {
// Don't use wc_clean as it destroys sanitized characters
$value = sanitize_title(stripslashes($_REQUEST[$taxonomy]));
} else {
$value = wc_clean(stripslashes($_REQUEST[$taxonomy]));
}
// Get valid value from variation
$valid_value = $variation->variation_data[$taxonomy];
// Allow if valid
if ('' === $valid_value || $valid_value === $value) {
// Pre 2.4 handling where 'slugs' were saved instead of the full text attribute
if (!$attribute['is_taxonomy']) {
if ($value === sanitize_title($value) && version_compare(get_post_meta($product_id, '_product_version', true), '2.4.0', '<')) {
$text_attributes = wc_get_text_attributes($attribute['value']);
foreach ($text_attributes as $text_attribute) {
if (sanitize_title($text_attribute) === $value) {
$value = $text_attribute;
break;
}
}
}
}
$variations[$taxonomy] = $value;
continue;
}
} else {
$missing_attributes[] = wc_attribute_label($attribute['name']);
}
}
if ($missing_attributes) {
wc_add_notice(sprintf(_n('%s is a required field', '%s are required fields', sizeof($missing_attributes), 'woocommerce'), wc_format_list_of_items($missing_attributes)), 'error');
return;
} elseif (empty($variation_id)) {
wc_add_notice(__('Please choose product options…', 'woocommerce'), 'error');
return;
} else {
// Add to cart validation
$passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations);
if ($passed_validation) {
if (WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations) !== false) {
wc_add_to_cart_message($product_id);
$was_added_to_cart = true;
$added_to_cart[] = $product_id;
}
}
}
// Grouped Products
} elseif ('grouped' === $add_to_cart_handler) {
if (!empty($_REQUEST['quantity']) && is_array($_REQUEST['quantity'])) {
$quantity_set = false;
foreach ($_REQUEST['quantity'] as $item => $quantity) {
if ($quantity <= 0) {
continue;
}
$quantity_set = true;
// Add to cart validation
$passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $item, $quantity);
if ($passed_validation) {
if (WC()->cart->add_to_cart($item, $quantity) !== false) {
$was_added_to_cart = true;
$added_to_cart[] = $item;
}
//.........这里部分代码省略.........
示例3: save
//.........这里部分代码省略.........
// Select based attributes - Format values (posted values are slugs)
if (is_array($attribute_values[$i])) {
$values = array_map('sanitize_title', $attribute_values[$i]);
// Text based attributes - Posted values are term names - don't change to slugs
} else {
$values = array_map('stripslashes', array_map('strip_tags', explode(WC_DELIMITER, $attribute_values[$i])));
}
// Remove empty items in the array
$values = array_filter($values, 'strlen');
} else {
$values = array();
}
// Update post terms
if (taxonomy_exists($attribute_names[$i])) {
foreach ($values as $key => $value) {
$term = get_term_by('name', trim($value), $attribute_names[$i]);
if ($term) {
$values[$key] = intval($term->term_id);
} else {
$term = wp_insert_term(trim($value), $attribute_names[$i]);
if (isset($term->term_id)) {
$values[$key] = intval($term->term_id);
}
}
}
wp_set_object_terms($post_id, $values, $attribute_names[$i]);
}
if (!empty($values)) {
// Add attribute to array, but don't set values
$attributes[sanitize_title($attribute_names[$i])] = array('name' => wc_clean($attribute_names[$i]), 'value' => '', 'position' => $attribute_position[$i], 'is_visible' => $is_visible, 'is_variation' => $is_variation, 'is_taxonomy' => $is_taxonomy);
}
} elseif (isset($attribute_values[$i])) {
// Text based, separate by pipe
$values = implode(' ' . WC_DELIMITER . ' ', array_map('wc_clean', wc_get_text_attributes($attribute_values[$i])));
// Custom attribute - Add attribute to array and set the values
$attributes[sanitize_title($attribute_names[$i])] = array('name' => wc_clean($attribute_names[$i]), 'value' => $values, 'position' => $attribute_position[$i], 'is_visible' => $is_visible, 'is_variation' => $is_variation, 'is_taxonomy' => $is_taxonomy);
}
}
}
if (!function_exists('attributes_cmp')) {
function attributes_cmp($a, $b)
{
if ($a['position'] == $b['position']) {
return 0;
}
return $a['position'] < $b['position'] ? -1 : 1;
}
}
uasort($attributes, 'attributes_cmp');
update_post_meta($post_id, '_product_attributes', $attributes);
// Sales and prices
if (in_array($product_type, array('variable', 'grouped'))) {
// Variable and grouped products have no prices
update_post_meta($post_id, '_regular_price', '');
update_post_meta($post_id, '_sale_price', '');
update_post_meta($post_id, '_sale_price_dates_from', '');
update_post_meta($post_id, '_sale_price_dates_to', '');
update_post_meta($post_id, '_price', '');
} else {
$date_from = isset($_POST['_sale_price_dates_from']) ? wc_clean($_POST['_sale_price_dates_from']) : '';
$date_to = isset($_POST['_sale_price_dates_to']) ? wc_clean($_POST['_sale_price_dates_to']) : '';
// Dates
if ($date_from) {
update_post_meta($post_id, '_sale_price_dates_from', strtotime($date_from));
} else {
update_post_meta($post_id, '_sale_price_dates_from', '');
示例4: load_variations
/**
* Load variations via AJAX
*/
public static function load_variations()
{
ob_start();
check_ajax_referer('load-variations', 'security');
// Check permissions again and make sure we have what we need
if (!current_user_can('edit_products') || empty($_POST['product_id']) || empty($_POST['attributes'])) {
die(-1);
}
global $post;
$product_id = absint($_POST['product_id']);
$post = get_post($product_id);
// Set $post global so its available like within the admin screens
$per_page = !empty($_POST['per_page']) ? absint($_POST['per_page']) : 10;
$page = !empty($_POST['page']) ? absint($_POST['page']) : 1;
// Get attributes
$attributes = array();
foreach ($_POST['attributes'] as $key => $value) {
$attributes[wc_clean($key)] = array_map('wc_clean', $value);
}
// Get tax classes
$tax_classes = WC_Tax::get_tax_classes();
$tax_class_options = array();
$tax_class_options[''] = __('Standard', 'woocommerce');
if (!empty($tax_classes)) {
foreach ($tax_classes as $class) {
$tax_class_options[sanitize_title($class)] = esc_attr($class);
}
}
// Set backorder options
$backorder_options = array('no' => __('Do not allow', 'woocommerce'), 'notify' => __('Allow, but notify customer', 'woocommerce'), 'yes' => __('Allow', 'woocommerce'));
// set stock status options
$stock_status_options = array('instock' => __('In stock', 'woocommerce'), 'outofstock' => __('Out of stock', 'woocommerce'));
$parent_data = array('id' => $product_id, 'attributes' => $attributes, 'tax_class_options' => $tax_class_options, 'sku' => get_post_meta($product_id, '_sku', true), 'weight' => wc_format_localized_decimal(get_post_meta($product_id, '_weight', true)), 'length' => wc_format_localized_decimal(get_post_meta($product_id, '_length', true)), 'width' => wc_format_localized_decimal(get_post_meta($product_id, '_width', true)), 'height' => wc_format_localized_decimal(get_post_meta($product_id, '_height', true)), 'tax_class' => get_post_meta($product_id, '_tax_class', true), 'backorder_options' => $backorder_options, 'stock_status_options' => $stock_status_options);
if (!$parent_data['weight']) {
$parent_data['weight'] = wc_format_localized_decimal(0);
}
if (!$parent_data['length']) {
$parent_data['length'] = wc_format_localized_decimal(0);
}
if (!$parent_data['width']) {
$parent_data['width'] = wc_format_localized_decimal(0);
}
if (!$parent_data['height']) {
$parent_data['height'] = wc_format_localized_decimal(0);
}
// Get variations
$args = array('post_type' => 'product_variation', 'post_status' => array('private', 'publish'), 'posts_per_page' => $per_page, 'paged' => $page, 'orderby' => 'ID', 'order' => 'DESC', 'post_parent' => $product_id);
$variations = get_posts($args);
$loop = 0;
if ($variations) {
foreach ($variations as $variation) {
$variation_id = absint($variation->ID);
$variation_meta = get_post_meta($variation_id);
$variation_data = array();
$shipping_classes = get_the_terms($variation_id, 'product_shipping_class');
$variation_fields = array('_sku' => '', '_stock' => '', '_regular_price' => '', '_sale_price' => '', '_weight' => '', '_length' => '', '_width' => '', '_height' => '', '_download_limit' => '', '_download_expiry' => '', '_downloadable_files' => '', '_downloadable' => '', '_virtual' => '', '_thumbnail_id' => '', '_sale_price_dates_from' => '', '_sale_price_dates_to' => '', '_manage_stock' => '', '_stock_status' => '', '_backorders' => null, '_tax_class' => null, '_variation_description' => '');
foreach ($variation_fields as $field => $value) {
$variation_data[$field] = isset($variation_meta[$field][0]) ? maybe_unserialize($variation_meta[$field][0]) : $value;
}
// Add the variation attributes
foreach ($variation_meta as $key => $value) {
if (0 !== strpos($key, 'attribute_')) {
continue;
}
/**
* Pre 2.4 handling where 'slugs' were saved instead of the full text attribute.
* Attempt to get full version of the text attribute from the parent.
*/
if (sanitize_title($value[0]) === $value[0] && version_compare(get_post_meta($product_id, '_product_version', true), '2.4.0', '<')) {
foreach ($attributes as $attribute) {
if ($key !== 'attribute_' . sanitize_title($attribute['name'])) {
continue;
}
$text_attributes = wc_get_text_attributes($attribute['value']);
foreach ($text_attributes as $text_attribute) {
if (sanitize_title($text_attribute) === $value[0]) {
$value[0] = $text_attribute;
}
}
}
}
$variation_data[$key] = $value[0];
}
// Formatting
$variation_data['_regular_price'] = wc_format_localized_price($variation_data['_regular_price']);
$variation_data['_sale_price'] = wc_format_localized_price($variation_data['_sale_price']);
$variation_data['_weight'] = wc_format_localized_decimal($variation_data['_weight']);
$variation_data['_length'] = wc_format_localized_decimal($variation_data['_length']);
$variation_data['_width'] = wc_format_localized_decimal($variation_data['_width']);
$variation_data['_height'] = wc_format_localized_decimal($variation_data['_height']);
$variation_data['_thumbnail_id'] = absint($variation_data['_thumbnail_id']);
$variation_data['image'] = $variation_data['_thumbnail_id'] ? wp_get_attachment_thumb_url($variation_data['_thumbnail_id']) : '';
$variation_data['shipping_class'] = $shipping_classes && !is_wp_error($shipping_classes) ? current($shipping_classes)->term_id : '';
// Stock BW compat
if ('' !== $variation_data['_stock']) {
$variation_data['_manage_stock'] = 'yes';
}
//.........这里部分代码省略.........
示例5: wc_get_product_variation_attributes
/**
* Get attibutes/data for an individual variation from the database and maintain it's integrity.
* @since 2.4.0
* @param int $variation_id
* @return array
*/
function wc_get_product_variation_attributes($variation_id)
{
// Build variation data from meta
$all_meta = get_post_meta($variation_id);
$parent_id = wp_get_post_parent_id($variation_id);
$parent_attributes = array_filter((array) get_post_meta($parent_id, '_product_attributes', true));
$found_parent_attributes = array();
$variation_attributes = array();
// Compare to parent variable product attributes and ensure they match
foreach ($parent_attributes as $attribute_name => $options) {
if (!empty($options['is_variation'])) {
$attribute = 'attribute_' . sanitize_title($attribute_name);
$found_parent_attributes[] = $attribute;
if (!array_key_exists($attribute, $variation_attributes)) {
$variation_attributes[$attribute] = '';
// Add it - 'any' will be asumed
}
}
}
// Get the variation attributes from meta
foreach ($all_meta as $name => $value) {
// Only look at valid attribute meta, and also compare variation level attributes and remove any which do not exist at parent level
if (0 !== strpos($name, 'attribute_') || !in_array($name, $found_parent_attributes)) {
unset($variation_attributes[$name]);
continue;
}
/**
* Pre 2.4 handling where 'slugs' were saved instead of the full text attribute.
* Attempt to get full version of the text attribute from the parent.
*/
if (sanitize_title($value[0]) === $value[0] && version_compare(get_post_meta($parent_id, '_product_version', true), '2.4.0', '<')) {
foreach ($parent_attributes as $attribute) {
if ($name !== 'attribute_' . sanitize_title($attribute['name'])) {
continue;
}
$text_attributes = wc_get_text_attributes($attribute['value']);
foreach ($text_attributes as $text_attribute) {
if (sanitize_title($text_attribute) === $value[0]) {
$value[0] = $text_attribute;
break;
}
}
}
}
$variation_attributes[$name] = $value[0];
}
return $variation_attributes;
}
示例6: sync_attributes
/**
* Sync the variable product's attributes with the variations.
*/
public static function sync_attributes($product_id, $children = false)
{
if (!$children) {
$children = get_posts(array('post_parent' => $product_id, 'posts_per_page' => -1, 'post_type' => 'product_variation', 'fields' => 'ids', 'post_status' => 'any'));
}
/**
* Pre 2.4 handling where 'slugs' were saved instead of the full text attribute.
* Attempt to get full version of the text attribute from the parent and UPDATE meta.
*/
if (version_compare(get_post_meta($product_id, '_product_version', true), '2.4.0', '<')) {
$parent_attributes = array_filter((array) get_post_meta($product_id, '_product_attributes', true));
foreach ($children as $child_id) {
$all_meta = get_post_meta($child_id);
foreach ($all_meta as $name => $value) {
if (0 !== strpos($name, 'attribute_')) {
continue;
}
if (sanitize_title($value[0]) === $value[0]) {
foreach ($parent_attributes as $attribute) {
if ($name !== 'attribute_' . sanitize_title($attribute['name'])) {
continue;
}
$text_attributes = wc_get_text_attributes($attribute['value']);
foreach ($text_attributes as $text_attribute) {
if (sanitize_title($text_attribute) === $value[0]) {
update_post_meta($child_id, $name, $text_attribute);
break;
}
}
}
}
}
}
}
}
示例7: isset
// Only deal with attributes that are variations
if (!$attribute['is_variation'] || 'false' === $attribute['is_variation']) {
continue;
}
// Get current value for variation (if set)
$variation_selected_value = isset($variation_data['attribute_' . sanitize_title($attribute['name'])]) ? $variation_data['attribute_' . sanitize_title($attribute['name'])] : '';
// Name will be something like attribute_pa_color
echo '<select name="attribute_' . sanitize_title($attribute['name']) . '[' . $loop . ']"><option value="">' . __('Any', 'woocommerce') . ' ' . esc_html(wc_attribute_label($attribute['name'])) . '…</option>';
// Get terms for attribute taxonomy or value if its a custom attribute
if ($attribute['is_taxonomy']) {
$post_terms = wp_get_post_terms($parent_data['id'], $attribute['name']);
foreach ($post_terms as $term) {
echo '<option ' . selected($variation_selected_value, $term->slug, false) . ' value="' . esc_attr($term->slug) . '">' . esc_html(apply_filters('woocommerce_variation_option_name', $term->name)) . '</option>';
}
} else {
$options = wc_get_text_attributes($attribute['value']);
foreach ($options as $option) {
$selected = sanitize_title($variation_selected_value) === $variation_selected_value ? selected($variation_selected_value, sanitize_title($option), false) : selected($variation_selected_value, $option, false);
echo '<option ' . $selected . ' value="' . esc_attr($option) . '">' . esc_html(apply_filters('woocommerce_variation_option_name', $option)) . '</option>';
}
}
echo '</select>';
}
?>
<input type="hidden" name="variable_post_id[<?php
echo $loop;
?>
]" value="<?php
echo esc_attr($variation_id);
?>
" />
示例8: read_attributes
/**
* Read attributes from post meta.
*
* @param WC_Product
* @since 2.7.0
*/
protected function read_attributes(&$product)
{
$meta_values = maybe_unserialize(get_post_meta($product->get_id(), '_product_attributes', true));
if ($meta_values) {
$attributes = array();
foreach ($meta_values as $meta_value) {
if (!empty($meta_value['is_taxonomy'])) {
if (!taxonomy_exists($meta_value['name'])) {
continue;
}
$options = wc_get_object_terms($product->get_id(), $meta_value['name'], 'term_id');
} else {
$options = wc_get_text_attributes($meta_value['value']);
}
$attribute = new WC_Product_Attribute();
$attribute->set_id(wc_attribute_taxonomy_id_by_name($meta_value['name']));
$attribute->set_name($meta_value['name']);
$attribute->set_options($options);
$attribute->set_position($meta_value['position']);
$attribute->set_visible($meta_value['is_visible']);
$attribute->set_variation($meta_value['is_variation']);
$attributes[] = $attribute;
}
$product->set_attributes($attributes);
}
}
示例9: save_attributes
/**
* Save attributes via ajax.
*/
public static function save_attributes()
{
check_ajax_referer('save-attributes', 'security');
if (!current_user_can('edit_products')) {
die(-1);
}
// Get post data
parse_str($_POST['data'], $data);
$post_id = absint($_POST['post_id']);
// Save Attributes
$attributes = array();
if (isset($data['attribute_names'])) {
$attribute_names = array_map('stripslashes', $data['attribute_names']);
$attribute_values = isset($data['attribute_values']) ? $data['attribute_values'] : array();
if (isset($data['attribute_visibility'])) {
$attribute_visibility = $data['attribute_visibility'];
}
if (isset($data['attribute_variation'])) {
$attribute_variation = $data['attribute_variation'];
}
$attribute_is_taxonomy = $data['attribute_is_taxonomy'];
$attribute_position = $data['attribute_position'];
$attribute_names_max_key = max(array_keys($attribute_names));
for ($i = 0; $i <= $attribute_names_max_key; $i++) {
if (empty($attribute_names[$i])) {
continue;
}
$is_visible = isset($attribute_visibility[$i]) ? 1 : 0;
$is_variation = isset($attribute_variation[$i]) ? 1 : 0;
$is_taxonomy = $attribute_is_taxonomy[$i] ? 1 : 0;
if ($is_taxonomy) {
if (isset($attribute_values[$i])) {
// Select based attributes - Format values (posted values are slugs)
if (is_array($attribute_values[$i])) {
$values = array_map('sanitize_title', $attribute_values[$i]);
// Text based attributes - Posted values are term names, wp_set_object_terms wants ids or slugs.
} else {
$values = array();
$raw_values = array_map('wc_sanitize_term_text_based', explode(WC_DELIMITER, $attribute_values[$i]));
foreach ($raw_values as $value) {
$term = get_term_by('name', $value, $attribute_names[$i]);
if (!$term) {
$term = wp_insert_term($value, $attribute_names[$i]);
if ($term && !is_wp_error($term)) {
$values[] = $term['term_id'];
}
} else {
$values[] = $term->term_id;
}
}
}
// Remove empty items in the array
$values = array_filter($values, 'strlen');
} else {
$values = array();
}
// Update post terms
if (taxonomy_exists($attribute_names[$i])) {
wp_set_object_terms($post_id, $values, $attribute_names[$i]);
}
if ($values) {
// Add attribute to array, but don't set values
$attributes[sanitize_title($attribute_names[$i])] = array('name' => wc_clean($attribute_names[$i]), 'value' => '', 'position' => $attribute_position[$i], 'is_visible' => $is_visible, 'is_variation' => $is_variation, 'is_taxonomy' => $is_taxonomy);
}
} elseif (isset($attribute_values[$i])) {
// Text based, possibly separated by pipes (WC_DELIMITER). Preserve line breaks in non-variation attributes.
$values = $is_variation ? wc_clean($attribute_values[$i]) : implode("\n", array_map('wc_clean', explode("\n", $attribute_values[$i])));
$values = implode(' ' . WC_DELIMITER . ' ', wc_get_text_attributes($values));
// Custom attribute - Add attribute to array and set the values
$attributes[sanitize_title($attribute_names[$i])] = array('name' => wc_clean($attribute_names[$i]), 'value' => $values, 'position' => $attribute_position[$i], 'is_visible' => $is_visible, 'is_variation' => $is_variation, 'is_taxonomy' => $is_taxonomy);
}
}
}
if (!function_exists('attributes_cmp')) {
function attributes_cmp($a, $b)
{
if ($a['position'] == $b['position']) {
return 0;
}
return $a['position'] < $b['position'] ? -1 : 1;
}
}
uasort($attributes, 'attributes_cmp');
update_post_meta($post_id, '_product_attributes', $attributes);
die;
}
示例10: sync_attributes
/**
* Sync the variable product's attributes with the variations.
*/
public static function sync_attributes($product, $children = false)
{
if (!is_a($product, 'WC_Product')) {
$product = wc_get_product($product);
}
/**
* Pre 2.4 handling where 'slugs' were saved instead of the full text attribute.
* Attempt to get full version of the text attribute from the parent and UPDATE meta.
*/
if (version_compare(get_post_meta($product->get_id(), '_product_version', true), '2.4.0', '<')) {
$parent_attributes = array_filter((array) get_post_meta($product->get_id(), '_product_attributes', true));
if (!$children) {
$children = $product->get_children('edit');
}
foreach ($children as $child_id) {
$all_meta = get_post_meta($child_id);
foreach ($all_meta as $name => $value) {
if (0 !== strpos($name, 'attribute_')) {
continue;
}
if (sanitize_title($value[0]) === $value[0]) {
foreach ($parent_attributes as $attribute) {
if ('attribute_' . sanitize_title($attribute['name']) !== $name) {
continue;
}
$text_attributes = wc_get_text_attributes($attribute['value']);
foreach ($text_attributes as $text_attribute) {
if (sanitize_title($text_attribute) === $value[0]) {
update_post_meta($child_id, $name, $text_attribute);
break;
}
}
}
}
}
}
}
}
示例11: get_formatted_variation_attributes
/**
* Get formatted variation data with WC < 2.4 back compat and proper formatting of text-based attribute names.
*
* @return string
*/
public function get_formatted_variation_attributes($flat = false)
{
$variation_data = $this->get_variation_attributes();
$attributes = $this->parent->get_attributes();
$description = array();
$return = '';
if (is_array($variation_data)) {
if (!$flat) {
$return = '<dl class="variation">';
}
foreach ($attributes as $attribute) {
// Only deal with attributes that are variations
if (!$attribute['is_variation']) {
continue;
}
$variation_selected_value = isset($variation_data['attribute_' . sanitize_title($attribute['name'])]) ? $variation_data['attribute_' . sanitize_title($attribute['name'])] : '';
$description_name = esc_html(wc_attribute_label($attribute['name']));
$description_value = __('Any', 'woocommerce');
// Get terms for attribute taxonomy or value if its a custom attribute
if ($attribute['is_taxonomy']) {
$post_terms = wp_get_post_terms($this->id, $attribute['name']);
foreach ($post_terms as $term) {
if ($variation_selected_value === $term->slug) {
$description_value = esc_html(apply_filters('woocommerce_variation_option_name', $term->name));
}
}
} else {
$options = wc_get_text_attributes($attribute['value']);
foreach ($options as $option) {
if (sanitize_title($variation_selected_value) === $variation_selected_value) {
if ($variation_selected_value !== sanitize_title($option)) {
continue;
}
} else {
if ($variation_selected_value !== $option) {
continue;
}
}
$description_value = esc_html(apply_filters('woocommerce_variation_option_name', $option));
}
}
if ($flat) {
$description[] = $description_name . ': ' . rawurldecode($description_value);
} else {
$description[] = '<dt>' . $description_name . ':</dt><dd>' . rawurldecode($description_value) . '</dd>';
}
}
if ($flat) {
$return .= implode(', ', $description);
} else {
$return .= implode('', $description);
}
if (!$flat) {
$return .= '</dl>';
}
}
return $return;
}
示例12: get_variation_attributes
/**
* Return an array of attributes used for variations, as well as their possible values.
*
* @return array of attributes and their available values
*/
public function get_variation_attributes()
{
$variation_attributes = array();
if (!$this->has_child()) {
return $variation_attributes;
}
$attributes = $this->get_attributes();
foreach ($attributes as $attribute) {
if (!$attribute['is_variation']) {
continue;
}
$values = array();
$attribute_field_name = 'attribute_' . sanitize_title($attribute['name']);
// Get used values from children variations
foreach ($this->get_children() as $child_id) {
$variation = $this->get_child($child_id);
if (!empty($variation->variation_id)) {
if (!$variation->variation_is_visible()) {
continue;
// Disabled or hidden
}
$child_variation_attributes = $variation->get_variation_attributes();
if (isset($child_variation_attributes[$attribute_field_name])) {
$values[] = $child_variation_attributes[$attribute_field_name];
}
}
}
// empty value indicates that all options for given attribute are available
if (in_array('', $values)) {
$values = $attribute['is_taxonomy'] ? wp_get_post_terms($this->id, $attribute['name'], array('fields' => 'slugs')) : wc_get_text_attributes($attribute['value']);
// Order custom attributes (non taxonomy) as defined
} elseif (!$attribute['is_taxonomy']) {
$text_attributes = wc_get_text_attributes($attribute['value']);
$assigned_text_attributes = $values;
$values = array();
// Pre 2.4 handling where 'slugs' were saved instead of the full text attribute
if ($assigned_text_attributes === array_map('sanitize_title', $assigned_text_attributes) && version_compare(get_post_meta($this->id, '_product_version', true), '2.4.0', '<')) {
$assigned_text_attributes = array_map('sanitize_title', $assigned_text_attributes);
foreach ($text_attributes as $text_attribute) {
if (in_array(sanitize_title($text_attribute), $assigned_text_attributes)) {
$values[] = $text_attribute;
}
}
} else {
foreach ($text_attributes as $text_attribute) {
if (in_array($text_attribute, $assigned_text_attributes)) {
$values[] = $text_attribute;
}
}
}
}
$variation_attributes[$attribute['name']] = array_unique($values);
}
return $variation_attributes;
}
示例13: process_matrix_submission
public function process_matrix_submission()
{
global $woocommerce;
$items = $_POST['order_info'];
$product_id = $_POST['product_id'];
$adding_to_cart = wc_get_product($product_id);
$added_count = 0;
$failed_count = 0;
$success_message = '';
$error_message = '';
foreach ($items as $item) {
$q = floatval($item['quantity']) ? floatval($item['quantity']) : 0;
if ($q) {
$variation_id = empty($item['variation_id']) ? '' : absint($item['variation_id']);
$missing_attributes = array();
//For validation, since 2.4
$variations = array();
// Only allow integer variation ID - if its not set, redirect to the product page
if (empty($variation_id)) {
//wc_add_notice( __( 'Please choose product options…', 'woocommerce' ), 'error' );
$failed_count++;
continue;
}
$attributes = $adding_to_cart->get_attributes();
$variation = wc_get_product($variation_id);
// Verify all attributes
foreach ($attributes as $attribute) {
if (!$attribute['is_variation']) {
continue;
}
$taxonomy = 'attribute_' . sanitize_title($attribute['name']);
if (isset($item['variation_data'][$taxonomy])) {
// Get value from post data
if ($attribute['is_taxonomy']) {
// Don't use wc_clean as it destroys sanitized characters
$value = sanitize_title(stripslashes($item['variation_data'][$taxonomy]));
} else {
$value = wc_clean(stripslashes($item['variation_data'][$taxonomy]));
}
// Get valid value from variation
$valid_value = $variation->variation_data[$taxonomy];
// Allow if valid
if ('' === $valid_value || $valid_value === $value) {
// Pre 2.4 handling where 'slugs' were saved instead of the full text attribute
if (!$attribute['is_taxonomy']) {
if ($value === sanitize_title($value) && version_compare(get_post_meta($product_id, '_product_version', true), '2.4.0', '<')) {
$text_attributes = wc_get_text_attributes($attribute['value']);
foreach ($text_attributes as $text_attribute) {
if (sanitize_title($text_attribute) === $value) {
$value = $text_attribute;
break;
}
}
}
}
$variations[$taxonomy] = $value;
continue;
}
} else {
$missing_attributes[] = wc_attribute_label($attribute['name']);
}
}
if (empty($missing_attributes)) {
// Add to cart validation
$passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $q, $variation_id, $variations);
if ($passed_validation) {
$added = WC()->cart->add_to_cart($product_id, $q, $variation_id, $variations);
}
} else {
$failed_count++;
continue;
}
if ($added) {
$added_count++;
} else {
$failed_count++;
}
}
}
if ($added_count) {
woocommerce_bulk_variations_add_to_cart_message($added_count);
}
if ($failed_count) {
WC_Bulk_Variations_Compatibility::wc_add_error(sprintf(__('Unable to add %s to the cart. Please check your quantities and make sure the item is available and in stock', 'wc_bulk_variations'), $failed_count));
}
if (!$added_count && !$failed_count) {
WC_Bulk_Variations_Compatibility::wc_add_error(__('No product quantities entered.', 'wc_bulk_variations'));
}
// If we added the products to the cart we can now do a redirect, otherwise just continue loading the page to show errors
if ($failed_count === 0 && wc_notice_count('error') === 0) {
// If has custom URL redirect there
if ($url = apply_filters('woocommerce_add_to_cart_redirect', false)) {
wp_safe_redirect($url);
exit;
} elseif (get_option('woocommerce_cart_redirect_after_add') === 'yes') {
wp_safe_redirect(WC()->cart->get_cart_url());
exit;
}
}
}
示例14: prepare_attributes
/**
* Prepare attributes for save.
* @return array
*/
public static function prepare_attributes($data = false)
{
$attributes = array();
if (!$data) {
$data = $_POST;
}
if (isset($data['attribute_names'], $data['attribute_values'])) {
$attribute_names = $data['attribute_names'];
$attribute_values = $data['attribute_values'];
$attribute_visibility = isset($data['attribute_visibility']) ? $data['attribute_visibility'] : array();
$attribute_variation = isset($data['attribute_variation']) ? $data['attribute_variation'] : array();
$attribute_position = $data['attribute_position'];
$attribute_names_max_key = max(array_keys($attribute_names));
for ($i = 0; $i <= $attribute_names_max_key; $i++) {
if (empty($attribute_names[$i]) || !isset($attribute_values[$i])) {
continue;
}
$attribute_name = wc_clean($attribute_names[$i]);
$attribute_id = wc_attribute_taxonomy_id_by_name($attribute_name);
$options = isset($attribute_values[$i]) ? $attribute_values[$i] : '';
if (is_array($options)) {
// Term ids sent as array.
$options = wp_parse_id_list($options);
} else {
// Terms or text sent in textarea.
$options = 0 < $attribute_id ? wc_sanitize_textarea(wc_sanitize_term_text_based($options)) : wc_sanitize_textarea($options);
$options = wc_get_text_attributes($options);
}
$attribute = new WC_Product_Attribute();
$attribute->set_id($attribute_id);
$attribute->set_name($attribute_name);
$attribute->set_options($options);
$attribute->set_position($attribute_position[$i]);
$attribute->set_visible(isset($attribute_visibility[$i]));
$attribute->set_variation(isset($attribute_variation[$i]));
$attributes[] = $attribute;
}
}
return $attributes;
}
示例15: __get
/**
* Get method returns variation meta data if set, otherwise in most cases the data from the parent.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
if (in_array($key, array_keys($this->variation_level_meta_data))) {
$value = get_post_meta($this->variation_id, '_' . $key, true);
if ('' === $value) {
$value = $this->variation_level_meta_data[$key];
}
} elseif (in_array($key, array_keys($this->variation_inherited_meta_data))) {
$value = metadata_exists('post', $this->variation_id, '_' . $key) ? get_post_meta($this->variation_id, '_' . $key, true) : get_post_meta($this->id, '_' . $key, true);
// Handle meta data keys which can be empty at variation level to cause inheritance
if ('' === $value && in_array($key, array('sku', 'weight', 'length', 'width', 'height'))) {
$value = get_post_meta($this->id, '_' . $key, true);
}
if ('' === $value) {
$value = $this->variation_inherited_meta_data[$key];
}
} elseif ('variation_data' === $key) {
$all_meta = get_post_meta($this->variation_id);
// The variation data array
$this->variation_data = array();
// Get the variation attributes from meta
foreach ($all_meta as $name => $value) {
if (0 !== strpos($name, 'attribute_')) {
continue;
}
/**
* Pre 2.4 handling where 'slugs' were saved instead of the full text attribute.
* Attempt to get full version of the text attribute from the parent.
*/
if (sanitize_title($value[0]) === $value[0] && version_compare(get_post_meta($this->id, '_product_version', true), '2.4.0', '<')) {
$attributes = $this->parent->get_attributes();
foreach ($attributes as $attribute) {
if ($name !== 'attribute_' . sanitize_title($attribute['name'])) {
continue;
}
$text_attributes = wc_get_text_attributes($attribute['value']);
foreach ($text_attributes as $text_attribute) {
if (sanitize_title($text_attribute) === $value[0]) {
$value[0] = $text_attribute;
}
}
}
}
$this->variation_data[$name] = $value[0];
}
return $this->variation_data;
} elseif ('variation_has_stock' === $key) {
return $this->managing_stock();
} else {
$value = metadata_exists('post', $this->variation_id, '_' . $key) ? get_post_meta($this->variation_id, '_' . $key, true) : parent::__get($key);
}
return $value;
}