Skip to content
Permalink
Browse files

Fix: variation display and select issues

  • Loading branch information...
kilbot committed Jul 20, 2015
1 parent f59bba6 commit 91c7ec13e737f820d84feb7890d7b6d027a79792
Showing with 90 additions and 33 deletions.
  1. +4 −1 Gruntfile.js
  2. +72 −18 includes/api/class-wc-pos-products.php
  3. +5 −5 package.json
  4. +5 −2 readme.txt
  5. +4 −7 woocommerce-pos.php
@@ -28,10 +28,13 @@ module.exports = function(grunt) {
'!package.json',
'!locales.json',
'!phpunit.xml',
'!phpunit.xml.dist',
'!phpunit.int.xml',
'!pioneer.json',
'!<%= app.css.src %>/**',
'!<%= app.js.src %>/**',
'!README.md'
'!README.md',
'!coverage/**'
]
},

@@ -119,13 +119,13 @@ public function product_response( $data, $product, $fields, $server ) {
foreach( $data['variations'] as &$variation ) :
$_product = wc_get_product( $variation['id'] );
$variation = $this->filter_response_data( $variation, $_product );
$variation['attributes'] = $this->patch_variation_attributes( $variation['attributes'], $data['attributes'] );
$variation['attributes'] = $this->patch_variation_attributes( $_product );
endforeach;
endif;
// variation
if( $type == 'variation' ) :
$data['attributes'] = $this->patch_variation_attributes( $data['attributes'], $data['parent']['attributes'] );
$data['attributes'] = $this->patch_variation_attributes( $product );
endif;
return $this->filter_response_data( $data, $product );
@@ -146,25 +146,79 @@ private function patch_variable_attributes(array $attributes){
/**
* https://github.com/woothemes/woocommerce/issues/8457
* - restore the correct attribute name
* - add label
* @param array $attributes
* @param $parent_attributes
* patches WC_Product_Variable->get_variation_attributes()
* @param $product
* @return array
*/
private function patch_variation_attributes(array $attributes, array $parent_attributes){
foreach( $attributes as &$attribute ) :
foreach($parent_attributes as $attr){
if( $attribute['slug'] == sanitize_title( $attr['slug'] ) ){
$attribute['name'] = $attr['name'];
$option_slugs = array_map( 'sanitize_title', $attr['options'] );
$key = array_search( $attribute['option'], $option_slugs );
$attribute['label'] = $attr['options'][$key];
break;
}
private function patch_variation_attributes( $product ){
$patched_attributes = array();
$attributes = $product->get_attributes();
$variation_attributes = $product->get_variation_attributes();
foreach( $variation_attributes as $slug => $option ){
$slug = str_replace( 'attribute_', '', $slug );
if( isset( $attributes[$slug] ) ){
$patched_attributes[] = array(
'slug' => str_replace( 'pa_', '', $slug ),
'name' => $this->get_variation_name( $attributes[$slug] ),
'option' => $option,
'label' => $this->get_variation_label( $product, $attributes[$slug], $option )
);
}
endforeach;
return $attributes;
}
return $patched_attributes;
}
/**
* @param $attribute
* @return null|string
*/
private function get_variation_name( $attribute ){
if( $attribute['is_taxonomy'] ){
global $wpdb;
$name = $wpdb->get_var(
$wpdb->prepare("
SELECT attribute_label
FROM {$wpdb->prefix}woocommerce_attribute_taxonomies
WHERE attribute_name = %s;
", str_replace( 'pa_', '', $attribute['name'] ) ) );
if($name) return $name;
}
return $attribute['name'];
}
/**
* @param $product
* @param $option
* @param $attribute
* @return mixed
*/
private function get_variation_label( $product, $attribute, $option ){
$name = $option;
// taxonomy attributes
if ( $attribute['is_taxonomy'] ) {
$terms = wp_get_post_terms( $product->parent->id, $attribute['name'] );
if( !is_wp_error($terms) ) : foreach( $terms as $term ) :
if( $option == $term->slug ) $name = $term->name;
endforeach; endif;
// piped attributes
} else {
$values = array_map( 'trim', explode( WC_DELIMITER, $attribute['value'] ) );
$options = array_combine( array_map( 'sanitize_title', $values) , $values );
if( $options && isset( $options[$option] ) ){
$name = $options[$option];
}
}
return $name;
}
/**
@@ -46,11 +46,11 @@
"select2": "^3.5.2-browserify"
},
"devDependencies": {
"chai": "^3.0.0",
"chai": "^3.1.0",
"chai-as-promised": "^5.1.0",
"chai-backbone": "^0.9.2",
"chai-jquery": "^2.0.0",
"coveralls": "^2.11.2",
"coveralls": "^2.11.3",
"exports-loader": "^0.6.2",
"grunt": "^0.4.5",
"grunt-contrib-clean": "^0.6.0",
@@ -65,7 +65,7 @@
"grunt-mocha-istanbul": "^2.4.0",
"grunt-phpunit": "^0.3.6",
"grunt-simple-mocha": "^0.4.0",
"grunt-webpack": "^1.0.10",
"grunt-webpack": "^1.0.11",
"grunt-wp-i18n": "^0.5.2",
"imports-loader": "^0.6.3",
"istanbul": "^0.3.17",
@@ -75,13 +75,13 @@
"mocha": "^2.2.5",
"node-localstorage": "^0.5.0",
"pioneer": "^0.11.3",
"proxyquire": "^1.3.1",
"proxyquire": "^1.6.0",
"raw-loader": "^0.5.1",
"selenium-webdriver": "^2.46.1",
"sinon": "^1.15.4",
"sinon-as-promised": "^4.0.0",
"sinon-chai": "^2.8.0",
"webpack": "^1.10.0",
"webpack": "^1.10.1",
"webpack-dev-server": "^1.10.1"
}
}
@@ -1,8 +1,8 @@
=== WooCommerce POS ===
Contributors: kilbot
Tags: cart, e-commerce, ecommerce, inventory, point-of-sale, pos, sales, sell, shop, shopify, store, vend, woocommerce, wordpress-ecommerce
Requires at least: 4.0 & WooCommerce 2.2
Tested up to: 4.2
Requires at least: 4.0 & WooCommerce 2.3
Tested up to: 4.2.2
Stable tag: 0.4.1
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -77,6 +77,9 @@ Bugs can be reported on the [WooCommerce POS GitHub repository](https://github.c

== Changelog ==

= 0.4.2 =
* Fix: variation display and select issues - commit

= 0.4.1 =
* Note: WooCommerce POS now requires WooCommerce 2.3 or greater
* New: added woocommerce_pos_email_receipt hook
@@ -52,11 +52,8 @@
* Begins execution of the plugin.
*/
function run_woocommerce_pos() {
new WC_POS();
if ( class_exists('WooCommerce') ) {
new WC_POS();
}
}
global $wp_actions;
if ( isset( $wp_actions['woocommerce_loaded'] ) ) {
run_woocommerce_pos();
} else {
add_action( 'woocommerce_loaded', 'run_woocommerce_pos' );
}
add_action( 'plugins_loaded', 'run_woocommerce_pos' );

0 comments on commit 91c7ec1

Please sign in to comment.
You can’t perform that action at this time.