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
}`
Related
Hi i have update my question to explain you my problem easily.Okay so,
What i wnant and whats wrong with me?
i code a product form where i require getcatlist.php which help me get value from sql database and show in product page, Snapshot of product form product form image
Product form code
<section class="main-section">
<article class="prod-item">
<h3>Product Form</h3>
<form action="addproducts.php" method="get" name="product_form">
<section class="select-box">
<label for="category">Category</label>
<?php require_once("getcatlist.php"); ?>
<input type="hidden" name="cat_id" value="<?php echo $_GET['cat_id']; ?>" >
</section>
</form>
</article>
</section>
my javascript in product form page used to create query string code
<script type="text/javascript">
function showCategory(str)
{
document.product_form.action="addproducts.php?cat_id="+str;
}
</script>
**getcallist.php page code which fetch data from databse and show in select tag **
<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once("../data_connect.php");
$error_msg = "";
$cat_id = "";
$catsql = "SELECT Cat_id,CategoryName FROM category order by Cat_id asc; ";
$cat_query = mysqli_query($connect,$catsql);
$numrows = mysqli_num_rows($cat_query);
$cat_id = $_GET['cat_id'];
if($numrows == 0)
{
$error_msg= "No Categories Found";
}
else{
echo "<select name=\"category\" id=\"category\" onchange=\"showCategory(this.value)\" >";
echo "<option value=\"null\">Select your category</option>";
while($catrow = mysqli_fetch_assoc($cat_query))
{
if ($catrow['Cat_id'] == $cat_id){
echo "<option value=\"".$catrow['Cat_id']."\" selected=\"selected\">".$catrow['CategoryName']."</option>";
}
else{
echo "<option value=\"".$catrow['Cat_id']."\">".$catrow['CategoryName']."</option>";
}
}
echo "</select>";
}
?>
here is my ajax code which move to another page when i click on dropdown list
<script type="text/javascript">
/**/$(document).ready(function(){
$("#category").change(function(){
var code = $(this).val();
if(code == 1)
{
$('.main-section').load('addconsoles.php');
}
else if(code == 2)
{
$('.main-section').load('addaccessories.php');
}
else if(code == 3)
{
$('.main-section').load('addgames.php');
}
});
});
</script>
okay i wants to create a query string of category list, when i click on any category option in dropdown list it wont show me any thing in url. This is the big problem i face and also when i try to set and get value in php oop class(require in product form page) it won't set and get when echo it.
Please help me out from this problem, Thanks
I think you have somewhat mis-understood how HTML forms work.
The code you've got will not work as you expect because you have got the cat_id field defined twice within your form - once in the hidden field you created, and again when you alter the action method of the form via the JavaScript. However when your JavaScript runs, it modifies the "action" string, but the hidden field will not change, and when you submit the form, the browser will submit the value from the hidden field, and ignores the value you manually added to the "action" method. This is just the default behaviour in such a situation, in order to resolve the ambiguity you created.
Therefore if at the time the page was first loaded, there was no value in cat_id (which seems likely, looking at your code), then no value for cat_id will be sent to the server when the form is submitted.
However, all of the code I described above is actually unnecessary. You can directly use the value of the dropdownlist as the cat_id. Simply changing the "name" parameter of your <select>, as follows:
echo '<select name="cat_id" id="category">';
will make the cat_id parameter change to whatever is in the selected value of the dropdown when the form is submitted.
You do not need the "showCategory" function, or the hidden field.
So the form can be
<form action="addproducts.php" method="get" name="product_form">
<section class="select-box">
<label for="category">Category</label>
<?php require_once("getcatlist.php"); ?>
</section>
</form>
without the hidden field, and the "showCategory" JavaScript function can be deleted entirely.
N.B. From this code it seems you have no way to submit this "addproducts" form to the server. It's not clear whether you have some other JavaScript elsewhere which controls this and is triggered based on some other event, and/or whether you need to add a "submit" button to the form. Either way, nothing will be sent to the server until you cause the form to be submitted, or provide a way for the user to cause it.
I hope this is useful, please comment if you still find anything unclear.
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
Hopefully a very basic question.
I have a custom post type with custom meta. I had it working ok, but after some modifications I am not seeing what i expected.... PHP is a new language for me so please forgive a dumb question. I was using:
$meta = get_post_custom($post->ID); ?>
<input type="text" id="dt_location" name="dt_location" placeholder="Dog Tag" value="<?php $meta["dt_location"][0] ?>" />
in the custom metabox and it was working great however now the variable is not outputting any value. If I use var_dump I get:
string(6) "450000"
print_r also will display the value, however when I use something like:
echo "START]" . $meta["dt_location"][0] . "[END <BR>";
all I receive is
START][END
Why am I seeing data with var_dump and print_r, but not using the method that worked earlier?
The value attribute in your input is missing the echo:
<?php echo $meta["dt_location"][0]; ?>
Also, if you just need to get a value for a specific custom field, say dt_location, you can use the get_post_meta function:
<?php echo get_post_meta($post->ID, 'dt_location', true); ?>
I have a site based on wordpress. I need to allow people to create posts from frontend so I have made a multi-part form which works pretty well. There are three parts of the form and each part of the form is validated before moving to the next part. Data is passed to another page through hidden inputs.
My form template looks somewhat like this ( complete code is pretty massive and irrelevant here, so just showing just the relevant parts ) which I hope is enough to give an idea how the form works.
MULTI-PART FORM WP-TEMPLATE SAMPLE CODE :
<?php
global $wpdb;
$this_page = $_SERVER['REQUEST_URI'];
$page = $_POST['page'];
if ( $page == NULL ) { ?>
<?php include_once('multiparts/form-files/first_part.php'); ?>
<?php } else if ( $page == 1 ) { ?>
<?php include_once('multiparts/validation/validate_first_part.php');
if (isset($_POST['submit-1']) && (!empty($error))) { ?>
<div class="error-head">Cannot continue registration. Error/s highlighted below.</div><br/>
<?php echo $error . '</br>'; ?>
<?php } else {
include_once('multiparts/form-files/second_part.php');
}
?>
<?php
} else if ( $page == 2 ) { ?>
//SO ON AND SO FORTH
<?php
}
?>
Recently, I have a added several checkbox fields in the form with an intention to display values of selected checkboxes, in the created posts. So here is a relevant html form code that I am currently using.
<fieldset class="work-areas">
<label for="areas" class="label">INTERESTED IN :</label></br>
<div class="work-class">
<input type="checkbox" name="workareas[]" value="administration"/>administration</br>
<input type="checkbox" name="workareas[]" value="technical"/>technical</br>
<input type="checkbox" name="workareas[]" value="creative"/>creative</br>
<input type="checkbox" name="workareas[]" value="fieldwork"/>fieldwork</br>
<input type="checkbox" name="workareas[]" value="marketing"/>marketing</br>
</div>
</fieldset>
I insert the values of these checkboxes into the post just like any other custom fields using this code: add_post_meta($pid, 'areas', $workareas, true );. In the processing part it is assigned a meta_key areas. I display it in the single.php with the code below :
<?php $areas = get_post_meta($post->ID, 'areas', true); ?>
<?php if (is_array($areas)) : ?>
<h4>INTERESTED AREAS OF WORK:</h4>
<?php if (is_array($areas)) {
foreach($areas as $area) {
echo '<li>'.$area.'</li>';
}
}
?>
<?php endif;?>
ISSUE: All this works well when the above given html form code for checkboxes is in the last/third part of the form. But it does work when the same checkbox fields is in the second part of the form. I guess it simply does not pass the array values of the selected checkboxes to the third part. Print_r shows an empty array and obviously does not display anything in the single.php too. Although I understand that the trouble is just the array of selected checkbox values, NOT being carried to the third part properly, I need help as I am noob to all this.
So the bottomline question is how do I save the array of the selected
checkboxes' values in the second part and carry it to the third part
and finally assign it to a variable which will hold the array values.
That which can be displayed in post using the code above.
THINGS TRIED : I have looked into this thread here and I am confused where I will insert my checkbox fields and not even sure it applies to my situation. I have been able to pass other text input values from one part to another part of the from using something like this :
<input type="hidden" name="eligible" value="<?php echo $eligible;?>" />
So, I tried using name="workareas[]" but did not work. I am doing print_r() for everything I am trying and till now have only been getting empty array. I am still going through tons of other threads looking for possible hints. In the meanwhile if you can help, that will be great. Thanks in advance.
UPDATE : Solved, please check the answer.
Not a WP user but your checkboxes are named "workareas" but you seem to refer to them as "areas" everywhere else.
Thanks for the suggestions in the comments but I have found a graceful and robust solution in my opinion, that is using sessions. I followed the basic idea from here which actually deals with a multipart form with sessions. Now I have the following code in my third/last part of the form, at the very beginning of the document. At this time please remember that the html checkbox fields as illustrated above in the question are in the second part.
<?php
session_start();
$_SESSION['workareas'] = $_POST['workareas'];
$result=$_POST['workareas'];
?>
The code is that is repeated during the validation of the third part is the same but this way the variable $result is still holding the values of the array of the selected checkboxes from the second part of the form.
session_start();
$result=$_SESSION['workareas'];
You can do a print_r($result) at this point and check. Furthermore, if you want to insert these array values to post in order for them to show up in the post just assign meta_key eg. areas to $result and use the code below to add it as a custom field :
add_post_meta($pid, 'areas', $result, true);
In the single.php you can use the code below to pull values from the array and show. Do not avoid if(is_array) statement else wordpress might throw an error warning: invalid arguments supplied foreach(). Goodluck.
<?php $areas = get_post_meta($post->ID, 'areas', true); ?>
<?php if (is_array($areas)) : ?>
<h4>INTERESTED AREAS OF WORK:</h4>
<?php if (is_array($areas)) {
foreach($areas as $area) {
echo '<li>'.$area.'</li>';
}
}
?>
<?php endif;?>
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