Browse Source

Ajustes para permitir produtos repetidos nos combos.

master
Guilherme Capanema 6 years ago
parent
commit
92d64cffd6
6 changed files with 141 additions and 73 deletions
  1. +8
    -1
      admin/css/wc-cdp-admin.css
  2. +22
    -27
      admin/js/wc-cdp-admin.js
  3. +26
    -0
      admin/partials/product-fields-search.php
  4. +16
    -23
      admin/partials/product-fields.php
  5. +61
    -19
      public/class-wc-cdp-public.php
  6. +8
    -3
      public/partials/product-bundle-modal.php

+ 8
- 1
admin/css/wc-cdp-admin.css View File

@ -1,4 +1,11 @@
/**
* All of the CSS for your admin-specific functionality should be
* included in this 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;
}

+ 22
- 27
admin/js/wc-cdp-admin.js View File

@ -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 );

+ 26
- 0
admin/partials/product-fields-search.php View File

@ -0,0 +1,26 @@
<p class="form-field hide_if_grouped hide_if_external _wc_cdp_bundle_product">
<label for="_wc_cdp_bundle_product_<?php echo $index; ?>">
<?php echo $index === 0 ? esc_html( 'Produtos do combo', 'wc-cdp' ) : ''; ?>
</label>
<select class="wc-product-search _wc_cdp_bundle_product_select" style="width: 50%;" id="_wc_cdp_bundle_product_<?php echo $index; ?>" name="_wc_cdp_bundle_products[]" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations">
<?php
if ( is_object( $inner_product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $inner_product->get_formatted_name() ) . '</option>';
}
?>
</select>
<a href="#" class="_wc_cdp_bundle_product_remove">
<?php esc_attr_e( 'Remover', 'wc-cdp' ); ?>
</a>
<?php
echo $index === 0 ? wc_help_tip( __( 'Quando a pessoa adiciona o produto atual ao carrinho, os produtos inseridos aqui são exibidos em um popup.', 'wc-cdp' ) ) : '';
?>
</p>

+ 16
- 23
admin/partials/product-fields.php View File

@ -11,33 +11,26 @@
* @package Wc_Cdp
* @subpackage Wc_Cdp/admin/partials
*/
?>
<div class="options_group">
<p class="form-field hide_if_grouped hide_if_external">
<label for="_wc_cdp_bundle_products">
<?php esc_html_e( 'Produtos do combo', 'wc-cdp' ); ?>
</label>
<select class="wc-product-search" multiple="multiple" style="width: 50%;" id="_wc_cdp_bundle_products" name="_wc_cdp_bundle_products[]" data-placeholder="<?php esc_attr_e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-exclude="<?php echo intval( $post->ID ); ?>">
<?php
foreach ( $bundle_products as $product_id ) {
$inner_product = wc_get_product( $product_id );
if ( is_object( $inner_product ) ) {
echo '<option value="' . esc_attr( $product_id ) . '"' . selected( true, true, false ) . '>' . wp_kses_post( $inner_product->get_formatted_name() ) . '</option>';
}
}
?>
</select>
$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;
?>
<div class="options_group _wc_cdp_container">
<div class="_wc_cdp_products_container" data-row="<?php esc_html_e($row_data); ?>">
<?php
echo wc_help_tip( __( 'Quando a pessoa adiciona o produto atual ao carrinho, os produtos inseridos aqui são exibidos em um popup.', 'wc-cdp' ) );
foreach ( $bundle_products as $index => $product_id ) {
$inner_product = wc_get_product( $product_id );
include('product-fields-search.php');
}
$index += 1;
$inner_product = null;
include('product-fields-search.php');
?>
</p>
</div>
<!-- <p class="form-field hide_if_grouped hide_if_external"> -->


+ 61
- 19
public/class-wc-cdp-public.php View File

@ -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');
}


+ 8
- 3
public/partials/product-bundle-modal.php View File

@ -52,8 +52,10 @@
</h3>
<?php
$product_cart_id = WC()->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())
) :
?>
<p class="<?php echo apply_filters( 'wc_cdp_already_on_cart_class', 'wc_cdp_modal_product_already_on_cart' ); ?>">
@ -62,7 +64,10 @@
</small>
</p>
<?php else: ?>
<?php
else:
$bundle_products_quantities[$bundle_product->get_id()]--;
?>
<input type="hidden" name="wc_cdp_bundle_products[]" value="<?php echo $bundle_product->get_id() ?>" />


Loading…
Cancel
Save