Adding Min Order QTY to tpl_pi_qty_input.php

Open to all! Ask other shopowners for help.
Post Reply
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Adding Min Order QTY to tpl_pi_qty_input.php

Post by Portman »

Hi
I have added a minimum order qty field to my products database products_min_order_qty

I am trying to modify tpl_pi_qty_input.php to force the user to purchase no less than this number....

this is what I have done;

Code: Select all

<?php
		    $products_id = Text::input($_GET['pID'] ?? '');
			$grab_val = $GLOBALS['db']->query("SELECT products_min_order_qty FROM products WHERE products_id = " . (int)$products_id)->fetch_assoc();
			$product_min_order_qty = $grab_val['products_min_order_qty'];
			
			if ($product_min_order_qty == 0) {
				$min_qty = 1; 
			}	else {
				$min_qty = $product_min_order_qty;
			}
  ?>

<div class="col-sm-<?= $content_width; ?> text-right mt-2 pi-qty-input">

  <div class="input-group mb-2">
    <div class="input-group-prepend">
      <?php
      if (PI_QTY_INPUT_BUTTONS == 'True') {
        ?>
        <button class="btn btn-info spinner" type="button" data-spin="minus">
          <i class="fa fa-minus"></i>
        </button>
        <?php
      }
      else {
        ?>
          <div class="input-group-text"><?= PI_QTY_INPUT_BUTTON_TEXT; ?></div>
        <?php
      }
      ?>
    </div>
    <?php
    echo tep_draw_input_field('qty', $min_qty, 'min="1"', 'number', true, 'class="form-control form-control-lg" id="pi-qty-input"');
    
    if (PI_QTY_INPUT_BUTTONS == 'True') {
    ?>
    <div class="input-group-append">
      <button class="btn btn-info spinner" type="button" data-spin="plus">
        <i class="fa fa-plus"></i>
      </button>
    </div>
      <?php
    }
    ?>
  </div>

</div>
the bit that I added at the top does not read the value of products_min_order_qty... what have I done wrong here? do I have to define the variables in the main file rather than the tempalte file?(the if statement is a bit wierd I know but thats only because I set the default value to 0)

ALSO
in the input field, how do I change around the 'min="1"' bit so that it reflects $min_qty ? I've tried multiple things but cant get it to work


Join The Code Co-op to get access to your library in the Code Co-op Forum
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Adding Min Order QTY to tpl_pi_qty_input.php

Post by ecartz »

1. What is $_GET['pID'] when it doesn't work? Given the code that you have, this is the first question you should ask.
2. If you're using a modern version, you could just use the existing $GLOBALS['product']->get('min_order_qty') -- you don't have to load it separately.
3. This would run on the product_info.php page, where you have (int)$_GET['products_id'] not pID.
4. Modern version again:

Code: Select all

    $min_qty = $GLOBALS['product']->get('min_order_qty') ?? '1';
    echo (new Input('qty', ['min' => "$min_qty", 'class' => 'form-control form-control-lg', 'id' => 'pi-qty-input'], 'number'))->default_value("$min_qty");
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: Adding Min Order QTY to tpl_pi_qty_input.php

Post by Portman »

Thank you so much for this ... I realised that although I had updated the pi modules I was using an outdated one in my override folder ... stupid mistake.

Can I ask for a bit of guidance on how to put something similar in the shopping cart? I assume I would be using a hook, but to be honest with you, no matter how hard I try I can't get my head around which hook I use where...

Obviously I want to stop the customer from reducing the qty of an item below the minimum quantity amount in the checkout product listing... with the same sort of warning that you get on pi_qty_input.php

Would I be using afterStart or startCheckout to controll this? or am I totally on the wrong track?
ecartz
Core Team
Posts: 3084
Joined: Tue Nov 05, 2019 6:02 pm
Phoenix Version:
Has thanked: 4 times
Been thanked: 208 times

Re: Adding Min Order QTY to tpl_pi_qty_input.php

Post by ecartz »

The quantity box is in the template.

https://github.com/CE-PhoenixCart/Phoen ... ng.php#L44

The checkout hooks are on the pages checkout*.php
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: Adding Min Order QTY to tpl_pi_qty_input.php

Post by heatherbell »

Portman wrote: Tue Sep 24, 2024 2:06 am Can I ask for a bit of guidance on how to put something similar in the shopping cart?
As above, override the tpl_ file.
Need to override /includes/modules/navbar/templates/tpl_nb_shopping_cart.php too.
We did this: app.php/addons/paid_addon/minimum_order ... r_product/
User avatar
Portman
Contributor
Posts: 158
Joined: Mon Mar 08, 2021 1:04 am
Phoenix Version: v1.0.8.20
Has thanked: 29 times
Been thanked: 4 times

Re: Adding Min Order QTY to tpl_pi_qty_input.php

Post by Portman »

Thanks for your help... I have it working through modifying the template page and saving it in the override folder, but to be honest with you I really can't get my head around the hooks thing...

Can you give me any guidance on how to understand the hooks? as in is there documentation or a tutorial anywhere? I have looked at a youtube video by preston lord https://www.youtube.com/watch?v=bLd9m_uABYI&t=184s
which was great for sitewide things but if you are just controlling the nitty gritty of one line of text like this I get a bit lost
heatherbell
Senior Contributor
Posts: 2540
Joined: Mon Oct 07, 2019 4:39 am
Phoenix Version:
Has thanked: 35 times
Been thanked: 243 times

Re: Adding Min Order QTY to tpl_pi_qty_input.php

Post by heatherbell »

Portman wrote: Tue Sep 24, 2024 6:48 amjust controlling the nitty gritty of one line of text like this
Hooks aren't used for that, for tpl_ files use tpl_override.
I wrote simple guide in User Guide: https://phoenixcart.org/phoenixcartwiki ... a_New_Hook


Join The Code Co-op to get access to your library in the Code Co-op Forum
Post Reply