[SOLVED] Add to Cart Button based on own rules

  • Posts: 108
  • Thank you received: 4
  • Hikashop Business
12 years 3 months ago #58126

Hello Hika-Shoppers

I would like to display (or hide) the "Add to Cart"-Button for a product based on more complex rules than is there a price and a stock. I enriched the product table with some attributes and based on these attributes, I have to display the "Add to Cart"-Button or not.

For example:
I have a custom field on the users table (expiration date of a criminal record document) and on the products table I have a custom field "requires valid criminal record". Now if the "requires valid criminal record" value is set and the expiration date of this document is still in the future, the customer is allowed to buy the product and I shall display the "Add-to-Cart"-Button. If the expiration date is in the past in this situation, I am not allowed to display the "Add-to-Cart"-Button and I have to display a message to the user e.g. "Your criminal record document has been expired"!

I know, it sounds very strange: It's about an online Gunstore (which is 100% perfect legal i would like to add). There are restructions for products e.g. ammunition where I need such a logic (and there is much more, but this would blow the frame of this post).

So, now I'm looking for the php-file where I have access to the products and user table data and where the "Add-to-Cart"-Button is created hidden. May I ask you, if you know where it is? Is it e.g. cart.php?

I would also like to ask if there is a Documentation how the Data-Access and these php-Files works together?

Thank you very much for your answer!
Best regards
Mike

Last edit: 12 years 3 months ago by mike.

Please Log in or Create an account to join the conversation.

  • Posts: 82819
  • Thank you received: 13366
  • MODERATOR
12 years 3 months ago #58194

Hi,

You will find that in the file "quantity" of the view "product" via the menu Display->Views.

You can get the custom fields of the current product like that:
echo $this->row->COLUMN_NAME;

where COLUMN_NAME is the column name of your custom field.

And for the user custom fields, you can do like that:
$user = hikashop_loadUser(true);
echo $user->COLUMN_NAME;

The following user(s) said Thank You: mike

Please Log in or Create an account to join the conversation.

  • Posts: 108
  • Thank you received: 4
  • Hikashop Business
12 years 3 months ago #58195

Hey there Nicolas

Thank you for that answer! I will try this out today!
I try to post the code and my solution if I have found the appropriate way!

Thanks again, Mike :)

Last edit: 12 years 3 months ago by mike.

Please Log in or Create an account to join the conversation.

  • Posts: 108
  • Thank you received: 4
  • Hikashop Business
12 years 3 months ago #59047

B) SOLVED

My problem is solved for displaying additional hints and enable/disable the "Add to cart" button. I would like to post here, what it did to solve that problem, so that maybe other users can use a solution like mine.

Since I had to manipulate the product detail page of some products in my store, I added custom fields via the Hika-Shop Administration UI to the products table. In my case, there are some boolean values called:

product_requires_wes
product_requires_contract
product_requires_criminal_record_doc
product_requires_adult
product_restrict_online_sale

For business reasons, I had to add two additional fields to the users table called:
user_date_of_birth
user_validity_expire_date
(both are of type "date")

I wanted two things:
a.) to display a hint message text on the product page, depending on the flags above
b.) display/hide the "Add to cart button" based on the flags above

I did 2 tings:
1.) I wrote a little php code file to cover a.)
public_html/shop/components/com_hikashop/views/product/tmpl/show_block_product_gunshop_notices.php

2.) I added a call to this code to the show_default.php file (at the very end of the file)

<?php
  $this->setLayout('show_block_product_gunshop_notices');
  echo $this->loadTemplate();
?>

3.) To cover the business cases for b.) I changed the logic of the quantity.php file. I just added this code in the block where the file starts after:
defined('_JEXEC') or die('Restricted access');
?>
<?php
// Read product values from current record
$productIsWesRequired = (strcasecmp($this->element->product_requires_wes, "yeswes") == 0) ? true : false;
$productIsContractRequired = (strcasecmp($this->element->product_requires_contract, "yescontract") == 0) ? true : false;
$productIsCriminalRecordRequired = (strcasecmp($this->element->product_requires_criminal_record_doc, "yescriminalrecord") == 0) ? true : false;
$productIsAdultRequired = (strcasecmp($this->element->product_requires_adult, "yesadult") == 0) ? true : false;
$productIsRestrictedForOnlineSale = (strcasecmp($this->element->product_restrict_online_sale, "yesrestrictedonlinesale") == 0) ? true : false;

