Merge pull request #12465 from woocommerce/crud-ajax-class

AJAX Class and other fixes
This commit is contained in:
Mike Jolley 2016-11-25 10:44:20 +00:00 committed by GitHub
commit f67ffe4d89
29 changed files with 473 additions and 620 deletions

View File

@ -125,10 +125,9 @@ jQuery( function ( $ ) {
} }
var data = { var data = {
user_id: user_id, user_id : user_id,
type_to_load: 'billing', action : 'woocommerce_get_customer_details',
action: 'woocommerce_get_customer_details', security: woocommerce_admin_meta_boxes.get_customer_details_nonce
security: woocommerce_admin_meta_boxes.get_customer_details_nonce
}; };
$( this ).closest( 'div.edit_address' ).block({ $( this ).closest( 'div.edit_address' ).block({
@ -144,9 +143,9 @@ jQuery( function ( $ ) {
data: data, data: data,
type: 'POST', type: 'POST',
success: function( response ) { success: function( response ) {
if ( response ) { if ( response && response.billing ) {
$.each( response, function( key, data ) { $.each( response.billing, function( key, data ) {
$( ':input#_' + key ).val( data ).change(); $( ':input#_billing_' + key ).val( data ).change();
}); });
} }
$( 'div.edit_address' ).unblock(); $( 'div.edit_address' ).unblock();
@ -169,7 +168,6 @@ jQuery( function ( $ ) {
var data = { var data = {
user_id: user_id, user_id: user_id,
type_to_load: 'shipping',
action: 'woocommerce_get_customer_details', action: 'woocommerce_get_customer_details',
security: woocommerce_admin_meta_boxes.get_customer_details_nonce security: woocommerce_admin_meta_boxes.get_customer_details_nonce
}; };
@ -187,9 +185,9 @@ jQuery( function ( $ ) {
data: data, data: data,
type: 'POST', type: 'POST',
success: function( response ) { success: function( response ) {
if ( response ) { if ( response && response.billing ) {
$.each( response, function( key, data ) { $.each( response.shipping, function( key, data ) {
$( ':input#_' + key ).val( data ).change(); $( ':input#_shipping_' + key ).val( data ).change();
}); });
} }
$( 'div.edit_address' ).unblock(); $( 'div.edit_address' ).unblock();

File diff suppressed because one or more lines are too long

View File

@ -460,18 +460,6 @@ abstract class WC_Abstract_Legacy_Product extends WC_Data {
return $post_data; return $post_data;
} }
/**
* Get the title of the post.
*
* @deprecated 2.7.0
* @return string
*/
public function get_title() {
wc_deprecated_function( 'WC_Product::get_title', '2.7', 'WC_Product::get_name' );
return apply_filters( 'woocommerce_product_title', $this->get_post_data() ? $this->get_post_data()->post_title : '', $this );
}
/** /**
* Get the parent of the post. * Get the parent of the post.
* *

View File

@ -1616,6 +1616,15 @@ class WC_Product extends WC_Abstract_Legacy_Product {
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
*/ */
/**
* Get the product's title. For products this is the product name.
*
* @return string
*/
public function get_title() {
return apply_filters( 'woocommerce_product_title', $this->get_name(), $this );
}
/** /**
* Product permalink. * Product permalink.
* @return string * @return string

View File

@ -24,7 +24,7 @@ if ( ! defined( 'ABSPATH' ) ) {
if ( ! $attribute->get_variation() ) { if ( ! $attribute->get_variation() ) {
continue; continue;
} }
$selected_value = isset( $attribute_values[ 'attribute_' . sanitize_title( $attribute->get_name() ) ] ) ? $attribute_values[ 'attribute_' . sanitize_title( $attribute->get_name() ) ] : ''; $selected_value = isset( $attribute_values[ sanitize_title( $attribute->get_name() ) ] ) ? $attribute_values[ sanitize_title( $attribute->get_name() ) ] : '';
?> ?>
<select name="attribute_<?php echo sanitize_title( $attribute->get_name() ) . "[{$loop}]"; ?>"> <select name="attribute_<?php echo sanitize_title( $attribute->get_name() ) . "[{$loop}]"; ?>">
<option value=""><?php <option value=""><?php
@ -115,15 +115,6 @@ if ( ! defined( 'ABSPATH' ) ) {
get_woocommerce_currency_symbol() get_woocommerce_currency_symbol()
); );
woocommerce_wp_text_input( array(
'id' => "variable_regular_price_{$loop}",
'name' => "variable_regular_price[{$loop}]",
'value' => wc_format_localized_price( $variation_object->get_regular_price( 'edit' ) ),
'label' => __( 'Regular price', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')',
'data_type' => 'price',
'wrapper_class' => 'form-row form-row-first',
) );
woocommerce_wp_text_input( array( woocommerce_wp_text_input( array(
'id' => "variable_sale_price{$loop}", 'id' => "variable_sale_price{$loop}",
'name' => "variable_sale_price[{$loop}]", 'name' => "variable_sale_price[{$loop}]",

File diff suppressed because it is too large Load Diff

View File

@ -537,12 +537,11 @@ class WC_Cart {
public function get_item_data( $cart_item, $flat = false ) { public function get_item_data( $cart_item, $flat = false ) {
$item_data = array(); $item_data = array();
// Variation data // Variation values are shown only if they are not found in the title as of 2.7.
// This is because variation titles display the attributes.
if ( $cart_item['data']->is_type( 'variation' ) && is_array( $cart_item['variation'] ) ) { if ( $cart_item['data']->is_type( 'variation' ) && is_array( $cart_item['variation'] ) ) {
foreach ( $cart_item['variation'] as $name => $value ) { foreach ( $cart_item['variation'] as $name => $value ) {
if ( '' === $value || stristr( $cart_item['data']->get_name(), $value ) ) {
if ( '' === $value ) {
continue; continue;
} }
@ -558,13 +557,8 @@ class WC_Cart {
// If this is a custom option slug, get the options name // If this is a custom option slug, get the options name
} else { } else {
$value = apply_filters( 'woocommerce_variation_option_name', $value ); $value = apply_filters( 'woocommerce_variation_option_name', $value );
$product_attributes = $cart_item['data']->get_attributes(); $label = wc_attribute_label( str_replace( 'attribute_', '', $name ), $cart_item['data'] );
if ( isset( $product_attributes[ str_replace( 'attribute_', '', $name ) ] ) ) {
$label = wc_attribute_label( $product_attributes[ str_replace( 'attribute_', '', $name ) ]['name'] );
} else {
$label = $name;
}
} }
$item_data[] = array( $item_data[] = array(

View File

@ -173,12 +173,4 @@ class WC_Order_Item_Coupon extends WC_Order_Item {
} }
return parent::offsetExists( $offset ); return parent::offsetExists( $offset );
} }
/**
* Internal meta keys we don't want exposed as part of meta_data.
* @return array()
*/
protected function get_internal_meta_keys() {
return array( 'discount_amount', 'discount_amount_tax' );
}
} }

View File

@ -234,12 +234,4 @@ class WC_Order_Item_Fee extends WC_Order_Item {
} }
return parent::offsetExists( $offset ); return parent::offsetExists( $offset );
} }
/**
* Internal meta keys we don't want exposed as part of meta_data.
* @return array()
*/
protected function get_internal_meta_keys() {
return array( '_tax_class', '_tax_status', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', '_line_tax_data' );
}
} }

View File

@ -450,13 +450,4 @@ class WC_Order_Item_Product extends WC_Order_Item {
} }
return parent::offsetExists( $offset ); return parent::offsetExists( $offset );
} }
/**
* Internal meta keys we don't want exposed as part of meta_data.
*
* @return array
*/
protected function get_internal_meta_keys() {
return array( '_product_id', '_variation_id', '_qty', '_tax_class', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', '_line_tax_data' );
}
} }

View File

@ -243,12 +243,4 @@ class WC_Order_Item_Shipping extends WC_Order_Item {
} }
return parent::offsetExists( $offset ); return parent::offsetExists( $offset );
} }
/**
* Internal meta keys we don't want exposed as part of meta_data.
* @return array()
*/
protected function get_internal_meta_keys() {
return array( 'method_id', 'cost', 'total_tax', 'taxes' );
}
} }

