Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smeric/6e64c2e3a6540aa552c4 to your computer and use it in GitHub Desktop.
Save smeric/6e64c2e3a6540aa552c4 to your computer and use it in GitHub Desktop.
Made to be used with the "Smart Coupons" WooCommerce extention. If we apply coupon from url and the coupon is dedicated to a specific product, the coupon is not applied if the product is not already in cart. So, before applying the coupon we add this product to cart.
<?php
/**
* Add to cart before applying coupon
*
* Made to be used with the "Smart Coupons" WooCommerce extention.
* If we apply coupon from url and the coupon is dedicated to a specific product, the coupon is not applied if
* the product is not already in cart. So, before applying the coupon we add this product to cart.
*
* @link
* @version 1.0.1
* @package woocommerce_add_to_cart_before_applying_coupon
*
* @wordpress-plugin
* Plugin Name: Add to cart before applying coupon
* Plugin URI:
* Description: If we apply coupon from url and the coupon is dedicated to a specific product, the coupon is not applied if the product is not already in cart. So, before applying the coupon we add this product to cart.
* Version: 1.0.1
* Author: Sébastien Méric <sebastien.meric@gmail.com>
* Author URI: http://www.sebastien-meric.com/
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: woocommerce_add_to_cart_before_applying_coupon
* Domain Path: /languages
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write
* to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
**/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Add a product to cart so applying a coupon on it from url is possible.
*
* @since 1.0.0
* @package woocommerce_add_to_cart_before_applying_coupon
* @author Sébastien Méric <sebastien.meric@gmail.com>
**/
add_action( 'wp_loaded', 'woocommerce_add_product_to_cart_on_coupon_from_url' );
function woocommerce_add_product_to_cart_on_coupon_from_url() {
global $woocommerce;
// Made to be used with the "Smart Coupons" WooCommerce extention.
if ( ! class_exists( 'WC_Smart_Coupons' ) ) {
return;
}
// Bail early if $woocommerce is empty, or there is no coupons passed, or an empty coupons has been passed.
if ( empty( $woocommerce ) || ! isset( $_GET['coupon-code'] ) || empty( $_GET['coupon-code'] ) ) {
return;
}
// Get the coupon code.
$coupon_code = $_GET['coupon-code'];
// Get the coupon id or do nothing.
if ( ! ( $coupon_id = woocommerce_get_coupon_id_from_code( $coupon_code ) ) ) {
return;
}
// Get the first associated product with this coupon code or do nothing.
if ( ! ( $product_id = intval( get_post_meta( $coupon_id, 'product_ids', true ) ) ) ) {
return;
}
// Do nothing if product is already in cart.
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
if ( $product_id === intval( $_product->id ) ) {
return;
}
}
// If product is not already in cart, then add it !
$woocommerce->cart->add_to_cart( $product_id, 1 );
}
/**
* Get a coupon ID from it's code
*
* @since 1.0.0
* @package woocommerce_add_to_cart_before_applying_coupon
* @param string $code
* @return int
*/
function woocommerce_get_coupon_id_from_code( $code ) {
global $wpdb;
return intval( $wpdb->get_var( $wpdb->prepare( apply_filters( 'woocommerce_coupon_code_query', "SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'shop_coupon' AND post_status = 'publish'" ), $code ) ) );
}
?>
@perrysessions
Copy link

When i added this to my function.php I had to delete one of the ")" on line 47. Other than that it works great! Thank you for posting!

@smeric
Copy link
Author

smeric commented Feb 16, 2017

code updated :)

thanks @perrysessions

@redlaw03
Copy link

I searched long and hard and almost tried some desperate moves. But this code worked perfectly. Thank you very much, exactly what I needed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment