How do I create WooCommerce Custom Product Sorting style? - plugins

I want to change the default sorting style given in WooCommerce plugin.
Example

I suppose you have already copy the WooCommerce template woocommerce/templates to your theme. You can find the WooCommerce structure here.
Once you have the WooCommerce template under your theme, looking for woocommerce/loop/orderby.php, this is where you can custom the product order. To change different order list, here is the code snippet to demonstrate how to do that:
//your-theme/woocommerce/loop/orderby.php
<form class="woocommerce-ordering" method="get">
<select name="orderby" class="orderby">
<?php
$catalog_orderby = apply_filters('woocommerce_catalog_orderby', array(
'title' => __('Alphabetically', 'woocommerce'),
'date' => __('Most Recent', 'woocommerce'),
'price' => __('Price Asc', 'woocommerce'),
'price-desc' => __('Price Desc', 'woocommerce')
));
?>
<?php foreach ( $catalog_orderby as $id => $name ) : ?>
<option value="<?php echo esc_attr( $id ); ?>" <?php selected( $orderby, $id ); ?>><?php echo esc_html( $name ); ?></option>
<?php endforeach; ?>
</select>
</form>
You can add or remove any options from the $catalog_orderby above.

**'price-asc**' => __('Price Asc', 'woocommerce'),
'price-desc' => __('Price Desc', 'woocommerce')
));
Must changed to this now work*

Related

How do i loop to display post category title?

Please, pardon me for unending questions. I am just a newbie as far as wordpress theme development is concerned. I am just cracking my brain to get something done. I have successfully worked on my code and it gives one category title. The category title of the post is not appropriate. I beg someone out there to help look into my code below and help me correct where necessary. I am just tired over this issue.
I want it to display appropriate category title above the post as you can see about. The category title is outside the loop and not within. It is equally not in category page rather at the front page.
`<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => 'building',
)
)
);
$q = new WP_Query( $args );
$leadingcount = 3;
$cats = get_terms( array( 'taxonomy' => 'product_cat' ) );
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo "<h6 class='mt-5 text-center'>".$cat->name."</h6>"; ?>
<?php if (have_posts()) : while ( $q->have_posts() ) : $q->the_post(); ?>
<article class="item col-lg-<?php echo $leadingcount; ?>">
<div class="card rounded-0">
<div class="card-body">
<a href="<?php the_permalink(); ?>">
<h5><?php the_title(); ?></h5>
</a>
<?php the_excerpt(); ?>
<?php the_category(); ?>
</div>
</div>
</article>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
`

Zend Framework multiple form elements names

I have multiple Zend Framework(v1.12) forms in my application.
Main form:
<?php
class Application_Form_Main extends Zend_Form
{
public function init()
{
$this->setMethod('post')->setAction('some/url');
}
}
?>
My subform:
<?php
class Application_Form_User extends Zend_Form_SubForm
{
public function init()
{
//first name element
$this->addElement('text',
'first_name',
array(
'label' => 'Name',
'required' => true,
'filters' => array('StringTrim')
)
);
//last name element
$this->addElement('text',
'last_name',
array(
'label' => 'Surname',
'required' => true,
'filters' => array('StringTrim')
)
);
$this->setElementDecorators(array(
'ViewHelper',
'Errors'
));
}
}
?>
In my custom controller (for example UsersController.php) Im rendering main form with multiple user subforms:
<?php
$mainForm = new Application_Form_Main();
for($i=0; $i<2; $i++){
$userForm = new Application_Form_User();
$mainForm->addSubForm($userForm, 'user_'.($i+1));
}
//passing main form to the template
$this->view->mainForm = $mainForm;
?>
So Im getting the form with 2 users first_name and last_name fields.
In my template Im rendering form this way:
<form action="<?php echo $this->mainForm->getAction(); ?>"
enctype="<?php echo $this->form->getEnctype(); ?>"
method="<?php echo $this->form->getMethod(); ?>"
">
<?php echo $this->mainForm->getSubForm('user_1')->first_name; ?>
<?php echo $this->mainForm->getSubForm('user_1')->last_name; ?>
<?php echo $this->echo $this->mainForm->getSubForm('user_2')->first_name; ?>
<?php echo $this->echo $this->mainForm->getSubForm('user_2')->last_name; ?>
</form>
The problem is first_name and last_name text field names are identical in both forms. How can I make it to have unique names? If I output the form:
<?php echo $this->mainForm; ?>
Then everything is ok, I get different field names.
So any ideas?