View File

@ -200,20 +200,6 @@ class WC_Order_Item_Tax extends WC_Order_Item {
return $this->get_prop( 'compound', $context ); return $this->get_prop( 'compound', $context );
} }
/*
|--------------------------------------------------------------------------
| Other
|--------------------------------------------------------------------------
*/
/**
* Internal meta keys we don't want exposed as part of meta_data.
* @return array()
*/
protected function get_internal_meta_keys() {
return array( 'rate_id', 'label', 'compound', 'tax_amount', 'shipping_tax_amount' );
}
/** /**
* Is this a compound tax rate? * Is this a compound tax rate?
* @return boolean * @return boolean

View File

@ -194,8 +194,8 @@ class WC_Order_Item extends WC_Data implements ArrayAccess {
continue; continue;
} }
$meta->key = rawurldecode( $meta->key ); $meta->key = rawurldecode( (string) $meta->key );
$meta->value = rawurldecode( $meta->value ); $meta->value = rawurldecode( (string) $meta->value );
$attribute_key = str_replace( 'attribute_', '', $meta->key ); $attribute_key = str_replace( 'attribute_', '', $meta->key );
$display_key = wc_attribute_label( $attribute_key, is_callable( array( $this, 'get_product' ) ) ? $this->get_product() : false ); $display_key = wc_attribute_label( $attribute_key, is_callable( array( $this, 'get_product' ) ) ? $this->get_product() : false );
$display_value = $meta->value; $display_value = $meta->value;

View File

@ -57,6 +57,7 @@ class WC_Product_Attribute implements ArrayAccess {
/** /**
* Gets terms from the stored options. * Gets terms from the stored options.
*
* @return array|null * @return array|null
*/ */
public function get_terms() { public function get_terms() {
@ -77,6 +78,28 @@ class WC_Product_Attribute implements ArrayAccess {
return $terms; return $terms;
} }
/**
* Gets slugs from the stored options, or just the string if text based.
*
* @return array
*/
public function get_slugs() {
if ( ! $this->is_taxonomy() || ! taxonomy_exists( $this->get_name() ) ) {
return $this->get_options();
}
foreach ( $this->get_options() as $option ) {
if ( is_int( $option ) ) {
$term = get_term_by( 'id', $option, $this->get_name() );
} else {
$term = get_term_by( 'name', $option, $this->get_name() );
}
if ( $term && ! is_wp_error( $term ) ) {
$terms[] = $term->slug;
}
}
return $terms;
}
/** /**
* Returns all data for this object. * Returns all data for this object.
* @return array * @return array

View File

@ -63,11 +63,15 @@ class WC_Product_Factory {
// Allow the overriding of the lookup in this function. Return the product type here. // Allow the overriding of the lookup in this function. Return the product type here.
$override = apply_filters( 'woocommerce_product_type_query', false, $product_id ); $override = apply_filters( 'woocommerce_product_type_query', false, $product_id );
if ( ! $override ) { if ( ! $override ) {
if ( 'product_variation' === get_post_type( $product_id ) ) { $post_type = get_post_type( $product_id );
if ( 'product_variation' === $post_type ) {
return 'variation'; return 'variation';
} elseif ( 'product' === $post_type ) {
$terms = get_the_terms( $product_id, 'product_type' );
return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple';
} else {
return false;
} }
$terms = get_the_terms( $product_id, 'product_type' );
return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple';
} else { } else {
return $override; return $override;
} }

View File

@ -247,6 +247,7 @@ class WC_Product_Variable extends WC_Product {
} }
return apply_filters( 'woocommerce_available_variation', array_merge( $variation->get_data(), array( return apply_filters( 'woocommerce_available_variation', array_merge( $variation->get_data(), array(
'attributes' => $variation->get_variation_attributes(),
'image' => wc_get_product_attachment_props( $variation->get_image_id() ), 'image' => wc_get_product_attachment_props( $variation->get_image_id() ),
'weight_html' => wc_format_weight( $variation->get_weight() ), 'weight_html' => wc_format_weight( $variation->get_weight() ),
'dimensions_html' => wc_format_dimensions( $variation->get_dimensions( false ) ), 'dimensions_html' => wc_format_dimensions( $variation->get_dimensions( false ) ),

View File

@ -65,12 +65,26 @@ class WC_Product_Variation extends WC_Product_Simple {
} }
/** /**
* Get variation attribute values. * Get the product's title. For variations this is the parent product name.
*
* @return string
*/
public function get_title() {
return apply_filters( 'woocommerce_product_title', $this->parent_data['title'], $this );
}
/**
* Get variation attribute values. Keys are prefixed with attribute_, as stored.
* *
* @return array of attributes and their values for this variation * @return array of attributes and their values for this variation
*/ */
public function get_variation_attributes() { public function get_variation_attributes() {
return $this->get_attributes(); $attributes = $this->get_attributes();
$variation_attributes = array();
foreach ( $attributes as $key => $value ) {
$variation_attributes[ 'attribute_' . $key ] = $value;
}
return $variation_attributes;
} }
/** /**
@ -87,9 +101,9 @@ class WC_Product_Variation extends WC_Product_Simple {
} elseif ( ! empty( $item_object['item_meta_array'] ) ) { } elseif ( ! empty( $item_object['item_meta_array'] ) ) {
$data_keys = array_map( 'wc_variation_attribute_name', wp_list_pluck( $item_object['item_meta_array'], 'key' ) ); $data_keys = array_map( 'wc_variation_attribute_name', wp_list_pluck( $item_object['item_meta_array'], 'key' ) );
$data_values = wp_list_pluck( $item_object['item_meta_array'], 'value' ); $data_values = wp_list_pluck( $item_object['item_meta_array'], 'value' );
$data = array_intersect_key( array_combine( $data_keys, $data_values ), $this->get_attributes() ); $data = array_intersect_key( array_combine( $data_keys, $data_values ), $this->get_variation_attributes() );
} else { } else {
$data = $this->get_attributes(); $data = $this->get_variation_attributes();
} }
return add_query_arg( array_map( 'urlencode', array_filter( $data ) ), $url ); return add_query_arg( array_map( 'urlencode', array_filter( $data ) ), $url );
@ -101,7 +115,7 @@ class WC_Product_Variation extends WC_Product_Simple {
* @return string * @return string
*/ */
public function add_to_cart_url() { public function add_to_cart_url() {
$variation_data = array_map( 'urlencode', $this->get_attributes() ); $variation_data = array_map( 'urlencode', $this->get_variation_attributes() );
$url = $this->is_purchasable() ? remove_query_arg( 'added-to-cart', add_query_arg( array( 'variation_id' => $this->get_id(), 'add-to-cart' => $this->get_parent_id() ), $this->get_permalink() ) ) : $this->get_permalink(); $url = $this->is_purchasable() ? remove_query_arg( 'added-to-cart', add_query_arg( array( 'variation_id' => $this->get_id(), 'add-to-cart' => $this->get_parent_id() ), $this->get_permalink() ) ) : $this->get_permalink();
return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this ); return apply_filters( 'woocommerce_product_add_to_cart_url', $url, $this );
} }
@ -295,9 +309,9 @@ class WC_Product_Variation extends WC_Product_Simple {
$attributes = array(); $attributes = array();
foreach ( $raw_attributes as $key => $value ) { foreach ( $raw_attributes as $key => $value ) {
// Add attribute prefix which meta gets stored with. // Remove attribute prefix which meta gets stored with.
if ( 0 !== strpos( $key, 'attribute_' ) ) { if ( 0 === strpos( $key, 'attribute_' ) ) {
$key = 'attribute_' . $key; $key = substr( $key, 10 );
} }
$attributes[ $key ] = $value; $attributes[ $key ] = $value;
} }
@ -305,7 +319,7 @@ class WC_Product_Variation extends WC_Product_Simple {
} }
/** /**
* Returns array of attribute name value pairs. * Returns array of attribute name value pairs. Keys are prefixed with attribute_, as stored.
* *
* @param string $context * @param string $context
* @return array * @return array

View File

@ -336,4 +336,36 @@ class WC_Customer_Data_Store extends WC_Data_Store_WP implements WC_Customer_Dat
return wc_format_decimal( $spent, 2 ); return wc_format_decimal( $spent, 2 );
} }
/**
* Search customers and return customer IDs.
*
* @param string $term
* @return array
*/
public function search_customers( $term ) {
$query = new WP_User_Query( array(
'search' => '*' . esc_attr( $term ) . '*',
'search_columns' => array( 'user_login', 'user_url', 'user_email', 'user_nicename', 'display_name' ),
'fields' => 'ID',
) );
$query2 = new WP_User_Query( array(
'fields' => 'ID',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $term,
'compare' => 'LIKE',
),
array(
'key' => 'last_name',
'value' => $term,
'compare' => 'LIKE',
),
),
) );
return wp_parse_id_list( array_merge( $query->get_results(), $query2->get_results() ) );
}
} }

