Okay: I’ve actually solved Bug #1. Here are my solutions which must be run in a child theme or a code snippet plugin.
On the cart page, the following code will fix the issue with the customer being able to add stock quantities greater than what the chosen location has. It inserts jQuery into the cart page to add validation that checks against the serialized data in the DB.
add_action( 'woocommerce_check_cart_items', 'limit_cart_to_single_location' );
function limit_cart_to_single_location() {
$locations = array();
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$location = isset($cart_item['_stock_location']) ? $cart_item['_stock_location'] : null;
if ($location) {
$locations[$location] = true;
}
}
if (count($locations) > 1) {
wc_add_notice( __( 'You can only checkout with items from one store location per transaction. Please remove any items from additional locations from your cart. If you need it all on one order, give us a call instead!', 'woocommerce' ), 'error' );
}
}add_action( 'wp_footer', 'limit_cart_quantity_input_field' );
function limit_cart_quantity_input_field() {
if ( is_cart() ) {
?>
<script type="text/javascript">
jQuery(function($) {
function limitQuantity() {
<?php foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) :
$product_id = $cart_item['product_id'];
$location = isset($cart_item['_stock_location']) ? $cart_item['_stock_location'] : null;
$location_stocks = get_post_meta($product_id, 'smifw_location_stocks', true);
$stock_at_location = 0;
if ($location && $location_stocks) {
$location_stocks = maybe_unserialize($location_stocks);
foreach ($location_stocks as $store => $store_data) {
if ($store == $location) {
$stock_at_location = $store_data['stock'];
}
}
}
?>
$('input[name="cart[<?php echo $cart_item_key; ?>][qty]"]').attr('max', <?php echo $stock_at_location; ?>);
<?php endforeach; ?>
}
$(document).ready(limitQuantity);
$(document).ajaxComplete(limitQuantity);
});
</script>
<?php
}
}
}
}
In addition, run this code as a fallback, as it will automatically adjust the cart QTY if somehow the customer manages to get the QTY above what the location has.
add_action( 'woocommerce_update_cart_action_cart_updated', 'check_cart_quantities', 20, 1 );
function check_cart_quantities( $cart_updated ) {
if (! $cart_updated) {
return;
}
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item) {
$product_id = $cart_item['product_id']; // Get the product id
$quantity = $cart_item['quantity']; // Get the quantity
$location = isset($cart_item['_stock_location']) ? $cart_item['_stock_location'] : null; // Get the location
if (!$location) {
continue;
}
$location_stocks = get_post_meta($product_id, 'smifw_location_stocks', true);
if ($location_stocks) {
$location_stocks = maybe_unserialize($location_stocks);
foreach ($location_stocks as $store => $store_data) {
if ($store == $location) {
$stock_at_location = $store_data['stock'];
if ($quantity > $stock_at_location) {
WC()->cart->set_quantity($cart_item_key, $stock_at_location);
wc_add_notice( __( "Your selected location doesn't have that much stock. We've adjusted the quantity of your cart items according to your selected location's stock limitations.", 'woocommerce' ), 'error' );
}
}
}
}
}
}
Here was something else I needed. This code disallows customers to purchase items from more than a single store location in one transaction, use the following code:
add_action( 'woocommerce_check_cart_items', 'limit_cart_to_single_location' );
function limit_cart_to_single_location() {
$locations = array();
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$location = isset($cart_item['_stock_location']) ? $cart_item['_stock_location'] : null;
if ($location) {
$locations[$location] = true;
}
}
if (count($locations) > 1) {
wc_add_notice( __( 'You can only checkout with items from one store location per transaction. Please remove any items from additional locations from your cart.', 'woocommerce' ), 'error' );
}
}
@yourwcninja – Feel free to add this to the next release. Just don’t break my existing setup if you do! ??
We still need a solution for Bug #2, or the buggy backend stuff.
@andresuba – If you need any of this, here it is.
-
This reply was modified 1 year, 5 months ago by mattvlc.