/**
  * 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;
     $adding_to_cart = wc_get_product($product_id);
     if (!$adding_to_cart) {
         return;
     }
     $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) {
         $was_added_to_cart = self::add_to_cart_handler_variable($product_id);
         // Grouped Products
     } elseif ('grouped' === $add_to_cart_handler) {
         $was_added_to_cart = self::add_to_cart_handler_grouped($product_id);
         // 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);
         // Simple Products
     } else {
         $was_added_to_cart = self::add_to_cart_handler_simple($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) {
         // 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;
         }
     }
 }
 /**
  * Return data for script handles.
  * @access private
  * @param  string $handle
  * @return array|bool
  */
 private static function get_script_data($handle)
 {
     global $wp;
     switch ($handle) {
         case 'woocommerce':
             return array('ajax_url' => WC()->ajax_url(), 'wc_ajax_url' => WC_AJAX::get_endpoint("%%endpoint%%"));
             break;
         case 'wc-geolocation':
             return array('wc_ajax_url' => WC_AJAX::get_endpoint("%%endpoint%%"), 'home_url' => home_url(), 'is_available' => !(is_cart() || is_account_page() || is_checkout() || is_customize_preview()) ? '1' : '0', 'hash' => isset($_GET['v']) ? wc_clean($_GET['v']) : '');
             break;
         case 'wc-single-product':
             return array('i18n_required_rating_text' => esc_attr__('Please select a rating', 'woocommerce'), 'review_rating_required' => get_option('woocommerce_review_rating_required'));
             break;
         case 'wc-checkout':
             return array('ajax_url' => WC()->ajax_url(), 'wc_ajax_url' => WC_AJAX::get_endpoint("%%endpoint%%"), 'update_order_review_nonce' => wp_create_nonce('update-order-review'), 'apply_coupon_nonce' => wp_create_nonce('apply-coupon'), 'remove_coupon_nonce' => wp_create_nonce('remove-coupon'), 'option_guest_checkout' => get_option('woocommerce_enable_guest_checkout'), 'checkout_url' => WC_AJAX::get_endpoint("checkout"), 'is_checkout' => is_page(wc_get_page_id('checkout')) && empty($wp->query_vars['order-pay']) && !isset($wp->query_vars['order-received']) ? 1 : 0, 'debug_mode' => defined('WP_DEBUG') && WP_DEBUG, 'i18n_checkout_error' => esc_attr__('Error processing checkout. Please try again.', 'woocommerce'));
             break;
         case 'wc-address-i18n':
             return array('locale' => json_encode(WC()->countries->get_country_locale()), 'locale_fields' => json_encode(WC()->countries->get_country_locale_field_selectors()), 'i18n_required_text' => esc_attr__('required', 'woocommerce'));
             break;
         case 'wc-cart':
             return array('ajax_url' => WC()->ajax_url(), 'wc_ajax_url' => WC_AJAX::get_endpoint("%%endpoint%%"), 'update_shipping_method_nonce' => wp_create_nonce("update-shipping-method"), 'apply_coupon_nonce' => wp_create_nonce("apply-coupon"), 'remove_coupon_nonce' => wp_create_nonce("remove-coupon"));
             break;
         case 'wc-cart-fragments':
             return array('ajax_url' => WC()->ajax_url(), 'wc_ajax_url' => WC_AJAX::get_endpoint("%%endpoint%%"), 'fragment_name' => apply_filters('woocommerce_cart_fragment_name', 'wc_fragments'));
             break;
         case 'wc-add-to-cart':
             return array('ajax_url' => WC()->ajax_url(), 'wc_ajax_url' => WC_AJAX::get_endpoint("%%endpoint%%"), 'i18n_view_cart' => esc_attr__('View Cart', 'woocommerce'), 'cart_url' => apply_filters('woocommerce_add_to_cart_redirect', wc_get_cart_url()), 'is_cart' => is_cart(), 'cart_redirect_after_add' => get_option('woocommerce_cart_redirect_after_add'));
             break;
         case 'wc-add-to-cart-variation':
             // We also need the wp.template for this script :)
             wc_get_template('single-product/add-to-cart/variation.php');
             return array('wc_ajax_url' => WC_AJAX::get_endpoint("%%endpoint%%"), 'i18n_no_matching_variations_text' => esc_attr__('Sorry, no products matched your selection. Please choose a different combination.', 'woocommerce'), 'i18n_make_a_selection_text' => esc_attr__('Please select some product options before adding this product to your cart.', 'woocommerce'), 'i18n_unavailable_text' => esc_attr__('Sorry, this product is unavailable. Please choose a different combination.', 'woocommerce'));
             break;
         case 'wc-country-select':
             return array('countries' => json_encode(array_merge(WC()->countries->get_allowed_country_states(), WC()->countries->get_shipping_country_states())), 'i18n_select_state_text' => esc_attr__('Select an option…', 'woocommerce'), 'i18n_matches_1' => _x('One result is available, press enter to select it.', 'enhanced select', 'woocommerce'), 'i18n_matches_n' => _x('%qty% results are available, use up and down arrow keys to navigate.', 'enhanced select', 'woocommerce'), 'i18n_no_matches' => _x('No matches found', 'enhanced select', 'woocommerce'), 'i18n_ajax_error' => _x('Loading failed', 'enhanced select', 'woocommerce'), 'i18n_input_too_short_1' => _x('Please enter 1 or more characters', 'enhanced select', 'woocommerce'), 'i18n_input_too_short_n' => _x('Please enter %qty% or more characters', 'enhanced select', 'woocommerce'), 'i18n_input_too_long_1' => _x('Please delete 1 character', 'enhanced select', 'woocommerce'), 'i18n_input_too_long_n' => _x('Please delete %qty% characters', 'enhanced select', 'woocommerce'), 'i18n_selection_too_long_1' => _x('You can only select 1 item', 'enhanced select', 'woocommerce'), 'i18n_selection_too_long_n' => _x('You can only select %qty% items', 'enhanced select', 'woocommerce'), 'i18n_load_more' => _x('Loading more results…', 'enhanced select', 'woocommerce'), 'i18n_searching' => _x('Searching…', 'enhanced select', 'woocommerce'));
             break;
         case 'wc-password-strength-meter':
             return array('min_password_strength' => apply_filters('woocommerce_min_password_strength', 3), 'i18n_password_error' => esc_attr__('Please enter a stronger password.', 'woocommerce'), 'i18n_password_hint' => esc_attr__('The password should be at least seven characters long. To make it stronger, use upper and lower case letters, numbers and symbols like ! " ? $ % ^ & ).', 'woocommerce'));
             break;
     }
     return false;
 }
 /**
  * Add a product to the cart.
  *
  * @param int $product_id contains the id of the product to add to the cart
  * @param int $quantity contains the quantity of the item to add
  * @param int $variation_id
  * @param array $variation attribute values
  * @param array $cart_item_data extra cart item data we want to pass into the item
  * @return string|bool $cart_item_key
  */
 public function add_to_cart($product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array())
 {
     // Wrap in try catch so plugins can throw an exception to prevent adding to cart
     try {
         $product_id = absint($product_id);
         $variation_id = absint($variation_id);
         // Ensure we don't add a variation to the cart directly by variation ID
         if ('product_variation' == get_post_type($product_id)) {
             $variation_id = $product_id;
             $product_id = wp_get_post_parent_id($variation_id);
         }
         // Get the product
         $product_data = wc_get_product($variation_id ? $variation_id : $product_id);
         // Sanity check
         if ($quantity <= 0 || !$product_data || 'trash' === $product_data->post->post_status) {
             return false;
         }
         // Load cart item data - may be added by other plugins
         $cart_item_data = (array) apply_filters('woocommerce_add_cart_item_data', $cart_item_data, $product_id, $variation_id);
         // Generate a ID based on product ID, variation ID, variation data, and other cart item data
         $cart_id = $this->generate_cart_id($product_id, $variation_id, $variation, $cart_item_data);
         // Find the cart item key in the existing cart
         $cart_item_key = $this->find_product_in_cart($cart_id);
         // Force quantity to 1 if sold individually and check for existing item in cart
         if ($product_data->is_sold_individually()) {
             $quantity = apply_filters('woocommerce_add_to_cart_sold_individually_quantity', 1, $quantity, $product_id, $variation_id, $cart_item_data);
             $in_cart_quantity = $cart_item_key ? $this->cart_contents[$cart_item_key]['quantity'] : 0;
             if ($in_cart_quantity > 0) {
                 throw new Exception(sprintf('<a href="%s" class="button wc-forward">%s</a> %s', wc_get_cart_url(), __('View Cart', 'woocommerce'), sprintf(__('You cannot add another &quot;%s&quot; to your cart.', 'woocommerce'), $product_data->get_title())));
             }
         }
         // Check product is_purchasable
         if (!$product_data->is_purchasable()) {
             throw new Exception(__('Sorry, this product cannot be purchased.', 'woocommerce'));
         }
         // Stock check - only check if we're managing stock and backorders are not allowed
         if (!$product_data->is_in_stock()) {
             throw new Exception(sprintf(__('You cannot add &quot;%s&quot; to the cart because the product is out of stock.', 'woocommerce'), $product_data->get_title()));
         }
         if (!$product_data->has_enough_stock($quantity)) {
             throw new Exception(sprintf(__('You cannot add that amount of &quot;%s&quot; to the cart because there is not enough stock (%s remaining).', 'woocommerce'), $product_data->get_title(), $product_data->get_stock_quantity()));
         }
         // Stock check - this time accounting for whats already in-cart
         if ($managing_stock = $product_data->managing_stock()) {
             $products_qty_in_cart = $this->get_cart_item_quantities();
             if ($product_data->is_type('variation') && true === $managing_stock) {
                 $check_qty = isset($products_qty_in_cart[$variation_id]) ? $products_qty_in_cart[$variation_id] : 0;
             } else {
                 $check_qty = isset($products_qty_in_cart[$product_id]) ? $products_qty_in_cart[$product_id] : 0;
             }
             /**
              * Check stock based on all items in the cart.
              */
             if (!$product_data->has_enough_stock($check_qty + $quantity)) {
                 throw new Exception(sprintf('<a href="%s" class="button wc-forward">%s</a> %s', wc_get_cart_url(), __('View Cart', 'woocommerce'), sprintf(__('You cannot add that amount to the cart &mdash; we have %s in stock and you already have %s in your cart.', 'woocommerce'), $product_data->get_stock_quantity(), $check_qty)));
             }
         }
         // If cart_item_key is set, the item is already in the cart
         if ($cart_item_key) {
             $new_quantity = $quantity + $this->cart_contents[$cart_item_key]['quantity'];
             $this->set_quantity($cart_item_key, $new_quantity, false);
         } else {
             $cart_item_key = $cart_id;
             // Add item after merging with $cart_item_data - hook to allow plugins to modify cart item
             $this->cart_contents[$cart_item_key] = apply_filters('woocommerce_add_cart_item', array_merge($cart_item_data, array('product_id' => $product_id, 'variation_id' => $variation_id, 'variation' => $variation, 'quantity' => $quantity, 'data' => $product_data)), $cart_item_key);
         }
         if (did_action('wp')) {
             $this->set_cart_cookies(!$this->is_empty());
         }
         do_action('woocommerce_add_to_cart', $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data);
         return $cart_item_key;
     } catch (Exception $e) {
         if ($e->getMessage()) {
             wc_add_notice($e->getMessage(), 'error');
         }
         return false;
     }
 }
