From 92d64cffd637256fd310ce1cde6452ca0c12cfca Mon Sep 17 00:00:00 2001 From: Guilherme Capanema Date: Tue, 22 Oct 2019 10:03:49 -0300 Subject: [PATCH] Ajustes para permitir produtos repetidos nos combos. --- admin/css/wc-cdp-admin.css | 9 ++- admin/js/wc-cdp-admin.js | 49 +++++++-------- admin/partials/product-fields-search.php | 26 ++++++++ admin/partials/product-fields.php | 39 +++++------- public/class-wc-cdp-public.php | 80 ++++++++++++++++++------ public/partials/product-bundle-modal.php | 11 +++- 6 files changed, 141 insertions(+), 73 deletions(-) create mode 100644 admin/partials/product-fields-search.php diff --git a/admin/css/wc-cdp-admin.css b/admin/css/wc-cdp-admin.css index 00c8c7f..3b7aa08 100755 --- a/admin/css/wc-cdp-admin.css +++ b/admin/css/wc-cdp-admin.css @@ -1,4 +1,11 @@ /** * All of the CSS for your admin-specific functionality should be * included in this file. - */ \ No newline at end of file + */ +._wc_cdp_bundle_product ._wc_cdp_bundle_product_remove { + margin-left: 0.5rem; +} + +._wc_cdp_bundle_product:last-child ._wc_cdp_bundle_product_remove { + display: none; +} \ No newline at end of file diff --git a/admin/js/wc-cdp-admin.js b/admin/js/wc-cdp-admin.js index b04717f..75d1224 100755 --- a/admin/js/wc-cdp-admin.js +++ b/admin/js/wc-cdp-admin.js @@ -1,32 +1,27 @@ (function( $ ) { 'use strict'; - /** - * All of the code for your admin-facing JavaScript source - * should reside in this file. - * - * Note: It has been assumed you will write jQuery code here, so the - * $ function reference has been prepared for usage within the scope - * of this function. - * - * This enables you to define handlers, for when the DOM is ready: - * - * $(function() { - * - * }); - * - * When the window is loaded: - * - * $( window ).load(function() { - * - * }); - * - * ...and/or other possibilities. - * - * Ideally, it is not considered best practise to attach more than a - * single DOM-ready or window-load handler for a particular page. - * Although scripts in the WordPress core, Plugins and Themes may be - * practising this, we should strive to set a better example in our own work. - */ + $(function() { + + $(document.body).on('change', '._wc_cdp_bundle_product_select', function() { + console.debug('select triggered'); + if ( ! $( this ).closest( '._wc_cdp_bundle_product' ).is( ':last-child' ) ) { + return; + } + console.debug('after return check'); + var container = $( this ).closest( '._wc_cdp_products_container' ), + index = container.find( '._wc_cdp_bundle_product' ).length, + row = container.data( 'row' ).replace( /\[99999\]/g, '[' + index + ']' ); + + container.append(row); + $( document.body ).trigger( 'wc-enhanced-select-init' ); + }); + + $('._wc_cdp_bundle_product_remove').click(function(event) { + event.preventDefault(); + $(this).closest('._wc_cdp_bundle_product').remove(); + }); + }); + })( jQuery ); diff --git a/admin/partials/product-fields-search.php b/admin/partials/product-fields-search.php new file mode 100644 index 0000000..68ad408 --- /dev/null +++ b/admin/partials/product-fields-search.php @@ -0,0 +1,26 @@ +

+ + + + + + + + + + + + +

diff --git a/admin/partials/product-fields.php b/admin/partials/product-fields.php index a39fcae..fdbb89f 100755 --- a/admin/partials/product-fields.php +++ b/admin/partials/product-fields.php @@ -11,33 +11,26 @@ * @package Wc_Cdp * @subpackage Wc_Cdp/admin/partials */ -?> -
- -

- - - - +$index = 99999; // dummy index for data-row attribute +ob_start(); //Init the output buffering +include('product-fields-search.php'); //Include (and compiles) the given file +$row_data = ob_get_clean(); //Get the buffer and erase it +$index = 0; +?> +

+
$product_id ) { + $inner_product = wc_get_product( $product_id ); + include('product-fields-search.php'); + } + $index += 1; + $inner_product = null; + include('product-fields-search.php'); ?> - -

