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