replace add to cart button

thoriax's picture
thoriax
Sat, 04/30/2016 - 14:20
#1
replace add to cart button

Hi,

Currently the add to cart button is always shown. Even when all items are out of stock.

Image title

Currently this is hardcoded:

<?php
 $product = commerce_product_load((int)$form['product_id']['#value']);
 $query = new EntityFieldQuery;
 $result = $query->entityCondition('entity_type', 'node', '=')
->propertyCondition('type', 'product')
->fieldCondition('field_product_ref', 'product_id', (int)$form['product_id']['#value'], '=')
->range(0, 1)->execute();
  
 $nids = array();
 foreach ($result['node'] as $node) {
$nids[] = $node->nid;
 }
 $node = node_load((int)$nids[0]);
?>
<?php if (isset($form['#is_product_page']) && $form['#is_product_page']): ?>
  <div class="add-to-box clearfix">
    <div class="input-content">
      <label for="qty"><?php print t('QTY'); ?>:</label>
      <div class="qty-box">
        <button class="qty-decrease">         <?php print render($form['quantity']) ?>
        <button class="qty-increase">       </div>
    </div>
    <div class="add-to-cart">
      <div class="js-hide"><?php print render($form['submit']) ?></div>
      <a href="#" class="btn btn-3 text-uppercase"><i class="fa fa-cart-plus"></i> <span><?php print t('Add To Cart'); ?></span></a>
    </div>
  </div>
<?php endif; ?>

<?php print drupal_render_children($form); ?>

Is there a way to replace the add to cart button with out of stock message? 

And possible hide the qty-plus and qty-minus buttons.

Thanks for feedback.

Regards

khoa's picture
khoa
Sun, 05/01/2016 - 04:33

Hello,

1.Is there a way to replace the add to cart button with out of stock message?
=> Open node--product.tpl.php file(sites/all/themes/md_orenmode/template/node), and replace

<?php print render($content['field_product_ref']); ?>

by

<?php if (isset($content['field_product_available']['#items'][0]['value']) && $content['field_product_available']['#items'][0]['value']): ?>     <?php print render($content['field_product_ref']); ?><?php else : ?>
YOUR_OUT_OF_STOCK_MESSAGE<?php endif; ?>

2. And possible hide the qty-plus and qty-minus buttons.

=> Open style.css file(sites/all/themes/md_orenmode/css/front), and add the following css to the bottom of that file

.qty-box { padding-right: 0; }
.product-text .add-to-box .input-content .qty-box .qty-decrease, .product-text .add-to-box .input-content .qty-box .qty-increase { display: none; }

Thanks!

thoriax's picture
thoriax
Mon, 05/02/2016 - 15:39

Hi Khoa,

Thanks for your feedback.

Indeed, I was already using this if condition. I would prefer to have this fully automatic based on stock level. If stock > 0 -> show 'add to cart', else 'out of stock'. Is this possible based on the decimal stock number?

Thanks for feedback

thoriax's picture
thoriax
Mon, 05/02/2016 - 16:45

changed it to

<?php if (isset($product->commerce_stock['und'][0]['value']) && ($product->commerce_stock['und'][0]['value']) > 0  ): ?>
<?php print render($content['field_product_ref']); ?>
<?php else : ?>
<div class="add-to-box clearfix"><div class="add-to-cart">
<a class="btn btn-3 text-uppercase"><i class="fa fa-cart-plus"></i> <span><?php print t('Out Of Stock'); ?></span></a>
</div
></div>
<?php endif; ?>

which did the trick.

Thanks!

Log in or register to post comments
Have More Idea?

We are here to build your website!

No Universal solution fits all special business requirements. Our experienced team is ready with ideas and state-of-the-art technical solutions to consult the best solution within your budget.