From eb3b1dbee6613c54d35c53b3f5ce6bd2a6340c95 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 11 Apr 2018 13:19:39 +0100 Subject: [PATCH] Add dummy product to cart when viewing preview --- includes/class-wc-cart-session.php | 4 +++ .../customizer/class-wc-shop-customizer.php | 26 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/includes/class-wc-cart-session.php b/includes/class-wc-cart-session.php index 668c6e1acb6..ed9d87acf09 100644 --- a/includes/class-wc-cart-session.php +++ b/includes/class-wc-cart-session.php @@ -97,6 +97,10 @@ final class WC_Cart_Session { foreach ( $cart as $key => $values ) { $product = wc_get_product( $values['variation_id'] ? $values['variation_id'] : $values['product_id'] ); + if ( ! is_customize_preview() && 'customize-preview' === $key ) { + continue; + } + if ( ! empty( $product ) && $product->exists() && $values['quantity'] > 0 ) { if ( ! $product->is_purchasable() ) { diff --git a/includes/customizer/class-wc-shop-customizer.php b/includes/customizer/class-wc-shop-customizer.php index 84e18369918..9fb9a00861d 100644 --- a/includes/customizer/class-wc-shop-customizer.php +++ b/includes/customizer/class-wc-shop-customizer.php @@ -21,6 +21,7 @@ class WC_Shop_Customizer { add_action( 'customize_controls_print_styles', array( $this, 'add_styles' ) ); add_action( 'customize_controls_print_scripts', array( $this, 'add_scripts' ), 30 ); add_action( 'wp_enqueue_scripts', array( $this, 'add_frontend_scripts' ) ); + add_action( 'woocommerce_cart_loaded_from_session', array( $this, 'populate_cart' ) ); } /** @@ -54,6 +55,31 @@ class WC_Shop_Customizer { wp_add_inline_style( 'customize-preview', $css ); } + /** + * Make sure the cart has something inside when we're customizing. + * + * @return void + */ + public function populate_cart() { + if ( ! is_customize_preview() ) { + return; + } + if ( WC()->cart->is_empty() ) { + $dummy_product = new WC_Product(); + $dummy_product->set_name( 'Sample' ); + $dummy_product->set_price( 0 ); + $dummy_product->set_status( 'publish' ); + $cart_contents['customize-preview'] = array( + 'data' => $dummy_product, + 'product_id' => 0, + 'variation_id' => 0, + 'data_hash' => false, + 'quantity' => 1, + ); + WC()->cart->set_cart_contents( $cart_contents ); + } + } + /** * CSS styles to improve our form. */