View File

@ -11,6 +11,14 @@ if ( ! defined( 'ABSPATH' ) ) {
* @author WooCommerce * @author WooCommerce
*/ */
class WC_Order_Item_Coupon_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface { class WC_Order_Item_Coupon_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface {
/**
* Data stored in meta keys.
* @since 2.7.0
* @var array
*/
protected $internal_meta_keys = array( 'discount_amount', 'discount_amount_tax' );
/** /**
* Read/populate data properties specific to this order item. * Read/populate data properties specific to this order item.
* *

View File

@ -11,6 +11,13 @@ if ( ! defined( 'ABSPATH' ) ) {
* @author WooCommerce * @author WooCommerce
*/ */
class WC_Order_Item_Fee_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface { class WC_Order_Item_Fee_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface {
/**
* Data stored in meta keys.
* @since 2.7.0
* @var array
*/
protected $internal_meta_keys = array( '_tax_class', '_tax_status', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', '_line_tax_data' );
/** /**
* Read/populate data properties specific to this order item. * Read/populate data properties specific to this order item.
* *

View File

@ -11,6 +11,13 @@ if ( ! defined( 'ABSPATH' ) ) {
* @author WooCommerce * @author WooCommerce
*/ */
class WC_Order_Item_Product_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface, WC_Order_Item_Product_Data_Store_Interface { class WC_Order_Item_Product_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface, WC_Order_Item_Product_Data_Store_Interface {
/**
* Data stored in meta keys.
* @since 2.7.0
* @var array
*/
protected $internal_meta_keys = array( '_product_id', '_variation_id', '_qty', '_tax_class', '_line_subtotal', '_line_subtotal_tax', '_line_total', '_line_tax', '_line_tax_data' );
/** /**
* Read/populate data properties specific to this order item. * Read/populate data properties specific to this order item.
* *

View File

@ -11,6 +11,13 @@ if ( ! defined( 'ABSPATH' ) ) {
* @author WooCommerce * @author WooCommerce
*/ */
class WC_Order_Item_Shipping_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface { class WC_Order_Item_Shipping_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface {
/**
* Data stored in meta keys.
* @since 2.7.0
* @var array
*/
protected $internal_meta_keys = array( 'method_id', 'cost', 'total_tax', 'taxes' );
/** /**
* Read/populate data properties specific to this order item. * Read/populate data properties specific to this order item.
* *

View File

@ -11,6 +11,13 @@ if ( ! defined( 'ABSPATH' ) ) {
* @author WooCommerce * @author WooCommerce
*/ */
class WC_Order_Item_Tax_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface { class WC_Order_Item_Tax_Data_Store extends Abstract_WC_Order_Item_Type_Data_Store implements WC_Object_Data_Store_Interface, WC_Order_Item_Type_Data_Store_Interface {
/**
* Data stored in meta keys.
* @since 2.7.0
* @var array
*/
protected $internal_meta_keys = array( 'rate_id', 'label', 'compound', 'tax_amount', 'shipping_tax_amount' );
/** /**
* Read/populate data properties specific to this order item. * Read/populate data properties specific to this order item.
* *

View File

@ -1022,4 +1022,61 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
return $return; return $return;
} }
} }
/**
* Search product data for a term and return ids.
*
* @param string $term
* @param string $type of product
* @param bool $include_variations in search or not
* @return array of ids
*/
public function search_products( $term, $type = '', $include_variations = false ) {
global $wpdb;
$search_fields = array_map( 'wc_clean', apply_filters( 'woocommerce_product_search_fields', array(
'_sku',
) ) );
$like_term = '%' . $wpdb->esc_like( $term ) . '%';
$post_types = $include_variations ? array( 'product', 'product_variation' ) : array( 'product' );
$type_join = '';
$type_where = '';
if ( $type ) {
if ( in_array( $type, array( 'virtual', 'downloadable' ) ) ) {
$type_join = " LEFT JOIN {$wpdb->postmeta} postmeta_type ON posts.ID = postmeta_type.post_id ";
$type_where = " AND ( postmeta_type.meta_key = '_{$type}' AND postmeta_type.meta_value = 'yes' ) ";
}
}
$product_ids = $wpdb->get_col(
$wpdb->prepare( "
SELECT DISTINCT posts.ID FROM {$wpdb->posts} posts
LEFT JOIN {$wpdb->postmeta} postmeta ON posts.ID = postmeta.post_id
$type_join
WHERE (
posts.post_title LIKE %s
OR posts.post_content LIKE %s
OR (
postmeta.meta_key = '_sku' AND postmeta.meta_value LIKE %s
)
)
AND posts.post_type IN ('" . implode( "','", $post_types ) . "')
AND posts.post_status = 'publish'
$type_where
ORDER BY posts.post_parent ASC, posts.post_title ASC
",
$like_term,
$like_term,
$like_term
)
);
if ( is_numeric( $term ) ) {
$product_ids[] = absint( $term );
$product_ids[] = get_post_parent( $term );
}
return wp_parse_id_list( $product_ids );
}
} }

View File

@ -86,7 +86,7 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
'post_type' => 'product_variation', 'post_type' => 'product_variation',
'post_status' => $product->get_status() ? $product->get_status() : 'publish', 'post_status' => $product->get_status() ? $product->get_status() : 'publish',
'post_author' => get_current_user_id(), 'post_author' => get_current_user_id(),
'post_title' => get_the_title( $product->get_parent_id() ) . ' &ndash;' . wc_get_formatted_variation( $product->get_attributes(), true ), 'post_title' => get_the_title( $product->get_parent_id() ) . ' &ndash; ' . wc_get_formatted_variation( $product, true, false ),
'post_content' => '', 'post_content' => '',
'post_parent' => $product->get_parent_id(), 'post_parent' => $product->get_parent_id(),
'comment_status' => 'closed', 'comment_status' => 'closed',
@ -121,7 +121,7 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
public function update( &$product ) { public function update( &$product ) {
$post_data = array( $post_data = array(
'ID' => $product->get_id(), 'ID' => $product->get_id(),
'post_title' => get_the_title( $product->get_parent_id() ) . ' &ndash;' . wc_get_formatted_variation( $product->get_attributes(), true ), 'post_title' => get_the_title( $product->get_parent_id() ) . ' &ndash; ' . wc_get_formatted_variation( $product, true, false ),
'post_parent' => $product->get_parent_id(), 'post_parent' => $product->get_parent_id(),
'comment_status' => 'closed', 'comment_status' => 'closed',
'post_status' => $product->get_status() ? $product->get_status() : 'publish', 'post_status' => $product->get_status() ? $product->get_status() : 'publish',
@ -199,6 +199,7 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
} }
$product->set_parent_data( array( $product->set_parent_data( array(
'title' => get_the_title( $product->get_parent_id() ),
'sku' => get_post_meta( $product->get_parent_id(), '_sku', true ), 'sku' => get_post_meta( $product->get_parent_id(), '_sku', true ),
'manage_stock' => get_post_meta( $product->get_parent_id(), '_manage_stock', true ), 'manage_stock' => get_post_meta( $product->get_parent_id(), '_manage_stock', true ),
'backorders' => get_post_meta( $product->get_parent_id(), '_backorders', true ), 'backorders' => get_post_meta( $product->get_parent_id(), '_backorders', true ),
@ -233,8 +234,8 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
$attributes = $product->get_attributes(); $attributes = $product->get_attributes();
$updated_attribute_keys = array(); $updated_attribute_keys = array();
foreach ( $attributes as $key => $value ) { foreach ( $attributes as $key => $value ) {
update_post_meta( $product->get_id(), $key, $value ); update_post_meta( $product->get_id(), 'attribute_' . $key, $value );
$updated_attribute_keys[] = $key; $updated_attribute_keys[] = 'attribute_' . $key;
} }
// Remove old taxonomies attributes so data is kept up to date - first get attribute key names. // Remove old taxonomies attributes so data is kept up to date - first get attribute key names.

View File

@ -131,9 +131,16 @@ function wc_attribute_label( $name, $product = '' ) {
$name = wc_sanitize_taxonomy_name( str_replace( 'pa_', '', $name ) ); $name = wc_sanitize_taxonomy_name( str_replace( 'pa_', '', $name ) );
$all_labels = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' ); $all_labels = wp_list_pluck( wc_get_attribute_taxonomies(), 'attribute_label', 'attribute_name' );
$label = isset( $all_labels[ $name ] ) ? $all_labels[ $name ] : $name; $label = isset( $all_labels[ $name ] ) ? $all_labels[ $name ] : $name;
} elseif ( $product && ( $attributes = $product->get_attributes() ) && isset( $attributes[ sanitize_title( $name ) ]['name'] ) ) { } elseif ( $product ) {
// Attempt to get label from product, as entered by the user if ( $product->is_type( 'variation' ) ) {
$label = $attributes[ sanitize_title( $name ) ]['name']; $product = wc_get_product( $product->get_parent_id() );
}
// Attempt to get label from product, as entered by the user.
if ( ( $attributes = $product->get_attributes() ) && isset( $attributes[ sanitize_title( $name ) ] ) ) {
$label = $attributes[ sanitize_title( $name ) ]->get_name();
} else {
$label = $name;
}
} else { } else {
$label = $name; $label = $name;
} }
@ -290,3 +297,14 @@ function wc_check_if_attribute_name_is_reserved( $attribute_name ) {
function wc_attributes_array_filter_visible( $attribute ) { function wc_attributes_array_filter_visible( $attribute ) {
return $attribute && is_a( $attribute, 'WC_Product_Attribute' ) && $attribute->get_visible() && ( ! $attribute->is_taxonomy() || taxonomy_exists( $attribute->get_name() ) ); return $attribute && is_a( $attribute, 'WC_Product_Attribute' ) && $attribute->get_visible() && ( ! $attribute->is_taxonomy() || taxonomy_exists( $attribute->get_name() ) );
} }
/**
* Callback for array filter to get variation attributes only.
*
* @since 2.7.0
* @param WC_Product $product
* @return bool
*/
function wc_attributes_array_filter_variation( $attribute ) {
return $attribute && is_a( $attribute, 'WC_Product_Attribute' ) && $attribute->get_variation();
}

View File

@ -50,6 +50,19 @@ add_filter( 'woocommerce_short_description', 'shortcode_unautop' );
add_filter( 'woocommerce_short_description', 'prepend_attachment' ); add_filter( 'woocommerce_short_description', 'prepend_attachment' );
add_filter( 'woocommerce_short_description', 'do_shortcode', 11 ); // AFTER wpautop() add_filter( 'woocommerce_short_description', 'do_shortcode', 11 ); // AFTER wpautop()
/**
* Define a constant if it is not already defined.
*
* @since 2.7.0
* @param string $name
* @param string $value
*/
function wc_maybe_define_constant( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/** /**
* Create a new order programmatically. * Create a new order programmatically.
* *

View File

@ -323,28 +323,40 @@ function wc_placeholder_img( $size = 'shop_thumbnail' ) {
* *
* Gets a formatted version of variation data or item meta. * Gets a formatted version of variation data or item meta.
* *
* @param string $variation * @param array|WC_Product_Variation $variation
* @param bool $flat (default: false) * @param bool $flat (default: false)
* @param bool $include_names include attribute names/labels
* @return string * @return string
*/ */
function wc_get_formatted_variation( $variation, $flat = false ) { function wc_get_formatted_variation( $variation, $flat = false, $include_names = true ) {
$return = ''; $return = '';
if ( is_array( $variation ) ) {
if ( is_a( $variation, 'WC_Product_Variation' ) ) {
$variation_attributes = $variation->get_attributes();
$product = $variation;
} else {
$variation_attributes = $variation;
$product = false;
}
$list_type = $include_names ? 'dl' : 'ul';
if ( is_array( $variation_attributes ) ) {
if ( ! $flat ) { if ( ! $flat ) {
$return = '<dl class="variation">'; $return = '<' . $list_type . ' class="variation">';
} }
$variation_list = array(); $variation_list = array();
foreach ( $variation as $name => $value ) { foreach ( $variation_attributes as $name => $value ) {
if ( ! $value ) { if ( ! $value ) {
continue; continue;
} }
// If this is a term slug, get the term's nice name // If this is a term slug, get the term's nice name
if ( taxonomy_exists( esc_attr( str_replace( 'attribute_', '', $name ) ) ) ) { if ( taxonomy_exists( $name ) ) {
$term = get_term_by( 'slug', $value, esc_attr( str_replace( 'attribute_', '', $name ) ) ); $term = get_term_by( 'slug', $value, $name );
if ( ! is_wp_error( $term ) && ! empty( $term->name ) ) { if ( ! is_wp_error( $term ) && ! empty( $term->name ) ) {
$value = $term->name; $value = $term->name;
} }
@ -352,10 +364,18 @@ function wc_get_formatted_variation( $variation, $flat = false ) {
$value = ucwords( str_replace( '-', ' ', $value ) ); $value = ucwords( str_replace( '-', ' ', $value ) );
} }
if ( $flat ) { if ( $include_names ) {
$variation_list[] = wc_attribute_label( str_replace( 'attribute_', '', $name ) ) . ': ' . rawurldecode( $value ); if ( $flat ) {
$variation_list[] = wc_attribute_label( $name, $product ) . ': ' . rawurldecode( $value );
} else {
$variation_list[] = '<dt>' . wc_attribute_label( $name, $product ) . ':</dt><dd>' . rawurldecode( $value ) . '</dd>';
}
} else { } else {
$variation_list[] = '<dt>' . wc_attribute_label( str_replace( 'attribute_', '', $name ) ) . ':</dt><dd>' . rawurldecode( $value ) . '</dd>'; if ( $flat ) {
$variation_list[] = rawurldecode( $value );
} else {
$variation_list[] = '<li>' . rawurldecode( $value ) . '</li>';
}
} }
} }
@ -366,7 +386,7 @@ function wc_get_formatted_variation( $variation, $flat = false ) {
} }
if ( ! $flat ) { if ( ! $flat ) {
$return .= '</dl>'; $return .= '</' . $list_type . '>';
} }
} }
return $return; return $return;
@ -935,6 +955,17 @@ function wc_products_array_filter_visible( $product ) {
return $product && is_a( $product, 'WC_Product' ) && $product->is_visible(); return $product && is_a( $product, 'WC_Product' ) && $product->is_visible();
} }
/**
* Callback for array filter to get products the user can edit only.
*
* @since 2.7.0
* @param WC_Product $product
* @return bool
*/
function wc_products_array_filter_editable( $product ) {
return $product && is_a( $product, 'WC_Product' ) && current_user_can( 'edit_product', $product->get_id() );
}
/** /**
* Sort an array of products by a value. * Sort an array of products by a value.
* @since 2.7.0 * @since 2.7.0

View File

@ -347,6 +347,6 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
$this->assertEquals( 'publish', $loaded_variation->get_status( 'edit' ) ); $this->assertEquals( 'publish', $loaded_variation->get_status( 'edit' ) );
$_attribute = $loaded_variation->get_attributes( 'edit' ); $_attribute = $loaded_variation->get_attributes( 'edit' );
$this->assertEquals( 'green', $_attribute['attribute_color'] ); $this->assertEquals( 'green', $_attribute['color'] );
} }
} }