// Load user data and read them to the required variables
$hikaShopUser = hikaShop_loadUser(true);
$isUserLoggedIn = (is_null($hikaShopUser)) ? false : true;

$userAge = 0;
$isUserAdult = false;
$isUsersPermitValid = false;
$isUsersBirthdateRegistered = false;
$isUsersValidityExpireDateRegistered = false;

if ($isUserLoggedIn) {
  // Calculate age of customer
  $usersBirthdate = $hikaShopUser->user_date_of_birth;
  if(!is_null($usersBirthdate)) {
    if(strlen($usersBirthdate) > 0) {
      $isUsersBirthdateRegistered = true;
      $birthday_timestamp = strtotime($hikaShopUser->user_date_of_birth);
      $userAge = date('md', $birthday_timestamp) > date('md') ? date('Y') - date('Y', $birthday_timestamp) - 1 : date('Y') - date('Y', $birthday_timestamp);
      if($userAge < 0) {
        $userAge = 0; // in case when a future date has been entered for birthdate.
      }
      if($userAge >= 18) {
        $isUserAdult = true;
      }
    }
  }

  // Calculate if the user is valid.
  $usersValidityExpireDate = $hikaShopUser->user_validity_expire_date;
  if(!is_null($usersValidityExpireDate)) {
    if(strlen($usersValidityExpireDate) > 0) {
      $isUsersValidityExpireDateRegistered = true;
      $validity_timestamp = strtotime($hikaShopUser->user_validity_expire_date);
      $now_timestamp = strtotime("now");
      if($validity_timestamp > $now_timestamp) {
        $isUsersPermitValid = true;
      }
      else {
        if($validity_timestamp == $now_timestamp) {
          $isUsersPermitValid = true;
        }
      }
    }
  }
}

$displayCart = true;
if($productIsWesRequired || $productIsContractRequired || $productIsRestrictedForOnlineSale)
{
    $displayCart = false;
}
else if ($productIsCriminalRecordRequired && $isUsersPermitValid == false)
{
    $displayCart = false;
}
else if($productIsAdultRequired && $isUserAdult == false)
{
    $displayCart = false;
}

$displayNoStockMessage = true;
if($productIsWesRequired || $productIsContractRequired || $productIsRestrictedForOnlineSale)
{
    $displayNoStockMessage = false;
}

if($productIsWesRequired || $productIsContractRequired || $productIsCriminalRecordRequired || $productIsAdultRequired || $productIsRestrictedForOnlineSale) {?>
  <br /><p><a href="#anch_important_product_notice"><strong><span style="background-color: #ffff00; color: #ff0000;"><?php echo JText::_('CHECK_SPECIAL_PRODUCT_NOTICE_ENDOFPAGE') ?></span></strong></a></p><br />
<?php }

I had to change the line:
}elseif(!$this->params->get('catalogue') && ($this->config->get('display_add_to_cart_for_free_products') || !empty($this->row->prices))){

to this...
}elseif(!$this->params->get('catalogue') && $displayCart && ($this->config->get('display_add_to_cart_for_free_products') || !empty($this->row->prices))){

4.) I also changed the line where the no-stock information is shown:
if($displayNoStockMessage)
            {
              echo JText::_('NO_STOCK');
            }

And here you can find, what I did: Please keep in mind, that I'm not a professional PHP-Programmer, so therefore my code must look terrible, but it seems that it works so far pretty well. I couldn't manage to make functions and include files and stuff like that and therefore the code is just "monolitic and as a block". Sorry for that, but I was not able to find the problem (locally in my NetBeans/XAMPP environment, includes worked well, but on my webserver it failed and I couldn't find the reason).

Maybe there are some suggestions for my code: I'm always open for good ideas.
Thank you and hopefully, this can help other people with their shop.
Regards, Mike

@Hika-Team and this forum: THANKS FOR THE HELP AND KEEP UP THE GOOD WORK! :)

Attachments:
Last edit: 12 years 3 months ago by mike. Reason: php is not accepted as a file extension for attachments

Please Log in or Create an account to join the conversation.

  • Posts: 82819
  • Thank you received: 13366
  • MODERATOR
12 years 3 months ago #59104

Thank you for the all the information !

Instead of creating the show_block_product_gunshop_notices file, I recommend you to put the code directly in the show_default view file via the menu Display->Views. That way, you won't loose the code when you update HikaShop.

Last edit: 12 years 3 months ago by nicolas.

Please Log in or Create an account to join the conversation.

Time to create page: 0.073 seconds
Powered by Kunena Forum