magento remove add to cart with view.phtml - php

I followed [these directions][1] for removing the "add to cart". I am trying to remove add to cart button for items with attribute of "instore_only" and when the response is yes, I want it to echo a static block I have made for it. When I do the first part, the button never goes away. Here is my code:
//Check if the "Available in store only" variable is set to 'Yes':
if(($_product->getAttributeText('instore_only')) == "Yes"){
//If set to Yes, tell PHP what to output:
echo $this->getLayout()->createBlock('cms/block')->setBlockId('instore_only')->toHtml();
}
//If set to No, then show the 'add to cart box' as normal.
else {
?>
<?php if (!$this->hasOptions()):?>
<div class="add-to-box">
<?php if($_product->isSaleable()): ?>
<?php echo $this->getChildHtml('addtocart') ?>
<?php if( $this->helper('wishlist')->isAllow() || $_compareUrl=$this->helper('catalog/product_compare')->getAddUrl($_product)): ?>
<span class="or"><?php echo $this->__('OR') ?></span>
<?php endif; ?>
<?php endif; ?>
<?php echo $this->getChildHtml('addto') ?>
</div>
<?php echo $this->getChildHtml('extra_buttons') ?>
<?php elseif (!$_product->isSaleable()): ?>
<div class="add-to-box">
<?php echo $this->getChildHtml('addto') ?>
</div>
<?php endif; ?>
<?php if ($_product->getShortDescription()):?>
<div class="short-description">
<h2><?php echo $this->__('Quick Overview') ?></h2>
<div class="std"><?php echo $_helper->productAttribute($_product, nl2br($_product->getShortDescription()), 'short_description') ?></div>
</div>
<?php endif;?>
<?php echo $this->getChildHtml('other');?>
<?php if ($_product->isSaleable() && $this->hasOptions()):?>
<?php echo $this->getChildChildHtml('container1', '', true, true) ?>
<?php endif;?>
<?php
}
?>
I have verified the location of the correct view.phtml using template path hints on my frontend.
So, in short, does this code look right, and if not, can I call a cms block in view.phtml? The site supports a small retail store, so some items are only available in the store and not for online purchasing.
I'm about 1 week old in magento and code. I am trying to do a few tweeks to a basic site with a basic template.

I'm assuming, from your question, that the static block is never displayed and that the add to cart button is always displayed. I'm also going to assume that you set your "Instore Only" attribute to "Yes" on the products that you are testing, you've created and enabled a CMS Static block with the identifier instore_only for the current store, and that you've cleared or disabled the Magento Cache.
Check your product attribute configuration
$_product->getAttributeText('instore_only') will return the text value for attributes that have the type Dropdown or Multiple select.
Yes/No catalog input type
If your product attribute is configured with the Yes/No catalog input type, then getAttributeText() will not return a value for it - so it will never be equal to "Yes" in your test and your static block will never be displayed.
Instead you should ask for the attribute value directly. The Yes/No input type is directly compatible with boolean operations, so you can simply test the value in your if statement. Like so:
if ($_product->getInstoreOnly()) {
//output your static block
} else {
//output the add to cart form
}
Text catalog input type
If your attribute configuration as the Text or Text area catalog input type, then you'd compare like this:
if ($_product->getInstoreOnly() == "Yes") {
//output your static block
} else {
//ouput the add to cart form
}
In this instance you'd have to manually type Yes into a box in the product editor to make this work.
Dropdown catalog input type
If your attribute is configured as a Dropdown, or Multiple select choice, to which you've manually added a choice named Yes, then your code above should be correct.
Used in product listing should be Yes
You should also check that the catalog attribute Used in product listing option set to Yes, so that the attribute value is loaded on the product page, for you, by Magento.

Check your attribute settings to make sure it's available on the front end. Also, make sure "Used in Listing" is set to yes so it gets added to the index tables. This makes it quicker to call. I suspect that will allow your current code to work...but not sure without testing.
A less elegant way is to call it from the resource model. I don't recommend this way because you are bypassing the index tables...
Try:
$_product->getResource()->getAttribute('instore_only')->getFrontend()->getValue($_product);

To hide the qty box and the "Add to cart" button from view.phtml, You can comment all the code in addtocart.phtml located in template/catalog/product/view/addtocart.phtml
Hope this helps

Related

Want to display default html if no value is present

