Page 1 of 1

Hide/Replace Buy Button on product_info

Posted: Sun Oct 24, 2021 11:46 am
by heatherbell
On 1.0.8.6
In tpl_pi_buy_button.php I had code that worked to only show buy button when stock>0 else show a different button like:

Code: Select all

<?php 
     if ($product_info['products_quantity'] > 0) {
  echo tep_draw_button(PI_BUY_BUTTON_TEXT, 'fas fa-shopping-basket', null, 'primary', array('params' => 'data-has-attributes="' . (($products_attributes['total'] > 0) ? '1' : '0') . '" data-in-stock="' . (int)$product_info['products_quantity'] . '" data-product-id="' . (int)$product_info['products_id'] . '"'), 'btn-success btn-block btn-lg btn-product-info btn-buy'); 
  echo tep_draw_hidden_field('products_id', (int)$product_info['products_id']);
} else {
  echo tep_draw_button(PI_BUY_BUTTON_UNAVAILABLE, 'fas fa-ban', null, 'primary', array('params' => 'disabled="disabled"'), 'btn-danger btn-block btn-lg');
  }
  ?>
</div>

<?php
On latest 1.0.8.7 this gives notices - it does not like $product_info.
I tried replacing the code with:

Code: Select all

<?php
  if ((int)$GLOBALS['product']->get('in_stock') > 0) {
    new Button(PI_BUY_BUTTON_TEXT, 'fas fa-shopping-cart', 'btn-success btn-block btn-lg btn-product-info btn-buy', [
      'data-has-attributes' => (int)$GLOBALS['product']->get('has_attributes'),
      'data-in-stock' => (int)$GLOBALS['product']->get('in_stock'),
      'data-product-id' => (int)$GLOBALS['product']->get('id'),
    ]);
    new Input('products_id', ['value' => (int)$GLOBALS['product']->get('id')], 'hidden');
  } else {
    new Button(PI_BUY_BUTTON_UNAVAILABLE, 'fas fa-ban', 'btn-danger btn-block btn-lg', [
      'disabled="disabled"'
    ]);
  }
  ?>
This does not give notices but shows no button at all even if stock>0
How should this be done in 1.0.8.7?

Re: Hide/Replace Buy Button on product_info

Posted: Sun Oct 24, 2021 12:34 pm
by ecartz

Code: Select all

<?= ((int)$GLOBALS['product']->get('in_stock') > 0)
  ? new Button(PI_BUY_BUTTON_TEXT, 'fas fa-shopping-cart', 'btn-success btn-block btn-lg btn-product-info btn-buy', [
      'data-has-attributes' => (int)$GLOBALS['product']->get('has_attributes'),
      'data-in-stock' => (int)$GLOBALS['product']->get('in_stock'),
      'data-product-id' => (int)$GLOBALS['product']->get('id'),
    ])
    . new Input('products_id', ['value' => (int)$GLOBALS['product']->get('id')], 'hidden')
  : new Button(PI_BUY_BUTTON_UNAVAILABLE, 'fas fa-ban', 'btn-danger btn-block btn-lg', [
      'disabled' => 'disabled'
    ])
  ?>
The specific thing that was missing was that you weren't echoing the button. Also made minor tweaks for consistency.

Re: Hide/Replace Buy Button on product_info

Posted: Sun Oct 24, 2021 12:43 pm
by heatherbell
ecartz wrote: Sun Oct 24, 2021 12:34 pm . new Input('products_id', ['value' => (int)$GLOBALS['product']->get('id')], 'hidden');
Many thanks for that but it gives Parse error: syntax error, unexpected ';' for the line quoted above.
Confused by the disappearance of if{}else{} so I guess something in that code replaces that logic but don't know what.

Re: Hide/Replace Buy Button on product_info

Posted: Sun Oct 24, 2021 1:02 pm
by ecartz
Just remove the ; from that line.

Because I switched from the long tags to the short echo tags, it won't allow if/else there, just things that return a value. So I also switched to the ?: which returns a value.

Code: Select all

true_false_condition ? when_true : when_false
It would also work to add echo statements to your original code (plus change the disabled parameter).

Re: Hide/Replace Buy Button on product_info

Posted: Sun Oct 24, 2021 1:13 pm
by heatherbell
ecartz wrote: Sun Oct 24, 2021 1:02 pm Just remove the ; from that line.
Works as expected now - thanks again.
ecartz wrote: Sun Oct 24, 2021 1:02 pm Because I switched from the long tags to the short echo tags, it won't allow if/else there, just things that return a value. So I also switched to the ?: which returns a value.

Code: Select all

true_false_condition ? when_true : when_false
It would also work to add echo statements to your original code (plus change the disabled parameter).
Many thanks for that, it explains a lot. Another step towards enlightenment! :D