• I have identified two bugs that need to be fixed for this plugin to be usable:

    • Bug 1: Let’s say you have two stock locations, and both have 5 units of stock of an item. If I add the product to the cart for one location, within the cart screen, I am able to increase the purchase amount up to 10 units which SHOULD NOT happen. It should instead cap at 5 units. The error in the plugin logic is that it is not limiting the purchase amount to the number of units available at the chosen location, and instead, allows you to purchase the TOTAL stock across all locations, even if only one location is selected.
    • Bug 2: On the backend product edit screen in WooCommerce, there is an additional custom field added to the right hand sidebar titled “Stock Locations”. However, this just flat out doesn’t work properly. The item will still show the location name in the select dropdown on the front end, even if that location shouldn’t be showing.

    The Bigger Problem: In addition, because the “smifw_location_stocks” field in the database is stored as serialized data, it’s extremely hard to import products using a tool like WP All Import and properly map the stock units or price for each location upon import. In fact, I have been trying to do this for 3 hours and have not had any success. Can you please suggest a way to work around this so we can import products? The shops I’d like to work with have thousands of products and they cannot be modified manually.

    I will legitimately pay you to fix these issues and make this plugin usable, because it is the only viable solution on the open plugin market for this need that actually works (almost). Email me if needed with a price and timeframe, as my company could really use this plugin and have it working.

    Also: The plugin code still has all the boilerplate and placeholder text. The plugin description needs to be edited, as it still displays the default plugin boilerplate on the plugin screen.

    • This topic was modified 1 year, 5 months ago by mattvlc.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter mattvlc

    (@mattvlc)

    Hi, any follow up on this?

    Hi @mattvlc, do you still ned help with the All Import problem because I was having the same problem and got a solution.

    Do you think you can help me with this problems (https://www.ads-software.com/support/topic/problems-with-add-to-cart-buttons/)

    Thread Starter mattvlc

    (@mattvlc)

    @andresuba I have not experienced the issue with the add to cart button, as mine works as expected except for not having the add to cart cap properly implemented like I mentioned in my post. Sorry!

    As for the filtering, I also can’t help with that since I am just going to filter by the default price.

    If you don’t mind sharing, what approach did you take to WP All Import?

    @mattvlc But in your category page, for example, if you add a product to the cart it adds the product with a location selected?

    For Wp All Import:
    In Custom fields add the field “smifw_location_stocks” with the field option “Serialized”.
    Then in “Click to Specify”, the “key” will be the slug to location you want to import the stock/price .
    In the value you will call the function “[stock({price[1]},sale_price[1]},{stock[1]})]” where the price and stock come from the file
    Then in the function editor add this function:

    function stock( $price = null, $sale_price = null, $stock = 0) {
    $array[“stock”] = $stock;
    $array[“regular_price”] = $price;
    $array[“sale_price”] = $sale_price;
    $str = serialize($array);
    return $str;
    }”

    Thread Starter mattvlc

    (@mattvlc)

    @andresuba Ah, I understand what you’re saying. I didn’t understand at first. You’re saying that you have an “add to cart” button on the product listings within the product archive page, not just the single product page, and that adding the product to the cart from the archive page causes issues with the location binding.

    I don’t run into that issue because I remove the “add to cart” button from the product listing cards/loop items on the product archive page because my website design did not require these buttons there. I don’t think there will be a workaround in your case– you will need to remove the buttons from the listings on the product archive pages and just leave them on the single product pages. This is actually how it works on most enterprise e-commerce stores as well for this exact same reason– they need to fill out additional fields on the single product page in order to add the product to the cart. In our case, the customer needs to select a location before adding to the cart.

    So, I think your best option is to remove the “add to cart” buttons on the product archive listings and only have them on the single product page. I hope that helps.

    Thank you for the help with the WP All Import. I will try that and see how it works.

    Thread Starter mattvlc

    (@mattvlc)

    @yourwcninja — Can you follow up on the bugs?

    Thread Starter mattvlc

    (@mattvlc)

    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.
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Bugs & Dealing With the Serialized Data Storage Upon Product Import’ is closed to new replies.