I'm using this code to display default html if there is no value present but it's not working with a price field on a wordpress theme
<?php
$price = the_field('ct_listing_price');
if (!empty($price)) {
?>
<h4 class="price marT0 marB0" style="color:#fff;"><?php ct_listing_price(); ?></h4>
<?php
} else { echo "Price Undisclosed";}
?>
I've also tried get_field, but this won't work either. It just starts to display "Price Undisclosed" on every listing despite some having prices.
Is this field inside a form?
If so the field should have the input tag with a name <input name="price"> and the form should have a method either method = GET or method = POST.
Post is most probable in this case, if thats true than your method for checking if the variable is set would look like this:
if (!empty($_POST['price'])) {
//action here
}`

Setting Value on PHP Zend Form

I currently have a form that will let you create a family.
FamilyForm.php
$description = new Textarea(self::KEY_FAMILY_DESCRIPTION);
$description->setAttribute("id", self::KEY_FAMILY_DESCRIPTION);
$description->setLabel("Description");
$this->add($description);
$status = new Hidden(self::KEY_FAMILY_STATUS);
$status->setAttribute("id", self::KEY_FAMILY_STATUS);
$this->add($status);
$save = new Button(self::KEY_SAVE_BTN);
$save->setAttributes(array("id", self::KEY_SAVE_BTN));
$save->setLabel("Save");
$save->setValue("Save");
$this->add($save);
Create.phtml
<?php echo ctrlGroup($this, ProjectFamilyForm::KEY_FAMILY_DESCRIPTION, !($this->admin)); ?>
<?php echo ctrlGroup($this, ProjectFamilyForm::KEY_FAMILY_STATUS, !($this->admin)); ?>
<div class="form-actions">
<?php $save = $this->form->get(ProjectFamilyForm::KEY_SAVE_BTN); ?>
<?php $save->setAttribute("class", "btn btn-primary"); ?>
<?php echo $this->formSubmit($save); ?>
<a class="btn" href="<?php echo $this->url('home'); ?>">Cancel</a>
</div>
This works and allows me to input the description and the status of the family upon creation. However, everytime a family is created the status should be "active". However, the setValue() method seems to not be working.
I am not expert of ZEND but to do this some ways are there:-
most preferable way:- Make you db table field set type and set the default value to active.
Create a hidden field with predefined active value.
Please check this link for help:- http://forums.zend.com/viewtopic.php?t=2079

Hiding a custom field if no value is submitted

How can I hide a specific field on a page displaying data from a form if no value is submitted? An example is below:
<?php if($price): ?>
<li><?php echo $price;?>: <?php echo get_property_price($post->ID);?> <?php echo get_post_meta($post->ID,'rentperiod',true);?></li>
<?php endif; ?>
I don't want the $price or rentperiod values to display as a list item if they are empty. What's the best way to accomplish this?
Get the data first, then display it conditionally. Also avoid changing too often between php and html to get more readable code.
Use the variable expansion in double quotes strings.
$property_price = get_property_price($post->ID);
$rentperiod = get_post_meta($post->ID,'rentperiod',true);
if($price && ($property_price || $rentperiod))
echo "<li>$price: $property_price $rentperiod</li>";

Hide/Show Input's based on mysql data

I have an active session in my page using: $_session_start(); I want to hide part of my form and show another based on the users previous entries.
Basically I offer Direct Deposit and Paper Check I want to be able to have the user only see the fields required for them to complete previously (in signup so the values are already in the database)
Right now the database table is set up like this:
paper_check with a value of 1 (yes pay this way) or 0 (no pay this way)
and the same thing with direct deposit. I need a way to show/hide the fields associated with each based on the users previous selections.
I typed this up but it doesn't seem to do anything. Please help me!!!!! (The class names hide and show are in my css and working properly!)
<?php
if ($_SESSION['direct_deposit'] == 1)
{
$doc->getElementById('check_payable_to')->className+" hide";
}
else
{
$doc->getElementById('name_on_account')->className+" hide";
$doc->getElementById('check_payable_to')->className+" show";
}
?>
I don't see a className property in the PHP DOM library. It looks like this is the way to add a class to an element:
$doc->getElementById('check_payable_to')->setAttribute('class',
$doc->getElementById('check_payable_to')->getAttribute('class') . " hide");
Do you have error reporting enabled? It should be warning about the unknown property.
If you want to do this in Javascript instead of PHP, have your PHP do something like:
<script>
var direct_deposit = <?php echo $_SESSION['direct_deposit']; ?>;
if (direct_deposit == 1) {
/* Do what you want */
} else {
/* Do what else you want */
}
</script>
In your question you said "I have an active session in my page using: $_session_start();" You shouldn't use $_session_start(). It should be session_start() .
className+" hide" should be className .= " hide"
By wrapping each section in it's own div like so:
<div id="paperCheck class="show">
<!--Your Paper Check Information here-->
</div>
<div id="directDeposit" class="show">
<!--Your Direct Deposit Information here-->
</div>
And then by using javascript you are able to do the following:
var direct_deposit = <?php echo $_SESSION['direct_deposit']; ?>;
if (direct_deposit == 1)
{
document.getElementById('paperCheck').className ="hide";
}
else
{
document.getElementById('directDeposit').className ="hide";
}

show division depending on result from server

How can I show or hide div depending on result come from server, in the file that handle the request, I get a customer object from db, depending on user request, if I didn't find the corresponding customer I want to hide a div .
here's my php code
$customer=getCustomerViaCellPhone($link,$cellPhoneNo);
if(!$customer){
$error = 'No result found';
}
here's the view
<div id="customerInfo" class="<?php (is_null($customer))?'hide':''?>">
....
</div>
but it shows the div in both cases
You are missing the command echo in your example so it should be:
<div id="customerInfo" class="<?php echo (is_null($customer))?'hide':''?>">
....
</div>
obviously your css would also need to define a class called .hide that had display:hidden; in it.
According to your comment, $customer is not NULL, it's an object with NULL values in it. Which will return false for your is_null($customer) check because it's not NULLDocs - it is an object.
You can solve this by decoupling the output logic from your data a bit:
$customer=getCustomerViaCellPhone($link,$cellPhoneNo);
$hideCustomerInfo = false;
if(!$customer) {
$error = 'No result found';
$hideCustomerInfo = true;
}
and in the view then:
<div id="customerInfo" class="<?php echo $hideCustomerInfo ? 'hide' : ''; ?>">
....
</div>
In case this does not work, you can validate the $hideCustomerInfo flag already in you controller to see if it contains the value you expect. Additionally this simplifies your view.
Next to that ensure your CSS is correct as well to hide HTML elements with such a class.
<div id="customerInfo" style="display:<?php echo (is_null($customer))?'none':'block'?>">
Take a look at css' display and visibility options

Categories