Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

commented Feb 12, 2017

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

This comment has been minimized.

Copy link
Owner Author

commented Feb 16, 2017

code updated :)

thanks @perrysessions

@redlaw03

This comment has been minimized.

Copy link

commented Jul 23, 2018

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
You can’t perform that action at this time.