Add dummy product to cart when viewing preview

This commit is contained in:
Mike Jolley 2018-04-11 13:19:39 +01:00
parent 2464a94920
commit eb3b1dbee6
2 changed files with 30 additions and 0 deletions

View File

@ -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() ) {

View File

@ -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.
*/