Merge pull request #18351 from woocommerce/fix/18318
Ajax add-to-cart button shortcode fix for variations
This commit is contained in:
commit
e59e45e52a
|
@ -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 ( $product && '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 );
|
||||
|
||||
|
|
Loading…
Reference in New Issue