public function create_cart_entry($addItem) { if ($addItem) { if (WC()->cart->add_to_cart($addItem->productId, $addItem->quantity)) { do_action("woocommerce_ajax_added_to_cart", $addItem->productId); if (get_option("woocommerce_cart_redirect_after_add") == "yes") { return wc_add_to_cart_message($addItem->productId); } } else { return array("error" => true, "product_url" => apply_filters("woocommerce_cart_redirect_after_error", get_permalink($addItem->productId), $addItem->productId)); } } }
function woocommerce_add_to_cart_simple_rc_callback() { ob_start(); $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['product_id'])); $quantity = empty($_POST['quantity']) ? 1 : apply_filters('woocommerce_stock_amount', $_POST['quantity']); $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity); if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity)) { do_action('woocommerce_ajax_added_to_cart', $product_id); if (get_option('woocommerce_cart_redirect_after_add') == 'yes') { wc_add_to_cart_message($product_id); } // Return fragments WC_AJAX::get_refreshed_fragments(); } else { $this->json_headers(); // If there was an error adding to the cart, redirect to the product page to show any errors $data = array('error' => true, 'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink($product_id), $product_id)); echo json_encode($data); } die; }
function woocommerce_add_to_cart_variable_rc_callback() { ob_start(); $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['product_id'])); $quantity = empty($_POST['quantity']) ? 1 : apply_filters('woocommerce_stock_amount', $_POST['quantity']); $variation_id = $_POST['variation_id']; $variation = $_POST['variation']; $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity); if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variation)) { do_action('woocommerce_ajax_added_to_cart', $product_id); if (get_option('woocommerce_cart_redirect_after_add') == 'yes') { wc_add_to_cart_message($product_id); } // Return fragments WC_AJAX::get_refreshed_fragments(); } else { // If there was an error adding to the cart, redirect to the product page to show any errors $notice = end(wc_get_notices('error')); $data = array('error' => true, 'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink($product_id), $product_id), 'notice' => $notice); wp_send_json($data); } die; }
/** * Handle adding variable products to the cart. * @since 2.4.6 Split from add_to_cart_action * @param int $product_id * @return bool success or not */ private static function add_to_cart_handler_variable($product_id) { $adding_to_cart = wc_get_product($product_id); $variation_id = empty($_REQUEST['variation_id']) ? '' : absint($_REQUEST['variation_id']); $quantity = empty($_REQUEST['quantity']) ? 1 : wc_stock_amount($_REQUEST['quantity']); $missing_attributes = array(); $variations = array(); $attributes = $adding_to_cart->get_attributes(); // If no variation ID is set, attempt to get a variation ID from posted attributes. if (empty($variation_id)) { $variation_id = $adding_to_cart->get_matching_variation(wp_unslash($_POST)); } $variation = wc_get_product($variation_id); // Validate the attributes. try { if (empty($variation_id)) { throw new Exception(__('Please choose product options…', 'woocommerce')); } foreach ($attributes as $attribute) { if (!$attribute['is_variation']) { continue; } $taxonomy = 'attribute_' . sanitize_title($attribute['name']); if (isset($_REQUEST[$taxonomy])) { // Get value from post data if ($attribute['is_taxonomy']) { // Don't use wc_clean as it destroys sanitized characters $value = sanitize_title(stripslashes($_REQUEST[$taxonomy])); } else { $value = wc_clean(stripslashes($_REQUEST[$taxonomy])); } // Get valid value from variation $valid_value = isset($variation->variation_data[$taxonomy]) ? $variation->variation_data[$taxonomy] : ''; // Allow if valid or show error. if ('' === $valid_value || $valid_value === $value) { $variations[$taxonomy] = $value; } else { throw new Exception(sprintf(__('Invalid value posted for %s', 'woocommerce'), wc_attribute_label($attribute['name']))); } } else { $missing_attributes[] = wc_attribute_label($attribute['name']); } } if (!empty($missing_attributes)) { throw new Exception(sprintf(_n('%s is a required field', '%s are required fields', sizeof($missing_attributes), 'woocommerce'), wc_format_list_of_items($missing_attributes))); } } catch (Exception $e) { wc_add_notice($e->getMessage(), 'error'); return false; } // Add to cart validation $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations); if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations) !== false) { wc_add_to_cart_message(array($product_id => $quantity), true); return true; } return false; }
/** * Add to cart action * * Checks for a valid request, does validation (via hooks) and then redirects if valid. * * @param bool $url (default: false) */ public static function add_to_cart_action($url = false) { if (empty($_REQUEST['add-to-cart']) || !is_numeric($_REQUEST['add-to-cart'])) { return; } $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_REQUEST['add-to-cart'])); $was_added_to_cart = false; $added_to_cart = array(); $adding_to_cart = wc_get_product($product_id); $add_to_cart_handler = apply_filters('woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart); // Check if the product is published if ('publish' !== $adding_to_cart->post->post_status) { wc_add_notice(__('Sorry, this product is unavailable.', 'woocommerce'), 'error'); return; } // Variable product handling if ('variable' === $add_to_cart_handler) { $variation_id = empty($_REQUEST['variation_id']) ? '' : absint($_REQUEST['variation_id']); $quantity = empty($_REQUEST['quantity']) ? 1 : wc_stock_amount($_REQUEST['quantity']); $missing_attributes = array(); $variations = array(); $attributes = $adding_to_cart->get_attributes(); $variation = wc_get_product($variation_id); // Verify all attributes foreach ($attributes as $attribute) { if (!$attribute['is_variation']) { continue; } $taxonomy = 'attribute_' . sanitize_title($attribute['name']); if (isset($_REQUEST[$taxonomy])) { // Get value from post data if ($attribute['is_taxonomy']) { // Don't use wc_clean as it destroys sanitized characters $value = sanitize_title(stripslashes($_REQUEST[$taxonomy])); } else { $value = wc_clean(stripslashes($_REQUEST[$taxonomy])); } // Get valid value from variation $valid_value = $variation->variation_data[$taxonomy]; // Allow if valid if ('' === $valid_value || $valid_value === $value) { // Pre 2.4 handling where 'slugs' were saved instead of the full text attribute if (!$attribute['is_taxonomy']) { if ($value === sanitize_title($value) && version_compare(get_post_meta($product_id, '_product_version', true), '2.4.0', '<')) { $text_attributes = wc_get_text_attributes($attribute['value']); foreach ($text_attributes as $text_attribute) { if (sanitize_title($text_attribute) === $value) { $value = $text_attribute; break; } } } } $variations[$taxonomy] = $value; continue; } } else { $missing_attributes[] = wc_attribute_label($attribute['name']); } } if ($missing_attributes) { wc_add_notice(sprintf(_n('%s is a required field', '%s are required fields', sizeof($missing_attributes), 'woocommerce'), wc_format_list_of_items($missing_attributes)), 'error'); return; } elseif (empty($variation_id)) { wc_add_notice(__('Please choose product options…', 'woocommerce'), 'error'); return; } else { // Add to cart validation $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations); if ($passed_validation) { if (WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations) !== false) { wc_add_to_cart_message($product_id); $was_added_to_cart = true; $added_to_cart[] = $product_id; } } } // Grouped Products } elseif ('grouped' === $add_to_cart_handler) { if (!empty($_REQUEST['quantity']) && is_array($_REQUEST['quantity'])) { $quantity_set = false; foreach ($_REQUEST['quantity'] as $item => $quantity) { if ($quantity <= 0) { continue; } $quantity_set = true; // Add to cart validation $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $item, $quantity); if ($passed_validation) { if (WC()->cart->add_to_cart($item, $quantity) !== false) { $was_added_to_cart = true; $added_to_cart[] = $item; } } } if ($was_added_to_cart) { wc_add_to_cart_message($added_to_cart); } if (!$was_added_to_cart && !$quantity_set) { wc_add_notice(__('Please choose the quantity of items you wish to add to your cart…', 'woocommerce'), 'error'); return; } } elseif ($product_id) { /* Link on product archives */ wc_add_notice(__('Please choose a product to add to your cart…', 'woocommerce'), 'error'); return; } // Custom Handler } elseif (has_action('woocommerce_add_to_cart_handler_' . $add_to_cart_handler)) { do_action('woocommerce_add_to_cart_handler_' . $add_to_cart_handler, $url); return; // Simple Products } else { $quantity = empty($_REQUEST['quantity']) ? 1 : wc_stock_amount($_REQUEST['quantity']); // Add to cart validation $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity); if ($passed_validation) { // Add the product to the cart if (WC()->cart->add_to_cart($product_id, $quantity) !== false) { wc_add_to_cart_message($product_id); $was_added_to_cart = true; $added_to_cart[] = $product_id; } } } // If we added the product to the cart we can now optionally do a redirect. if ($was_added_to_cart && wc_notice_count('error') == 0) { $url = apply_filters('woocommerce_add_to_cart_redirect', $url); // If has custom URL redirect there if ($url) { wp_safe_redirect($url); exit; } elseif (get_option('woocommerce_cart_redirect_after_add') == 'yes') { wp_safe_redirect(WC()->cart->get_cart_url()); exit; } } }
/** * AJAX add to cart */ public static function add_to_cart() { ob_start(); $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['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); if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity) && 'publish' === $product_status) { do_action('woocommerce_ajax_added_to_cart', $product_id); if (get_option('woocommerce_cart_redirect_after_add') == 'yes') { wc_add_to_cart_message($product_id); } // Return fragments self::get_refreshed_fragments(); } else { // If there was an error adding to the cart, redirect to the product page to show any errors $data = array('error' => true, 'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink($product_id), $product_id)); wp_send_json($data); } die; }
if (isset($_GET['redirect_to_cart']) && $_GET['redirect_to_cart'] == 'true') { if (function_exists('icl_object_id')) { $redirect_url = get_permalink(icl_object_id(function_exists('wc_get_page_id') ? wc_get_page_id('cart') : woocommerce_get_page_id('cart')), 'page', true); } else { $redirect_url = get_permalink(function_exists('wc_get_page_id') ? wc_get_page_id('cart') : woocommerce_get_page_id('cart')); } } else { $redirect_url = $yith_wcwl->get_wishlist_url(); } //get the details of the product $details = $yith_wcwl->get_product_details($_GET['wishlist_item_id']); //add to the cart if ($woocommerce->cart->add_to_cart($details[0]['prod_id'], 1)) { //$_SESSION['messages'] = sprintf( '<a href="%s" class="button">%s</a> %s', get_permalink( woocommerce_get_page_id( 'cart' ) ), __( 'View Cart →', 'yit' ), __( 'Product successfully added to the cart.', 'yit' ) ); if (function_exists('wc_add_to_cart_message')) { wc_add_to_cart_message($details[0]['prod_id']); } else { woocommerce_add_to_cart_message($details[0]['prod_id']); } //$woocommerce->set_messages(); if (get_option('yith_wcwl_remove_after_add_to_cart') == 'yes') { $yith_wcwl->remove($details[0]['ID']); } header("Location: {$redirect_url}"); } else { //if failed, redirect to wishlist page with errors if (function_exists('wc_get_notices')) { $_SESSION['errors'] = wc_get_notices("error"); } else { $_SESSION['errors'] = $woocommerce->get_errors(); }
/** * Handle adding variable products to the cart * @since 2.4.6 Split from add_to_cart_action * @param int $product_id * @return bool success or not */ private static function add_to_cart_handler_variable($product_id) { $adding_to_cart = wc_get_product($product_id); $variation_id = empty($_REQUEST['variation_id']) ? '' : absint($_REQUEST['variation_id']); $quantity = empty($_REQUEST['quantity']) ? 1 : wc_stock_amount($_REQUEST['quantity']); $missing_attributes = array(); $variations = array(); $attributes = $adding_to_cart->get_attributes(); $variation = wc_get_product($variation_id); // Verify all attributes foreach ($attributes as $attribute) { if (!$attribute['is_variation']) { continue; } $taxonomy = 'attribute_' . sanitize_title($attribute['name']); if (isset($_REQUEST[$taxonomy])) { // Get value from post data if ($attribute['is_taxonomy']) { // Don't use wc_clean as it destroys sanitized characters $value = sanitize_title(stripslashes($_REQUEST[$taxonomy])); } else { $value = wc_clean(stripslashes($_REQUEST[$taxonomy])); } // Get valid value from variation $valid_value = $variation->variation_data[$taxonomy]; // Allow if valid if ('' === $valid_value || $valid_value === $value) { $variations[$taxonomy] = $value; continue; } } else { $missing_attributes[] = wc_attribute_label($attribute['name']); } } if ($missing_attributes) { wc_add_notice(sprintf(_n('%s is a required field', '%s are required fields', sizeof($missing_attributes), 'woocommerce'), wc_format_list_of_items($missing_attributes)), 'error'); } elseif (empty($variation_id)) { wc_add_notice(__('Please choose product options…', 'woocommerce'), 'error'); } else { // Add to cart validation $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations); if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations) !== false) { wc_add_to_cart_message($product_id); return true; } } return false; }
/** * @deprecated */ function woocommerce_add_to_cart_message($product_id) { wc_add_to_cart_message($product_id); }
/** * Test wc_add_to_cart_message */ public function test_wc_add_to_cart_message() { $product = WC_Helper_Product::create_simple_product(); $message = wc_add_to_cart_message(array($product->id => 1), false, true); $this->assertEquals('<a href="http://example.org" class="button wc-forward">View Cart</a> “Dummy Product” has been added to your cart.', $message); $message = wc_add_to_cart_message(array($product->id => 3), false, true); $this->assertEquals('<a href="http://example.org" class="button wc-forward">View Cart</a> “Dummy Product” has been added to your cart.', $message); $message = wc_add_to_cart_message(array($product->id => 1), true, true); $this->assertEquals('<a href="http://example.org" class="button wc-forward">View Cart</a> “Dummy Product” has been added to your cart.', $message); $message = wc_add_to_cart_message(array($product->id => 3), true, true); $this->assertEquals('<a href="http://example.org" class="button wc-forward">View Cart</a> 3 × “Dummy Product” have been added to your cart.', $message); $message = wc_add_to_cart_message($product->id, false, true); $this->assertEquals('<a href="http://example.org" class="button wc-forward">View Cart</a> “Dummy Product” has been added to your cart.', $message); $message = wc_add_to_cart_message($product->id, true, true); $this->assertEquals('<a href="http://example.org" class="button wc-forward">View Cart</a> “Dummy Product” has been added to your cart.', $message); }
/** * Add to cart action * * Checks for a valid request, does validation (via hooks) and then redirects if valid. * * @param bool $url (default: false) */ public function add_to_cart_action($url = false) { if (empty($_REQUEST['add-to-cart']) || !is_numeric($_REQUEST['add-to-cart'])) { return; } $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_REQUEST['add-to-cart'])); $was_added_to_cart = false; $added_to_cart = array(); $adding_to_cart = get_product($product_id); $add_to_cart_handler = apply_filters('woocommerce_add_to_cart_handler', $adding_to_cart->product_type, $adding_to_cart); // Variable product handling if ('variable' === $add_to_cart_handler) { $variation_id = empty($_REQUEST['variation_id']) ? '' : absint($_REQUEST['variation_id']); $quantity = empty($_REQUEST['quantity']) ? 1 : apply_filters('woocommerce_stock_amount', $_REQUEST['quantity']); $all_variations_set = true; $variations = array(); // Only allow integer variation ID - if its not set, redirect to the product page if (empty($variation_id)) { wc_add_notice(__('Please choose product options…', 'woocommerce'), 'error'); return; } $attributes = $adding_to_cart->get_attributes(); $variation = get_product($variation_id); // Verify all attributes foreach ($attributes as $attribute) { if (!$attribute['is_variation']) { continue; } $taxonomy = 'attribute_' . sanitize_title($attribute['name']); if (isset($_REQUEST[$taxonomy])) { // Get value from post data // Don't use wc_clean as it destroys sanitized characters $value = sanitize_title(trim(stripslashes($_REQUEST[$taxonomy]))); // Get valid value from variation $valid_value = $variation->variation_data[$taxonomy]; // Allow if valid if ($valid_value == '' || $valid_value == $value) { if ($attribute['is_taxonomy']) { $variations[$taxonomy] = $value; } else { // For custom attributes, get the name from the slug $options = array_map('trim', explode(WC_DELIMITER, $attribute['value'])); foreach ($options as $option) { if (sanitize_title($option) == $value) { $value = $option; break; } } $variations[$taxonomy] = $value; } continue; } } $all_variations_set = false; } if ($all_variations_set) { // Add to cart validation $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations); if ($passed_validation) { if (WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations)) { wc_add_to_cart_message($product_id); $was_added_to_cart = true; $added_to_cart[] = $product_id; } } } else { wc_add_notice(__('Please choose product options…', 'woocommerce'), 'error'); return; } // Grouped Products } elseif ('grouped' === $add_to_cart_handler) { if (!empty($_REQUEST['quantity']) && is_array($_REQUEST['quantity'])) { $quantity_set = false; foreach ($_REQUEST['quantity'] as $item => $quantity) { if ($quantity <= 0) { continue; } $quantity_set = true; // Add to cart validation $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $item, $quantity); if ($passed_validation) { if (WC()->cart->add_to_cart($item, $quantity)) { $was_added_to_cart = true; $added_to_cart[] = $item; } } } if ($was_added_to_cart) { wc_add_to_cart_message($added_to_cart); } if (!$was_added_to_cart && !$quantity_set) { wc_add_notice(__('Please choose the quantity of items you wish to add to your cart…', 'woocommerce'), 'error'); return; } } elseif ($product_id) { /* Link on product archives */ wc_add_notice(__('Please choose a product to add to your cart…', 'woocommerce'), 'error'); return; } // Simple Products } else { $quantity = empty($_REQUEST['quantity']) ? 1 : apply_filters('woocommerce_stock_amount', $_REQUEST['quantity']); // Add to cart validation $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity); if ($passed_validation) { // Add the product to the cart if (WC()->cart->add_to_cart($product_id, $quantity)) { wc_add_to_cart_message($product_id); $was_added_to_cart = true; $added_to_cart[] = $product_id; } } } // If we added the product to the cart we can now optionally do a redirect. if ($was_added_to_cart && wc_notice_count('error') == 0) { $url = apply_filters('add_to_cart_redirect', $url); // If has custom URL redirect there if ($url) { wp_safe_redirect($url); exit; } elseif (get_option('woocommerce_cart_redirect_after_add') == 'yes') { wp_safe_redirect(WC()->cart->get_cart_url()); exit; } } }
/** * Custom add to cart handler for variable products * * Based on function add_to_cart_handler_variable( $product_id ) from * <install_dir>/wp-content/plugins/woocommerce/includes/class-wc-form-handler.php * but using $url as argument.Therefore we use the initial bits from * add_to_cart_action( $url ). * * @param string $url Add to cart url (e.g. https://www.yourdomain.com/?add-to-cart=123&quantity=1&variation_id=117&attribute_size=Small&attribute_color=Black ) */ public function add_to_cart_handler_variable($url) { // From add_to_cart_action( $url ) if (empty($_REQUEST['add-to-cart']) || !is_numeric($_REQUEST['add-to-cart'])) { return; } $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_REQUEST['add-to-cart'])); $was_added_to_cart = false; $adding_to_cart = wc_get_product($product_id); if (!$adding_to_cart) { return; } // End: From add_to_cart_action( $url ) // From add_to_cart_handler_variable( $product_id ) $variation_id = empty($_REQUEST['variation_id']) ? '' : absint($_REQUEST['variation_id']); $quantity = empty($_REQUEST['quantity']) ? 1 : wc_stock_amount($_REQUEST['quantity']); $missing_attributes = array(); $variations = array(); $attributes = $adding_to_cart->get_attributes(); // If no variation ID is set, attempt to get a variation ID from posted attributes. if (empty($variation_id)) { $variation_id = $adding_to_cart->get_matching_variation(wp_unslash($_POST)); } /** * Custom code to check if a translation of the product is already in the * cart,* and in that case, replace the variation being added to the cart * by the respective translation in the language of the product already * in the cart. * NOTE: The product_id is filtered by $this->add_to_cart() and holds the * id of the product translation, if one exists in the cart. */ if ($product_id != absint($_REQUEST['add-to-cart'])) { // There is a translation of the product already in the cart: // Get the language of the product in the cart $lang = pll_get_post_language($product_id); // Get the respective variation in the language of the product in the cart $variation = $this->get_variation_translation($variation_id, $lang); $variation_id = $variation->variation_id; } else { $variation = wc_get_product($variation_id); } /** * End of custom code. */ //$variation = wc_get_product( $variation_id ); // Verify all attributes foreach ($attributes as $attribute) { if (!$attribute['is_variation']) { continue; } $taxonomy = 'attribute_' . sanitize_title($attribute['name']); if (isset($_REQUEST[$taxonomy])) { // Get value from post data if ($attribute['is_taxonomy']) { // Don't use wc_clean as it destroys sanitized characters $value = sanitize_title(stripslashes($_REQUEST[$taxonomy])); /** * Custom code to check if a translation of the product is already in the cart, * and in that case, replace the variation attribute being added to the cart by * the respective translation in the language of the product already in the cart * NOTE: The product_id is filtered by $this->add_to_cart() and holds the id of * the product translation, if one exists in the cart. */ if ($product_id != absint($_REQUEST['add-to-cart'])) { // Get the translation of the term $term = get_term_by('slug', $value, $attribute['name']); $_term = get_term_by('id', pll_get_term(absint($term->term_id), $lang), $attribute['name']); if ($_term) { $value = $_term->slug; } } /** * End of custom code. */ } else { $value = wc_clean(stripslashes($_REQUEST[$taxonomy])); } // Get valid value from variation $valid_value = isset($variation->variation_data[$taxonomy]) ? $variation->variation_data[$taxonomy] : ''; // Allow if valid if ('' === $valid_value || $valid_value === $value) { $variations[$taxonomy] = $value; continue; } } else { $missing_attributes[] = wc_attribute_label($attribute['name']); } } if (!empty($missing_attributes)) { wc_add_notice(sprintf(_n('%s is a required field', '%s are required fields', sizeof($missing_attributes), 'woocommerce'), wc_format_list_of_items($missing_attributes)), 'error'); } elseif (empty($variation_id)) { wc_add_notice(__('Please choose product options…', 'woocommerce'), 'error'); } else { // Add to cart validation $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations); if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations) !== false) { wc_add_to_cart_message(array($product_id => $quantity), true); //return true; Doing an action, no return needed but we need to set $was_added_to_cart to trigger the redirect $was_added_to_cart = true; } else { $was_added_to_cart = false; } } //return false; Doing an action, no return needed but we need to set $was_added_to_cart to trigger the redirect // End: From add_to_cart_handler_variable( $product_id ) /** * Because this is a custom handler we need to take care of the rediret * to the cart. Again we use the code from add_to_cart_action( $url ) */ // From add_to_cart_action( $url ) // If we added the product to the cart we can now optionally do a redirect. if ($was_added_to_cart && wc_notice_count('error') === 0) { // If has custom URL redirect there if ($url = apply_filters('woocommerce_add_to_cart_redirect', $url)) { wp_safe_redirect($url); exit; } elseif (get_option('woocommerce_cart_redirect_after_add') === 'yes') { wp_safe_redirect(wc_get_cart_url()); exit; } } // End: From add_to_cart_action( $url ) }
public function add_to_cart_handler() { $product_id = absint($_REQUEST['add-to-cart']); $quantity = $_REQUEST['quantity']; $amount = $_REQUEST['gift_amounts']; for ($i = 0; $i < $quantity; $i++) { $new_gift_card = new YWGC_Gift_Card(); $new_gift_card->product_id = $product_id; $new_gift_card->set_amount($amount); WC()->cart->add_to_cart($product_id, 1, 0, array(), (array) $new_gift_card); } wc_add_to_cart_message($product_id); return true; /* if (WC()->cart->add_to_cart($product_id, $quantity, 0, array(), $gift_cards) !== false) { wc_add_to_cart_message($product_id); return true; } return false; */ }
/** * AJAX add to cart */ public static function add_to_cart() { ob_start(); $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['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); if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity) && 'publish' === $product_status) { do_action('woocommerce_ajax_added_to_cart', $product_id); if (get_option('woocommerce_cart_redirect_after_add') == 'yes') { wc_add_to_cart_message($product_id); } // Return fragments self::get_refreshed_fragments(); } else { $redirect = wp_get_referer() ? wp_get_referer() : home_url(); $data = array('error' => true, 'product_url' => apply_filters('woocommerce_cart_redirect_after_error', $redirect, $product_id)); // it's redirect link to current page we are on wp_send_json($data); } die; }
/** * Handler used for adding tour to the shopping card. * Used by booking form. * * @param string $url redirect url * @return void */ public function handler_add_to_cart_handler_tour($url) { $is_ajax_reply = !empty($_REQUEST['is_ajax']); $product_id = $this->get_field_value('add-to-cart', null, 0); if ($product_id) { $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($product_id)); } // tmp hack to prevent separation of the same product in shopping cart /*if ( $product_id && adventure_tours_check('is_wpml_in_use') ) { $product_id = apply_filters( 'translate_object_id', $product_id, 'product', true, apply_filters( 'wpml_default_language', '' ) ); }*/ if ($product_id < 1) { if ($is_ajax_reply) { echo wp_json_encode(array('success' => false)); exit; } return; } $product = wc_get_product($product_id); list($left_add_counter, $added_to_cart, $booking_form_validation_errors) = $this->process_add_to_cart_request($product, WC()->cart); $is_success = !empty($added_to_cart) && wc_notice_count('error') == 0 && $left_add_counter < 1; if ($is_success) { $url = apply_filters('woocommerce_add_to_cart_redirect', $url); if (!$url) { $redirect_mode = adventure_tours_get_option('tours_booking_redirect', 'checkout_page'); if ('checkout_page' == $redirect_mode) { $url = WC()->cart->get_checkout_url(); } elseif ('cart_page' == $redirect_mode || 'same_as_product' == $redirect_mode && get_option('woocommerce_cart_redirect_after_add') == 'yes') { $url = WC()->cart->get_cart_url(); } } $this->added_to_cart($product); } if ($is_ajax_reply) { $ajax_response = array('success' => $is_success); if ($is_success) { if ($url) { $ajax_response['data'] = array('redirect_url' => $url); } else { wc_add_to_cart_message($added_to_cart); } } else { $full_errors_set = $booking_form_validation_errors; $wc_notices = WC()->session->get('wc_notices', array()); $shopping_cart_errors = !empty($wc_notices['error']) ? $wc_notices['error'] : array(); if ($shopping_cart_errors) { wc_clear_notices(); $full_errors_set[$this->get_error_movement_field('quantity')] = $shopping_cart_errors; } $ajax_response['data'] = array('errors' => $full_errors_set); } echo wp_json_encode($ajax_response); exit; } elseif ($is_success) { // If has custom URL redirect there if ($url) { wp_safe_redirect($url); exit; } wc_add_to_cart_message($added_to_cart); } }
function so_27270880_add_variation_to_cart() { ob_start(); $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint($_POST['product_id'])); $quantity = empty($_POST['quantity']) ? 1 : wc_stock_amount($_POST['quantity']); $variation_id = isset($_POST['variation_id']) ? absint($_POST['variation_id']) : ''; $variations = !empty($_POST['variation']) ? (array) $_POST['variation'] : ''; $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity, $variation_id, $variations, $cart_item_data); if ($passed_validation && WC()->cart->add_to_cart($product_id, $quantity, $variation_id, $variations)) { do_action('woocommerce_ajax_added_to_cart', $product_id); if (get_option('woocommerce_cart_redirect_after_add') == 'yes') { wc_add_to_cart_message($product_id); } // Return fragments WC_AJAX::get_refreshed_fragments(); } else { // If there was an error adding to the cart, redirect to the product page to show any errors $data = array('error' => true, 'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink($product_id), $product_id)); wp_send_json($data); } die; }