+
diff --git a/public/class-wc-cdp-public.php b/public/class-wc-cdp-public.php index c1853d3..c283562 100755 --- a/public/class-wc-cdp-public.php +++ b/public/class-wc-cdp-public.php @@ -106,14 +106,60 @@ class Wc_Cdp_Public { if ( !$product->get_meta( '_wc_cdp_bundle_products' ) ) return null; - $bundle_products = wc_get_products([ - 'limit' => 2, - 'include' => $product->get_meta( '_wc_cdp_bundle_products' ), - ]); + $bundle_products = array(); + + foreach ($product->get_meta( '_wc_cdp_bundle_products' ) as $bundle_product) { + $bundle_products[] = wc_get_product($bundle_product); + } + + array_unshift($bundle_products, $product); return $bundle_products; } + private function get_bundle_products_quantities($bundle_products) + { + $bundle_product_ids = array_map(function($product) { + return $product->get_id(); + }, $bundle_products); + + return array_count_values($bundle_product_ids); + } + + private function get_bundle_product_quantity($bundle_products, $product_id) + { + $bundle_product_quantities = $this->get_bundle_products_quantities($bundle_products); + + return $bundle_product_quantities[$product_id]; + } + + private function is_product_in_cart($product_id) + { + $product = wc_get_product( $product_id ); + + $product_cart_id = WC()->cart->generate_cart_id( $product->get_id() ); + + if ( !WC()->cart->find_product_in_cart( $product_cart_id ) ) { + return false; + } + + return true; + } + + private function get_product_in_cart_quantity($product_id) + { + $product = wc_get_product( $product_id ); + + $product_cart_id = WC()->cart->generate_cart_id( $product->get_id() ); + $cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id ); + + if ($cart_item_key) { + return WC()->cart->get_cart_item($cart_item_key)['quantity']; + } + + return 0; + } + private function get_bundle_coupon($product_id) { $product = wc_get_product( $product_id ); @@ -133,29 +179,30 @@ class Wc_Cdp_Public { private function should_display_bundle_modal($product_id) { + $product = wc_get_product( $product_id ); $bundle_products = $this->get_bundle_products($product_id); if (!$bundle_products) return false; - $all_products_in_cart = true; - foreach ($bundle_products as $bundle_product) { + if ($bundle_product->get_id() === $product_id) continue; + if ($bundle_product->get_stock_status() === 'outofstock' && $bundle_product->get_backorders() !== 'yes') return false; - $product_cart_id = WC()->cart->generate_cart_id( $bundle_product->get_id() ); - if ( !WC()->cart->find_product_in_cart( $product_cart_id ) ) { - $all_products_in_cart = false; - } + if ( $this->is_product_in_cart( $bundle_product->get_id() ) && + ( + $this->get_bundle_product_quantity( $bundle_products, $bundle_product->get_id() ) <= + $this->get_product_in_cart_quantity( $bundle_product->get_id() ) + ) + ) return false; } - if ($all_products_in_cart) return false; - return true; } public function maybe_display_bundle_modal($cart_item_key, $product_id) { - if ( $this->should_display_bundle_modal( $product_id ) ) { // TODO: Check if all bundle products are available + if ( $this->should_display_bundle_modal( $product_id ) ) { add_action( 'woocommerce_after_single_product', array( $this, 'display_bundle_modal' ) ); } } @@ -166,11 +213,7 @@ class Wc_Cdp_Public { $bundle_products = $this->get_bundle_products( $product->get_id() ); - $bundle_ids = array_map( function($product) { - return $product->get_id(); - }, $bundle_products ); - - array_unshift($bundle_products, $product); + $bundle_products_quantities = $this->get_bundle_products_quantities($bundle_products); $bundle_regular_price = array_sum( array_map( function ($product) { return $product->get_price(); @@ -186,7 +229,6 @@ class Wc_Cdp_Public { } } - require('partials/product-bundle-modal.php'); } diff --git a/public/partials/product-bundle-modal.php b/public/partials/product-bundle-modal.php index a887aec..9de6864 100644 --- a/public/partials/product-bundle-modal.php +++ b/public/partials/product-bundle-modal.php @@ -52,8 +52,10 @@ cart->generate_cart_id( $bundle_product->get_id() ); - if ( WC()->cart->find_product_in_cart( $product_cart_id ) ) : + if ( + $bundle_products_quantities[$bundle_product->get_id()] <= + $this->get_product_in_cart_quantity($bundle_product->get_id()) + ) : ?>

@@ -62,7 +64,10 @@

- + get_id()]--; + ?>