Exemple #4
0
 /**
  * Test get_cart_url method.
  *
  * @since 2.5.0
  */
 public function test_wc_get_cart_url()
 {
     $cart_page_url = wc_get_page_permalink('cart');
     $this->assertEquals(apply_filters('woocommerce_get_cart_url', $cart_page_url ? $cart_page_url : ''), wc_get_cart_url());
 }
 *
 * @see     http://docs.woothemes.com/document/template-structure/
 * @author  WooThemes
 * @package WooCommerce/Templates
 * @version 2.3.8
 */
if (!defined('ABSPATH')) {
    exit;
    // Exit if accessed directly
}
wc_print_notices();
do_action('woocommerce_before_cart');
?>

<form action="<?php 
echo esc_url(wc_get_cart_url());
?>
" method="post">

<?php 
do_action('woocommerce_before_cart_table');
?>

<table class="shop_table shop_table_responsive cart" cellspacing="0">
	<tr>
		<td>Product</td>
		<td></td>
		<td>Price</td>
	</tr>

		<?php 
/**
 * Get a coupon value.
 *
 * @access public
 * @param string $coupon
 */
function wc_cart_totals_coupon_html($coupon)
{
    if (is_string($coupon)) {
        $coupon = new WC_Coupon($coupon);
    }
    $value = array();
    if ($amount = WC()->cart->get_coupon_discount_amount($coupon->code, WC()->cart->display_cart_ex_tax)) {
        $discount_html = '-' . wc_price($amount);
    } else {
        $discount_html = '';
    }
    $value[] = apply_filters('woocommerce_coupon_discount_amount_html', $discount_html, $coupon);
    if ($coupon->enable_free_shipping()) {
        $value[] = __('Free shipping coupon', 'woocommerce');
    }
    // get rid of empty array elements
    $value = array_filter($value);
    $value = implode(', ', $value) . ' <a href="' . esc_url(add_query_arg('remove_coupon', urlencode($coupon->code), defined('WOOCOMMERCE_CHECKOUT') ? wc_get_checkout_url() : wc_get_cart_url())) . '" class="woocommerce-remove-coupon" data-coupon="' . esc_attr($coupon->code) . '">' . __('[Remove]', 'woocommerce') . '</a>';
    echo apply_filters('woocommerce_cart_totals_coupon_html', $value, $coupon);
}
 /**
  * Create the button to go to the product page for embedded products.
  *
  * @since 2.4.11
  * @return string
  */
 public static function product_buttons()
 {
     $_product = wc_get_product(get_the_ID());
     $buttons = array();
     $button = '<a href="%s" class="wp-embed-more wc-embed-button">%s</a>';
     if ($_product->is_type('simple') && $_product->is_purchasable() && $_product->is_in_stock()) {
         $buttons[] = sprintf($button, esc_url(add_query_arg('add-to-cart', get_the_ID(), wc_get_cart_url())), esc_html__('Buy now', 'woocommerce'));
     }
     $buttons[] = sprintf($button, get_the_permalink(), esc_html__('Read more', 'woocommerce'));
     return '<p>' . implode(' ', $buttons) . '</p>';
 }
 /**
  * redirect after add to cart from YITH WooCommerce Frequently Bought Together Premium shortcode
  *
  * @since 1.0.0
  */
 public function yith_wfbt_redirect_after_add_to_cart($url)
 {
     if (!isset($_REQUEST['yith_wfbt_shortcode'])) {
         return $url;
     }
     $cart_url = function_exists('wc_get_cart_url') ? wc_get_cart_url() : WC()->cart->get_cart_url();
     return get_option('yith_wcwl_redirect_cart') == 'yes' ? $cart_url : $this->get_wishlist_url();
 }
 function redirect_to_checkout()
 {
     return wc_get_cart_url();
 }
 /**
  * Output the proceed to checkout button.
  *
  * @subpackage	Cart
  */
 function woocommerce_widget_shopping_cart_button_view_cart()
 {
     echo '<a href="' . esc_url(wc_get_cart_url()) . '" class="button wc-forward">' . __('View cart', 'woocommerce') . '</a>';
 }
 * @author 		WooThemes
 * @package 	WooCommerce/Templates
 * @version     2.0.8
 */
if (!defined('ABSPATH')) {
    exit;
    // Exit if accessed directly
}
if ('no' === get_option('woocommerce_enable_shipping_calc') || !WC()->cart->needs_shipping()) {
    return;
}
$porto_woo_version = porto_get_woo_version_number();
?>

<form class="woocommerce-shipping-calculator shipping_calculator" action="<?php 
echo esc_url(version_compare($porto_woo_version, '2.5', '<') ? WC()->cart->get_cart_url() : wc_get_cart_url());
?>
" method="post">

    <?php 
do_action('woocommerce_before_shipping_calculator');
?>

    <?php 
if (version_compare($porto_woo_version, '2.3', '<')) {
    ?>
        <h2><a href="#" class="shipping-calculator-button"><?php 
    _e('Calculate Shipping', 'woocommerce');
    ?>
</a></h2>
    <?php 
Exemple #12
0
 /**
  * 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&hellip;', '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 )
 }