Forum Replies Created

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter mattvlc

    (@mattvlc)

    I came up with a solution via ChatGPT, just add this if you want. Other people– here is a solution. Just run this with the plugin in standalone mode if you like.

    // Hook into the admin_menu action with a high priority to ensure it runs after Fluent Snippets adds its menu
    add_action('admin_menu', 'move_fluent_snippets_menu', 999);

    /**
    * Moves Fluent Snippets menu under Tools submenu if the plugin is active.
    */
    function move_fluent_snippets_menu() {
    // Check if the Fluent Snippets plugin is active by verifying the existence of a key class
    if (!class_exists('FluentSnippets\App\Hooks\Handlers\AdminMenuHandler')) {
    return; // Exit if the Fluent Snippets plugin is not active
    }

    // Define the Fluent Snippets menu slug
    $fluent_snippets_slug = 'fluent-snippets';

    // Remove the top-level Fluent Snippets menu
    remove_menu_page($fluent_snippets_slug);

    // Add Fluent Snippets as a submenu under Tools
    add_submenu_page(
    'tools.php', // Parent slug (Tools menu)
    __('Fluent Snippets', 'easy-code-manager'), // Page title
    __('Fluent Snippets', 'easy-code-manager'), // Menu title
    'install_plugins', // Capability (matches the original)
    $fluent_snippets_slug // Menu slug (same as original)
    // No callback needed as the plugin already handles it
    );
    }
    Thread Starter mattvlc

    (@mattvlc)

    @fifco2000 There is no solution. ASE custom fields do not support WP All Import. Use ACF or similar plugin.

    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.
    Thread Starter mattvlc

    (@mattvlc)

    @yourwcninja — Can you follow up on the bugs?

    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)

    @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?

    Thread Starter mattvlc

    (@mattvlc)

    Hi, any follow up on this?

Viewing 7 replies - 1 through 7 (of 7 total)