CakePHP 2.0 Determine which submit button has been clicked

In CakePHP 1.3 you can create a form with multiple submit buttons:
echo $this->Form->submit('Submit 1', array('name'=>'submit');
echo $this->Form->submit('Submit 2', array('name'=>'submit');
and detect which submit button was pressed in the controller with:
if (isset($this->params['form']['submit']) && $this->params['form']['submit'] == "Submit 1") {
// first button clicked
}
In CakePHP, $this->params['form'] isn't set and the clicked button value doesn't appear anywhere in $this->request, $this->request->data, $this->params, $this->data or $_POST.
How do I determine which button has been clicked in CakePHP 2.0?
Thanks in advance.
Edit:
As requested, here's the code for the form:
<?php echo $this->Form->create('History', array('action'=>'add')); ?>
<div class='submit'>
<?php
echo $this->Form->submit('Yes', array('div'=>false, 'name'=>'submit'));
echo $this->Form->submit('No', array('div'=>false, 'name'=>'submit'));
?>
</div>
<?php echo $this->Form->end()?>
And the output of the form:
<form action="/projects/kings_recruit/trunk/www/histories/add" id="HistoryAddForm" method="post" accept-charset="utf-8">
<div style="display:none;">
<input name="_method" value="POST" type="hidden">
</div>
<div class="submit">
<input name="submit" value="Yes" type="submit">
<input name="submit" value="No" type="submit">
</div>
</form>
Generally it is a bad practise to use the same name for both submit buttons.
There should be a "submit" key in the $_POST and $this->request->data
I tested this in CakePHP 2.1.1 as shown below:
The view code:
<?php echo $this->Form->create('Message', array('action'=>'test'));
// Extra test input field
echo $this->Form->input('test');
?>
<div class='submit'>
<?php
echo $this->Form->submit('Yes', array('div'=>false, 'name'=>'submit'));
echo $this->Form->submit('No', array('div'=>false, 'name'=>'submit'));
?>
</div>
<?php echo $this->Form->end()?>
The in the controller in $this->request->data:
array(
'submit' => 'Yes',
'Message' => array(
'test' => 'TestFieldTest'
)
)
And in $_POST:
array(
'_method' => 'POST',
'data' => array(
'Message' => array(
'test' => 'TestFieldTest'
)
),
'submit' => 'Yes'
)
You can also give the two submits different names:
echo $this->Form->submit('Yes', array('div'=>false, 'name'=>'submitY'));
echo $this->Form->submit('No', array('div'=>false, 'name'=>'submitN'));
This way you can differ them in the $_POST or $this->request->data, because the keys will be the submits' names:
array(
'submitY' => 'Yes',
'Message' => array(
'test' => 'foo'
)
)
array(
'_method' => 'POST',
'data' => array(
'Message' => array(
'test' => 'Bar'
)
),
'submitY' => 'Yes'
)
Then to determine which button is pressed you can use a simple isset($_POST['']) or over $this->request->data ?
Don't use the same name for both submit buttons. Consider this example:
<?php echo $this->Form->create(false); ?>
<?php echo $this->Form->text('input'); ?>
<?php echo $this->Form->submit('Yes', array('name' => 'submit1')); ?>
<?php echo $this->Form->submit('No', array('name' => 'submit2')); ?>
<?php echo $this->Form->end(); ?>
debug($this->request->data) will produce the following when the "Yes" button is clicked:
array(
'submit1' => 'Yes',
'input' => 'test'
)
And here it is when the "No" button is clicked:
array(
'submit2' => 'No',
'input' => 'test'
)
To check which button was clicked:
if (isset($this->request->data['submit1'])) {
// yes button was clicked
} else if (isset($this->request->data['submit2'])) {
// no button was clicked
}
in 2.0 there is no $this->params['form'] anymore
all form helper posted fields end up in $this->data (which makes more sense anyway)
so
if (!empty($this->data['submit']) && $this->data['submit'] == "Submit 1") {}
note that !empty() is better here as well.
PS: you can use my enhanced upgrade shell to replace it in your code: https://github.com/dereuromark/upgrade
its the command
cake Upgrade.Upgrade request
(https://github.com/dereuromark/upgrade/blob/master/Console/Command/UpgradeShell.php#L833)
if (!empty($this->request->data['submit']) && $this->request->data['submit'] == "Yes") {
// do your stuff
}

select menu default values not rendering in symfony

I am using Symfony 1.4, and am using embedded forms to place multiple similar forms into one form for a configuration page. I am having success showing the form, but the default values of the sfWidgetFormChoice widgets are not being rendered, i.e. the selected="selected" attribute is gone from the HTML.
Incidentally, the default values show up if I don't use embedded forms. The problem with avoiding embedded forms is that each form has identical inputs and therefore overwrites itself.
The action code is as such, some code omitted for brevity:
$serviceFormArray = array();
$this->fullForm = new ConfigForm();
foreach($this->serviceArray as $net => $service)
{
$this->partialForm = new ConfigForm();
foreach($service as $typeId => $val)
{
$typeObj = Doctrine::getTable('Type')->find($typeId);
$typeField = new sfWidgetFormChoice(array(
'default' => $val,
'choices' => array('1' => 'on', '0' => 'off'),
'label' => $typeObj->name)
);
$typeField->setDefault($val);
$serviceFormArray[$typeObj->name] = $typeField;
}
$netObj = Doctrine::getTable('Network')->find($net);
$this->partialForm->setWidgets($serviceFormArray);
$this->fullForm->embedForm($netObj->name,$this->partialForm);
}
and the template looks like this, some code omitted for brevity:
<div class="sectionBox">
<?php echo $fullForm->renderFormTag('/configure/submitconfig') ?>
<?php foreach ($fullForm->getVisibleFields() as $part => $field): ?>
<div class="settingsField">
<?php echo $field->renderLabel() ?>
<?php echo $field->render() ?>
<input type="hidden" name="plug" value="<?php echo $plugName; ?>"/>
</div>
<?php endforeach; ?>
<div id="submitConfig"><input type="submit" value="Save"/></div>
</form>
</div>
Try setting default value via $form->setDefault($name, $default).
$this->partialForm->setDefault($typeObj->name, $val);

How do I use ViewScripts on Zend_Form File Elements?

I am using this ViewScript for my standard form elements:
<div class="field" id="field_<?php echo $this->element->getId(); ?>">
<?php if (0 < strlen($this->element->getLabel())) : ?>
<?php echo $this->formLabel($this->element->getName(), $this->element->getLabel());?>
<?php endif; ?>
<span class="value"><?php echo $this->{$this->element->helper}(
$this->element->getName(),
$this->element->getValue(),
$this->element->getAttribs()
) ?></span>
<?php if (0 < $this->element->getMessages()->length) : ?>
<?php echo $this->formErrors($this->element->getMessages()); ?>
<?php endif; ?>
<?php if (0 < strlen($this->element->getDescription())) : ?>
<span class="hint"><?php echo $this->element->getDescription(); ?></span>
<?php endif; ?>
</div>
Trying to use that ViewScript alone results in an error:
Exception caught by form: No file
decorator found... unable to render
file element
Looking at this FAQ revealed part of my problem, and I updated my form element decorators like this:
'decorators' => array(
array('File'),
array('ViewScript', array('viewScript' => 'form/field.phtml'))
)
Now it's rendering the file element twice, once within my view script, and extra elements with the file element outside my view script:
<input type="hidden" name="MAX_FILE_SIZE" value="8388608" id="MAX_FILE_SIZE" />
<input type="hidden" name="UPLOAD_IDENTIFIER" value="4b5f7335a55ee" id="progress_key" />
<input type="file" name="upload_file" id="upload_file" />
<div class="field" id="field_upload_file">
<label for="upload_file">Upload File</label>
<span class="value"><input type="file" name="upload_file" id="upload_file" /></span>
</div>
Any ideas on how to handle this properly with a ViewScript?
UPDATE: Based on Shaun's solution, here's my final code:
Form Element:
$this->addElement('file', 'upload_file', array(
'disableLoadDefaultDecorators' => true,
'decorators' => array('File', array('ViewScript', array(
'viewScript' => '_form/file.phtml',
'placement' => false,
))),
'label' => 'Upload',
'required' => false,
'filters' => array(),
'validators' => array(array('Count', false, 1),),
));
View Script:
<?php
$class .= 'field ' . strtolower(end(explode('_',$this->element->getType())));
if ($this->element->isRequired()) {
$class .= ' required';
}
if ($this->element->hasErrors()) {
$class .= ' errors';
}
?>
<div class="<?php echo $class; ?>" id="field_<?php echo $this->element->getId(); ?>">
<?php if (0 < strlen($this->element->getLabel())): ?>
<?php echo $this->formLabel($this->element->getFullyQualifiedName(), $this->element->getLabel());?>
<?php endif; ?>
<span class="value"><?php echo $this->content; ?></span>
<?php if ($this->element->hasErrors()): ?>
<?php echo $this->formErrors($this->element->getMessages()); ?>
<?php endif; ?>
<?php if (0 < strlen($this->element->getDescription())): ?>
<p class="hint"><?php echo $this->element->getDescription(); ?></p>
<?php endif; ?>
</div>
The answer is relatively simple as it happens. All you need do is specify the File decorator first, create a specific view script for the file input and specify false for the placement in the viewScript decorator arguments, this will effectively inject the output from the File decorator into the viewScript decorator e.g.
$element->setDecorators(array('File', array('ViewScript', array('viewScript' => 'decorators/file.phtml', 'placement' => false))));
Then in the new file element view script you simply echo $this->content in the script where you'd like the file input markup to be placed. Here's an example from a recent project, so ignore the markup if it looks a little odd, hopefully it will illustrate the point.
<label for="<?php echo $this->element->getName(); ?>" class="element <?php if ($this->element->hasErrors()): ?> error<?php endif; ?>" id="label_<?php echo $this->element->getName(); ?>">
<span><?php echo $this->element->getLabel(); ?></span>
<?php echo $this->content; ?>
<?php if ($this->element->hasErrors()): ?>
<span class="error">
<?php echo $this->formErrors($this->element->getMessages()); ?>
</span>
<?php endif; ?>
</label>
When rendered you will see this html for the element
<label for="photo" class="element" id="label_photo">
<span>Photo</span>
<input type="hidden" name="MAX_FILE_SIZE" value="6291456" id="MAX_FILE_SIZE">
<input type="file" name="photo" id="photo">
</label>
This is not a simple or ideal solution because it requires an extension of the File decorator... but it's rather frustrating that they didn't make the effort to separate the hidden element generation logic from the file input generation logic. I'm not sure if the file view helper handles the issue of an element being an array (that seems to be the reason they did it this way.)
Extension of File Decorator:
(the commented out part is what causes the extra input to be generated.)
<?php
class Sys_Form_Decorator_File extends Zend_Form_Decorator_File {
public function render($content) {
$element = $this->getElement();
if (!$element instanceof Zend_Form_Element) {return $content;}
$view = $element->getView();
if (!$view instanceof Zend_View_Interface) {return $content;}
$name = $element->getName();
$attribs = $this->getAttribs();
if (!array_key_exists('id', $attribs)) {$attribs['id'] = $name;}
$separator = $this->getSeparator();
$placement = $this->getPlacement();
$markup = array();
$size = $element->getMaxFileSize();
if ($size > 0) {
$element->setMaxFileSize(0);
$markup[] = $view->formHidden('MAX_FILE_SIZE', $size);
}
if (Zend_File_Transfer_Adapter_Http::isApcAvailable()) {
$apcAttribs = array('id' => 'progress_key');
$markup[] = $view->formHidden('APC_UPLOAD_PROGRESS', uniqid(), $apcAttribs);
}
else if (Zend_File_Transfer_Adapter_Http::isUploadProgressAvailable()) {
$uploadIdAttribs = array('id' => 'progress_key');
$markup[] = $view->formHidden('UPLOAD_IDENTIFIER', uniqid(), $uploadIdAttribs);
}
/*
if ($element->isArray()) {
$name .= "[]";
$count = $element->getMultiFile();
for ($i = 0; $i < $count; ++$i) {
$htmlAttribs = $attribs;
$htmlAttribs['id'] .= '-' . $i;
$markup[] = $view->formFile($name, $htmlAttribs);
}
}
else {$markup[] = $view->formFile($name, $attribs);}
*/
$markup = implode($separator, $markup);
switch ($placement) {
case self::PREPEND: return $markup . $separator . $content;
case self::APPEND:
default: return $content . $separator . $markup;
}
}
}
?>
Form setup in controller action:
$form = new Zend_Form();
$form->addElement(new Zend_Form_Element_File('file'));
$form->file->setLabel('File');
$form->file->setDescription('Description goes here.');
$decorators = array();
$decorators[] = array('File' => new Sys_Form_Decorator_File());
$decorators[] = array('ViewScript', array('viewScript' => '_formElementFile.phtml'));
$form->file->setDecorators($decorators);
$this->view->form = $form;
In action view:
<?php echo $this->form; ?>
In element script:
<div class="field" id="field_<?php echo $this->element->getId(); ?>">
<?php if (0 < strlen($this->element->getLabel())) : ?>
<?php echo $this->formLabel($this->element->getName(), $this->element->getLabel());?>
<?php endif; ?>
<span class="value">
<?php
echo $this->{$this->element->helper}(
$this->element->getName(),
$this->element->getValue(),
$this->element->getAttribs()
);
?>
</span>
<?php if (0 < $this->element->getMessages()->length) : ?>
<?php echo $this->formErrors($this->element->getMessages()); ?>
<?php endif; ?>
<?php if (0 < strlen($this->element->getDescription())) : ?>
<span class="hint"><?php echo $this->element->getDescription(); ?></span>
<?php endif; ?>
</div>
Output should be:
<form enctype="multipart/form-data" action="" method="post">
<dl class="zend_form">
<input type="hidden" name="MAX_FILE_SIZE" value="134217728" id="MAX_FILE_SIZE" />
<div class="field" id="field_file">
<label for="file">File</label>
<span class="value"><input type="file" name="file" id="file" /></span>
<span class="hint">Description goes here.</span>
</div>
</dl>
</form>
A problem with this solution is that the hidden elements don't render within the viewscript; this might be a problem if you're using the div as a selector in a client-side script...
What I've realized is, a custom decorator class will handle most fields except File fields.
Make sure your class implements the interface like so:
class CC_Form_Decorator_Pattern
extends Zend_Form_Decorator_Abstract
implements Zend_Form_Decorator_Marker_File_Interface
This worked for me.
This helped me fix my problem. I adjusted the code to wrap the file element inside a table. To make it work, simply remove the label from the viewdecorator and add the file element as follows:
$form->addElement('file', 'upload_file', array(
'disableLoadDefaultDecorators' => true,
'decorators' => array(
'Label',
array(array('labelTd' => 'HtmlTag'), array('tag' => 'td', 'class' => 'labelElement')),
array(array('elemTdOpen' => 'HtmlTag'), array('tag' => 'td', 'class' => 'dataElement','openOnly' => true, 'placement' => 'append')),
'File',
array('ViewScript', array(
'viewScript' => 'decorators/file.phtml',
'placement' => false,
)),
array(array('elemTdClose' => 'HtmlTag'), array('tag' => 'td', 'closeOnly' => true, 'placement' => 'append')),
array(array('row' => 'HtmlTag'), array('tag' => 'tr'))
),
'label' => 'Upload',
'required' => false,
'filters' => array(),
'validators' => array(array('Count', false, 1), ),
));
I've found a work-around that avoids the ViewScript altogether.
First, the element definition:
$this->addElement('file', 'upload_file', array(
'disableLoadDefaultDecorators' => true,
'decorators' => array(
'File',
array(array('Value'=>'HtmlTag'), array('tag'=>'span','class'=>'value')),
'Errors',
'Description',
'Label',
array(array('Field'=>'HtmlTag'), array('tag'=>'div','class'=>'field file')),
),
'label' => 'Upload File',
'required' => false,
'filters' => array('StringTrim'),
'validators' => array(),
));
Second, after the form class has been instantiated, I mimic the behavior of my ViewScript:
$field = $form->getElement('upload_file');
$decorator = $field->getDecorator('Field');
$options = $decorator->getOptions();
$options['id'] = 'field_' . $field->getId();
if ($field->hasErrors()) {
$options['class'] .= ' errors';
}
$decorator->setOptions($options);
I guess that I should look into class-based decorators. Maybe there's more flexibility there?
The easiest thing to do is to add no markup at all to the output in your custom File Decorator:
class Custom_Form_Decorator_File extends Zend_Form_Decorator_File {
public function render($content) {
return $content;
}
}
now you can do whatever you want in your viewscript for this file element (output the file input field and all hidden fields you need on your own).
Just in case you have followed #Shaun's answer and you are still getting the error: make sure that you've disabled default decorators for the element in question (take look at line 2):
$this->addElement('file', 'upload_file', array(
'disableLoadDefaultDecorators' => true,
'decorators' => array('File', array('ViewScript', array(
'viewScript' => '_form/file.phtml',
'placement' => false,
))),
'label' => 'Upload',
'required' => false,
'filters' => array(),
'validators' => array(array('Count', false, 1),),
));