Last active
July 28, 2016 21:32
-
-
Save helgatheviking/25e57fe033d719059dcf994ffe33d718 to your computer and use it in GitHub Desktop.
Force NYP products to respect "sold individually" regardless of price
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: WooCommerce Name Your Price Sold Individually | |
* Plugin URI: https://gist.github.com/helgatheviking/25e57fe033d719059dcf994ffe33d718 | |
* Description: Double check enforcement of "Sold Individually" for NYP items | |
* Version: 1.0.0 | |
* Author: Kathy Darling | |
* Author URI: http://kathyisawesome.com/ | |
* | |
* Copyright: © 2016 Kathy Darling | |
* License: GNU General Public License v3.0 | |
* License URI: http://www.gnu.org/licenses/gpl-3.0.html | |
*/ | |
add_filter( 'woocommerce_add_to_cart_validation', 'wc_nyp_sold_individually_double_check', 5, 20 ); | |
function wc_nyp_sold_individually_double_check( $passed, $product_id, $quantity, $variation_id = '', $variations= '' ) { | |
// if it has already failed no need to keep checking | |
if( $passed === false ){ | |
return $passed; | |
} | |
// Get the product | |
$product_data = wc_get_product( $variation_id ? $variation_id : $product_id ); | |
// double check against the cart contents | |
if ( $product_data->is_sold_individually() ) { | |
foreach ( WC()->cart->get_cart() as $cart_item ){ | |
if ( $product_id == $cart_item['product_id'] || ( $variation_id && $variation_id == $cart_item['variation_id'] ) ) { | |
$passed = false; | |
wc_add_notice( sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', WC()->cart->get_cart_url(), __( 'View Cart', 'woocommerce' ), sprintf( __( 'You cannot add another "%s" to your cart.', 'woocommerce' ), $product_data->get_title() ) ), 'error' ); | |
} | |
} | |
} | |
return $passed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment