From afdc737ec56999eee1694fa2fb9ac449d883d34f Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Wed, 3 Jan 2018 13:38:06 -0800 Subject: [PATCH 1/2] Ajax add to cart button shortcode fix for variations --- includes/class-wc-ajax.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index 89ee563edfb..29a6fea6be0 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -360,11 +360,20 @@ class WC_AJAX { ob_start(); $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) ); + $product = wc_get_product( $product_id ); $quantity = empty( $_POST['quantity'] ) ? 1 : wc_stock_amount( $_POST['quantity'] ); $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity ); $product_status = get_post_status( $product_id ); + $variation_id = 0; + $variation = array(); - if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity ) && 'publish' === $product_status ) { + if ( 'variation' === $product->get_type() ) { + $variation_id = $product_id; + $product_id = $product->get_parent_id(); + $variation = $product->get_variation_attributes(); + } + + if ( $passed_validation && false !== WC()->cart->add_to_cart( $product_id, $quantity, $variation_id, $variation ) && 'publish' === $product_status ) { do_action( 'woocommerce_ajax_added_to_cart', $product_id ); From cdad70a6b1d36fd169c57111dd1ceb19e36c2fc3 Mon Sep 17 00:00:00 2001 From: claudiulodro Date: Wed, 3 Jan 2018 13:40:51 -0800 Subject: [PATCH 2/2] Safety first --- includes/class-wc-ajax.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index 29a6fea6be0..95a5f14858e 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -367,7 +367,7 @@ class WC_AJAX { $variation_id = 0; $variation = array(); - if ( 'variation' === $product->get_type() ) { + if ( $product && 'variation' === $product->get_type() ) { $variation_id = $product_id; $product_id = $product->get_parent_id(); $variation = $product->get_variation_attributes();