My first post, i have code where i can individually remove add-to-cart options from products using an additional attribute in the back end, added in addtocart.phtml.
The problem i am having is that i do not want this to affect the add to cart button just the quantity box and label.
I have tried many variations and nothing seems to help, i am thinking that i have this code inserted in an incorrect file but i am not sure.
You can see the code below, i was wondering if anyone could assist me in amending this code in order to achieve the functionality that i am looking for:
<?php $_product = $this->getProduct() ?>
<?php $attTest= $_product->getData(); ?>
<?php if($attTest['sell_online']): ?>
<?php if($_product->isSaleable()): ?>
<div class="add-to-cart">
<?php if(!$_product->isGrouped()): ?>
<label for="qty"><?php echo $this->__('Number of weeks:') ?></label>
<input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" title="<?php echo $this->__('Number of Weeks') ?>" class="input-text qty" />
<?php endif; ?>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="productAddToCartForm.submit()"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php echo $this->getChildHtml('', true, true) ?>
</div>
<?php endif; ?>
<?php endif; ?>
Any help with this would be much appreciated. thanks in advance.
Set hints for template on to get .phtml and block class involved.
Cross check if block class in local folder.
Also check if compiler mode is on in that case your file will be in inc class folder.
Once you make sure you have modify right file then :
assuming that if $attTest sell online is the needed variable value.
<?php if($attTest['sell_online']): ?>
<label for="qty"><?php echo $this->__('Number of weeks:') ?></label>
<input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" title="<?php echo $this->__('Number of Weeks') ?>" class="input-text qty" />
<?php endif; ?>
I update this answer for any of the community members searching for this solution, my thanks go out to Satish the cleaver dude who helped me with my predicament and the original code provider who gave me the idea.
So firstly create an attribute so that you can turn quantity on and off (quantity_active), set catalog input type for store owner to yes/no.
Then copy and paste the following code over the code that exists in your addtocart.phtml.
<?php $_product = $this->getProduct() ?>
<?php $attTest= $_product->getData(); ?>
<?php if($_product->isSaleable()): ?>
<div class ="add-to-cart"></div>
<?php if(!$_product->isGrouped()): ?>
<?php if($attTest['quantity_active']): ?>
<strong><label for="qty"><?php echo $this->__('Number of Weeks:') ?></label></strong>
<input type="text" name="qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty ($_product) ?>" title="<?php echo $this->__('Number of Weeks') ?>" class="input-text qty" />
<?php endif; ?>
<div class ="clear"></div>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="productAddToCartForm.submit()"><span><span><?php echo $this->__('Add to Cart') ? ></span></span></button>
<?php echo $this->getChildHtml('', true, true) ?>
<?php endif; ?>
<?php endif; ?>
you should now be able to switch the quantity option on and off. :-)
The other answer is great except that if any product doesn't have the attribute, an error will be thrown. Best to change the if test slightly:
<?php if (empty($attTest['quantity_active']) || false) :?>
This just checks to see if the attribute exists or is set for the product.
Related
Below is my code,
<?php if($_product->isSaleable()): ?>
<script type="text/javascript">
function setQty(id, url) {
var qty = document.getElementById('qty_' + id).value;
document.getElementById('cart_button_' + id).innerHTML = '<button type="button" class="button" onclick="setLocation(\'' + url + 'qty/' + qty + '/\')"><span><span>Add to Cart</span></span></button>';
}
</script>
<label for="qty"><?php echo $this->__('Qty:') ?></label>
<input type="text" name="qty_<?php echo $_product->getId(); ?>" id="qty_<?php echo $_product->getId(); ?>" maxlength="12" value="1" onkeyup="setQty(<?php echo $_product->getId(); ?>, '<?php echo $this->getAddToCartUrl($_product) ?>');" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
<span id="cart_button_<?php echo $_product->getId(); ?>"><button type="button" class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button></span>
<?php else: ?>
the above code i replaced by previous code(below code)
<?php if($_product->isSaleable()): ?>
<button type="button" class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
<?php else: ?>
now it is adding the quantity into the cart but i have to put manually into the box, i want to add + and - button for increasing and decreasing the quantity, what should i do.
I tried lots of things but it is not working here.
If there is no any spacial design require then you can try input type "number" instead of "text", with that field increase and decrease button comes in input box.
http://www.w3schools.com/html/tryit.asp?filename=tryhtml_input_number
<input type="number" name="qty_<?php echo $_product->getId(); ?>" id="qty_<?php echo $_product->getId(); ?>" maxlength="12" value="1" onkeyup="setQty(<?php echo $_product->getId(); ?>, '<?php echo $this->getAddToCartUrl($_product) ?>');" title="<?php echo $this->__('Qty') ?>" class="input-text qty" />
Despite that I have added the <?php comments_template(); ?> to single.php it seems like it's not working! I'm note sure, but why are the comments in the list of comments to be approved empty? The fields for name, email and URL and the content are empty for each comment.
Should the wp-comments-post.php be located together with the rest of the theme files or should it be together with the main files?
What could be the cause of this problem?
The function comments_template() looks for a file called comments.php in the root of your themes directory. The code inside comments.php should look like the following:
<!-- Display the comments -->
<?php if($comments) : ?>
<ol>
<?php foreach($comments as $comment) : ?>
<li id="comment-<?php comment_ID(); ?>">
<?php if ($comment->comment_approved == '0') : ?>
<p>Your comment is awaiting approval</p>
<?php endif; ?>
<?php comment_text(); ?>
<cite><?php comment_type(); ?> by <?php comment_author_link(); ?> on <?php comment_date(); ?> at <?php comment_time(); ?></cite>
</li>
<?php endforeach; ?>
</ol>
<?php else : ?>
<p>No comments yet</p>
<?php endif; ?>
<!-- Display the form -->
<?php if(comments_open()) : ?>
<?php if(get_option('comment_registration') && !$user_ID) : ?>
<p>You must be logged in to post a comment.</p><?php else : ?>
<form action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post" id="commentform">
<?php if($user_ID) : ?>
<p>Logged in as <?php echo $user_identity; ?>. Log out ยป</p>
<?php else : ?>
<p><input type="text" name="author" id="author" value="<?php echo $comment_author; ?>" size="22" tabindex="1" />
<label for="author"><small>Name <?php if($req) echo "(required)"; ?></small></label></p>
<p><input type="text" name="email" id="email" value="<?php echo $comment_author_email; ?>" size="22" tabindex="2" />
<label for="email"><small>Mail (will not be published) <?php if($req) echo "(required)"; ?></small></label></p>
<p><input type="text" name="url" id="url" value="<?php echo $comment_author_url; ?>" size="22" tabindex="3" />
<label for="url"><small>Website</small></label></p>
<?php endif; ?>
</form>
<?php endif; ?>
<?php else : ?>
<p>The comments are closed.</p>
<?php endif; ?>
however, if you have a different theme directory layout, for example a custom comments.php, you can pass its path in as the funcion parameter. For example, if your theme had a modules direcotry:
<?php comments_template('modules/custom-comments.php'); ?>
Conclusion
This could be a code problem or a misuse of the comments_template() function. I recommend you don't change any WordPress code as it will just be overriden by future updates.
This answer is based on: https://codex.wordpress.org/Function_Reference/comments_template
Code Drops taken from:
http://code.tutsplus.com/articles/unraveling-the-secrets-of-wordpress-commentsphp-file--net-28
I currently have the newsletter checkbox enabled in:
/app/design/frontend/new/default/template/customer/form/register.php
It seems to use the following code in the template file:
<?php if ($this->isNewsletterEnabled()): ?>
<input type="checkbox" name="is_subscribed" title="<?php echo $this->__('Sign Up for Newsletter') ?>" value="1" id="is_subscribed"<?php if($this->getFormData()->getIsSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox" />
<label for="is_subscribed"><?php echo $this->__('Sign Up for Newsletter') ?></label>
<?php echo $this->getChildHtml('customer.form.register.newsletter')?>
<?php endif ?>
The Question
How can I add this same form input to the edit account page?
/app/design/frontend/new/default/template/customer/form/edit.php
Copying just the code above gives the following error, which obviously means the needed functions are not being included.
I'm guessing this means I'm missing some XML somewhere ... but where?
Fatal error: Call to a member function getIsSubscribed() on a non-object
I think I may need to add the following to a layout file, should this be the customer.xml and if so whereabouts in this file? inside this tag <customer_account_edit translate="label">?
{{block type="newsletter/subscribe" template="newsletter/subscribe.phtml"}}
Research
I have found the following Magento StackOverflow question but it doesn't answer where the XML goes?
Newsletter Signup on CMS page in Magento
You can use this code in /app/design/frontend/new/default/template/customer/form/edit.phtml file
<li class="control">
<div class="input-box">
<input type="checkbox" name="is_subscribed"
title="<?php echo $this->__('Sign Up for Newsletter') ?>"
value="1" id="is_subscribed" class="checkbox" />
</div>
<label for="is_subscribed">
<?php echo $this->__('Sign Up for Newsletter') ?>
</label>
<?php echo $this->getChildHtml('customer.form.register.newsletter')?>
</li>
I have a magento store where the Add to Cart button has stopped working and instead a search query is performed ( where the & are changed to & so it doesn't even work ) This can be seen by adding a item to cart here: http://bit.ly/1o8SwmE
This add to cart button does however work on category grid pages. I have tried pasting the link generated in the form into a web browser and it also redirects me to the same broken search page, I have also tried pasting the action link from the add to cart form on a working category grid page into firebug on the non working page and this then adds the product from the category page perfectly.
The snippets of code i have used are pasted below and other people have had them working it seems so this may not be the issue.
OLD CODE
<form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<?php if(!$_product->isGrouped()): ?>
<label for="qty"><?php echo $this->__('QTY') ?>:</label>
<input type="text" name="qty" id="qty" class="new-qty-cat" value="<?php echo ($this->getMinimalQty($_product)?$this->getMinimalQty($_product):1) ?>" />
<?php endif; ?>
<button class="new-qty-button" type="button" onclick="this.form.submit()"></button>
</form>
<?php else: ?>
<p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
NEW CODE
<form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId(); ?>">
<input name="qty" type="text" class="input-text qty" id="qty" maxlength="12" value="<?php echo $this->getMinimalQty($_product) ?>" />
<button class=form-button" onclick="productAddToCartForm_<?php echo $_product->getId(); ?>.submit()"><span><?php echo $this->__('Add to Cart') ?></span></button>
</form>
<script type="text/javascript"> var productAddToCartForm_<?php echo $_product->getId(); ?> = new VarienForm('product_addtocart_form_<?php echo $_product->getId(); ?>'); productAddToCartForm_<?php echo $_product->getId(); ?>.submit = function(){ if (this.validator.validate()) { this.form.submit(); } }.bind(productAddToCartForm_<?php echo $_product->getId(); ?>);</script>
NEW CODE 2
<form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<?php if(!$_product->isGrouped()): ?>
<label for="qty"><?php echo $this->__('Qty') ?>:</label>
<input type="text" name="qty" id="qty" maxlength="12" value="<?php echo ($this->getMinimalQty($_product)?$this->getMinimalQty($_product):1) ?>" />
<?php endif; ?>
<button type="button" onclick="this.form.submit()"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button>
</form>
New Code 3
<form action="<?php echo $this->getAddToCartUrl($_product) ?>" method="post" id="product_addtocart_form_<?php echo $_product->getId()?>"<?php if($_product->getOptions()): ?> enctype="multipart/form-data"<?php endif; ?>>
<?php if(!$_product->isGrouped()): ?>
<label for="qty"><?php echo $this->__('Qty') ?>:</label>
<input class="new-qty-cat" type="text" name="qty" id="qty" maxlength="12" value="<?php echo ($this->getMinimalQty($_product)?$this->getMinimalQty($_product):1) ?>" />
<?php endif; ?>
<button type="button" class="new-qty-button" onclick="this.form.submit()"><span><span> </span></span></button>
</form>
I've succesfully managed to add custom fields to the customer. However I need these fields to show up in onepage checkout.
I've overridden Mage_Customer_Block_Widget_Name and created my own customer/widget/name.phtml, added the attributes in the sql/xxx_setup/installer-x.y.z.php (added them to adminhtml_customer, customer_account_edit, checkout_register and customer_account_create) and they work fine in the admin site, however they just wont work on the checkout form. The field shows up, but it has the wrong value and no label.
I'm clueless why does it work in the customer registration form but doesn't in the checkout.
The installer code to add the attribute is:
$attributes = array(
'lastname2' => array(
'frontend_label'=>'Apellido Materno',
'label' => 'Apellido Materno',
'input' => 'text',
'type' => 'varchar',
//System = False and visible true = Show in 'customer_account_create', 'customer_account_edit', 'checkout_register'
'system'=>true,
'visible'=>true, //Watch out!! Only visible fields get processed by the form controllers!!!
'user_defined'=>false,
'used_in_forms' => array('adminhtml_customer', 'customer_account_edit', 'checkout_register','customer_account_create'),
'required' => 0,
'position' =>69
));
foreach($attributes as $attribute_code=>$definition)
{
$installer->addAttribute('customer', $attribute_code, $definition);
/**
* #var Mage_Eav_Model_Config
*/
Mage::getSingleton('eav/config')
->getAttribute('customer', $attribute_code)
->setData('used_in_forms',$definition['used_in_forms'])
->save();
}
The code in name.phtml is
<div class="<?php echo $this->getContainerClassName()?>">
<?php if ($this->showPrefix()): ?>
<div class="field name-prefix">
<label for="<?php echo $this->getFieldId('prefix')?>"<?php if ($this->isPrefixRequired()) echo ' class="required"' ?>><?php if ($this->isPrefixRequired()) echo '<em>*</em>' ?><?php echo $this->getStoreLabel('prefix') ?></label>
<div class="input-box">
<?php if ($this->getPrefixOptions() === false): ?>
<input type="text" id="<?php echo $this->getFieldId('prefix')?>" name="<?php echo $this->getFieldName('prefix')?>" value="<?php echo $this->escapeHtml($this->getObject()->getPrefix()) ?>" title="<?php echo $this->getStoreLabel('prefix') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('prefix') ?>" <?php echo $this->getFieldParams() ?> />
<?php else: ?>
<select id="<?php echo $this->getFieldId('prefix')?>" name="<?php echo $this->getFieldName('prefix')?>" title="<?php echo $this->getStoreLabel('prefix') ?>" class="<?php echo $this->helper('customer/address')->getAttributeValidationClass('prefix') ?>" <?php echo $this->getFieldParams() ?>>
<?php foreach ($this->getPrefixOptions() as $_option): ?>
<option value="<?php echo $_option?>"<?php if ($this->getObject()->getPrefix()==$_option):?> selected="selected"<?php endif; ?>><?php echo $this->__($_option)?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<div class="field name-firstname">
<label for="<?php echo $this->getFieldId('firstname')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('firstname') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('firstname')?>" name="<?php echo $this->getFieldName('firstname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getFirstname()) ?>" title="<?php echo $this->getStoreLabel('firstname') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('firstname') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<?php if ($this->showMiddlename()): ?>
<?php $isMiddlenameRequired = $this->isMiddlenameRequired(); ?>
<div class="field name-middlename">
<label for="<?php echo $this->getFieldId('middlename')?>"<?php echo $isMiddlenameRequired ? ' class="required"' : '' ?>><?php echo $isMiddlenameRequired ? '<em>*</em>' : '' ?><?php echo $this->getStoreLabel('middlename') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('middlename')?>" name="<?php echo $this->getFieldName('middlename')?>" value="<?php echo $this->escapeHtml($this->getObject()->getMiddlename()) ?>" title="<?php echo $this->getStoreLabel('middlename') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('middlename') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<?php endif; ?>
<div class="field name-lastname">
<label for="<?php echo $this->getFieldId('lastname')?>" class="required"><em>*</em><?php echo $this->getStoreLabel('lastname') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('lastname')?>" name="<?php echo $this->getFieldName('lastname')?>" value="<?php echo $this->escapeHtml($this->getObject()->getLastname()) ?>" title="<?php echo $this->getStoreLabel('lastname') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<div class="field name-lastname">
<label for="<?php echo $this->getFieldId('lastname2')?>"><?php echo $this->getStoreLabel('lastname2') ?></label>
<div class="input-box">
<input type="text" id="<?php echo $this->getFieldId('lastname2')?>" name="<?php echo $this->getFieldName('lastname2')?>" value="<?php echo $this->escapeHtml($this->getObject()->getLastname2()) ?>" title="<?php echo $this->getStoreLabel('lastname2') ?>" maxlength="255" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('lastname2') ?>" <?php echo $this->getFieldParams() ?> />
</div>
</div>
<?php if ($this->showSuffix()): ?>
<div class="field name-suffix">
<label for="<?php echo $this->getFieldId('suffix')?>"<?php if ($this->isSuffixRequired()) echo ' class="required"' ?>><?php if ($this->isSuffixRequired()) echo '<em>*</em>' ?><?php echo $this->getStoreLabel('suffix') ?></label>
<div class="input-box">
<?php if ($this->getSuffixOptions() === false): ?>
<input type="text" id="<?php echo $this->getFieldId('suffix')?>" name="<?php echo $this->getFieldName('suffix')?>" value="<?php echo $this->escapeHtml($this->getObject()->getSuffix()) ?>" title="<?php echo $this->getStoreLabel('suffix') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('suffix') ?>" <?php echo $this->getFieldParams() ?> />
<?php else: ?>
<select id="<?php echo $this->getFieldId('suffix')?>" name="<?php echo $this->getFieldName('suffix')?>" title="<?php echo $this->getStoreLabel('suffix') ?>" class="<?php echo $this->helper('customer/address')->getAttributeValidationClass('suffix') ?>" <?php echo $this->getFieldParams() ?>>
<?php foreach ($this->getSuffixOptions() as $_option): ?>
<option value="<?php echo $_option?>"<?php if ($this->getObject()->getSuffix()==$_option):?> selected="selected"<?php endif; ?>><?php echo $this->__($_option)?></option>
<?php endforeach; ?>
</select>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
</div>
This fields in the checkout you are talking about are address attributes, not customer attributes. So you need to load them differently. For registered users you could use Mage::getSingleton('customer/session')->getCustomer()->getLastname2() but it won't be saved to your address, as there is no attribute yet.
Depending on where you want lastname2 to be accessible you can create corresponding attributes for the entities customer_address,quote_address and order_address. They are created the same way you did for customer with
$installer->addAttribute($entityName, $attribute_code, $definition);
But that's not all. For the attributes to be converted correctly you need to set up conversion rules in your module config.xml. See for example the configuration of Mage_Sales.
In the global node there is a node fieldsets with the corresponding rules. There is a node customer_address to convert address attributes to quote address attributes. In sales_convert_quote there are rules to convert this attributes to order attributes.
So for your attributes to be accessible in all you config should look like this:
<global>
<fieldsets>
<customer_address>
<lastname2>
<to_quote_address>*</to_quote_address>
</lastname2>
</customer_address>
<sales_copy_order_billing_address>
<lastname2>
<to_order>*</to_order>
</lastname2>
<sales_copy_order_billing_address>
<sales_copy_order_shipping_address>
<lastname2>
<to_order>*</to_order>
</lastname2>
</sales_copy_order_shipping_address>
<sales_convert_quote_address>
<lastname2>
<to_order_address>*</to_order_address>
<to_customer_address>*</to_customer_address>
</lastname2>
</sales_convert_quote_address>
<sales_convert_order_address>
<lastname2>
<to_quote_address>*</to_quote_address>
</lastnam2e>
<sales_convert_order_address>
<customer_address>
<lastname2>
<to_quote_address>*</to_quote_address>
</lastname2>
</customer_address>
</fieldsets>
</global>