ezPublish - fetch objects published between dates - ezpublish

I need to fetch articles (or objects) published between "now" and "a month before". Code looks like this:
{def $date = currentdate()}
{def $day = currentdate()|datetime( 'custom', '%d' )}
{def $month = currentdate()|datetime( 'custom', '%m' )}
{def $year = currentdate()|datetime( 'custom', '%Y' )}
{def $fromDate = maketime(0,0,0,$month|dec(), $day, $year)}
{def $items = fetch(content, list, hash(
'parent_node_id', 10,
'attribute_filter', array('and',
array('article/publish_date', '<=', $data),
array('article/publish_date', '>', $fromDate),
array( 'priority', '>=', 0 )),
'class_filter_type', 'include',
'class_filter_array', array('article')
))}
Unfortunately, nothing comes out. When I remove the $fromDate array from the fetch, it works.

In short, your template fetch code is inaccurate (either here or in your template).
The line, "array('article/publish_date', '<=', $data)," should be "array('article/publish_date', '<=', $date)," instead to fix the variable typo.
Also your date timestamp comparison logic (operator) usage is also incorrect (I think). It has been a while since I last did what you are doing but when I did it last I used the following logic instead.
{* Example: http://example/layout/set/content_statistics.csv/(startDate)/2010-07-14/(endDate)/2010-07-19/ *}
{def $articles=false()}
{if and( is_set( $view_parameters.startDate ), is_set( $view_parameters.endDate ) )}
{def $startDate=$view_parameters.startDate|explode("-")
$endDate=$view_parameters.endDate|explode("-")}
{def $startDateTimestamp=maketime(0,0,0,$startDate.1,$startDate.2,$startDate.0)
$endDateTimestamp=maketime(23,59,59,$endDate.1,$endDate.2,$endDate.0)}
{set $articles=fetch( 'content', 'list', hash(
'parent_node_id', 77,
'class_filter_type', 'include',
'class_filter_array', array( 'article' ),
'sort_by', array( 'published', false() ),
'attribute_filter', array( array( 'published', '>=', $startDateTimestamp ),
array( 'published', '<', $endDateTimestamp ) )
) )}
{else}
{set $articles=fetch( 'content', 'list', hash(
'parent_node_id', 77,
'class_filter_type', 'include',
'class_filter_array', array( 'article' ),
'sort_by', array( 'published', false() ),
'limit', 10000
) )}
{/if}
Notice that the attribute filter date timestamp comparison operators use a very different formula, re: '>=' '<' and I think this example will work better for you.
'attribute_filter', array( array( 'published', '>=', $startDateTimestamp ),
array( 'published', '<', $endDateTimestamp ) )
) )}
I hope this helps!
Cheers,
Heath
Note: I borrowed the above example from eZpedia but was afraid to link here as I am unsure of the policy of linking to community documentation (even when it has the answer to the question being asked): http://www.ezpedia.org/en/solution/how_to_fetch_content_based_on_view_parameter_date_range

Related

Woocommerce / sort related products by price

I added this filter to functions.php, but unfortunately it doesn't work. May anybody please help to find the reason
add_filter( 'woocommerce_grouped_children_args', 'custom_grouped_children_args', 18 );
function custom_grouped_children_args( $args ){
$args = array(
'post_type' => 'product',
'posts_per_page' => 4,
'orderby' => 'meta_value_num',
'meta_key' => '_price',
'order' => 'desc'
);
}
just found out the solution, in case anybody needs it. I just had to remove the default filter and then to add mine
function custom_remove_hook(){
remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
add_action( 'woocommerce_after_single_product_summary', 'custom_function_to_sort_related', 22 );
}
add_action( 'woocommerce_after_single_product_summary', 'custom_remove_hook' );
function custom_function_to_sort_related( $args = array() ) {
global $product;
if ( ! $product ) {
return;
}
$defaults = array(
'posts_per_page' => 4,
'columns' => 4,
'orderby' => 'price', // #codingStandardsIgnoreLine.
'order' => 'desc'
);
$args = wp_parse_args( $args, $defaults );
// Get visible related products then sort them at random.
$args['related_products'] = array_filter( array_map( 'wc_get_product', wc_get_related_products( $product->get_id(), $args['posts_per_page'], $product->get_upsell_ids() ) ), 'wc_products_array_filter_visible' );
// Handle orderby.
$args['related_products'] = wc_products_array_orderby( $args['related_products'], $args['orderby'], $args['order'] );
// Set global loop values.
wc_set_loop_prop( 'name', 'related' );
wc_set_loop_prop( 'columns', apply_filters( 'woocommerce_related_products_columns', $args['columns'] ) );
wc_get_template( 'single-product/related.php', $args );
}

Yii Sortable Column in CGridView

I have a calculated column I wanna have sortable.
I have a get method for it...
public function getCur_margin() {
return number_format(($this->store_cost / $this->store_price) / $this->store_cost * 100, 2) . '%';
}
And I followed this thread...
CGridview custom field sortable
But now it's looking for an actual column in the database. How can I fetch a field using <calculations> AS cur_margin only temporarily for this CActiveDataProvider?
Here is the controller code for the provider:
$dataProvider = new CActiveDataProvider('Products', array(
'criteria' => array(
'condition' => 'store_id IN (SELECT `id` FROM `stores` WHERE `user_id` = ' . Yii::app()->user->id . ')',
),
'pagination' => array(
'pageSize' => 15,
),
'sort'=>array(
'attributes'=>array(
'cur_margin' => array(
'asc' => 'cur_margin ASC',
'desc' => 'cur_margin DESC',
),
),
),
));
Have you tried to specify your custom field in select property of CDbCriteria?
'select' => array(
..., // fields to select
'(<calculations>) AS cur_margin'
)
You don't have to declare getCur_margin() method then but only declare a public $cur_margin member in your model. All required calculations will be done in SQL query.
You can also refer to this thread as an example.

WooCommerce Custom Loop to get all product from one specific category

I try to find the code (short code) in the woo comm plugin that made the list of all the product from one category, to be able to modify it... no luck after 1 hours, still no find.
So i start coding it myself (reinventing the wheel) and here what i try to get
get me all the product from category ID="151" and be able to output the name, the permalink etc etc...
this is the code now, that return everything.... way too much ! and i don't know how to filter it
{
$args = array(
'post_type' => 'product',
'posts_per_page' => 99
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
//echo get_title()."<br/>";
var_dump($loop);
endwhile;
}
here is the code i have found, and modify to my needs
function get_me_list_of($atts, $content = null)
{
$args = array( 'post_type' => 'product', 'posts_per_page' => 10, 'product_cat' => $atts[0], 'orderby' => 'rand' );
$loop = new WP_Query( $args );
echo '<h1 class="upp">Style '.$atts[0].'</h1>';
echo "<ul class='mylisting'>";
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo '<li>'.get_the_post_thumbnail($loop->post->ID, 'thumbnail').'</li>';
endwhile;
echo "</ul>";
wp_reset_query();
}
?>
The [product_category] shortcode defined in /woocommerce/includes/class-wc-shortcodes.php is a great starting place for this, particularly as Woocommerce is constantly evolving!
The guts of it is just a standard WP_Query with some extra code added on to do pagination, setting sort orders from the Woocommerce settings and some checking to see if the products are marked as visible or not.
So if you strip out the shortcode related code and wanted a function that just got visible products from a category with a specific slug, it would look like this:
function getCategoryProducts($category_slug) {
// Default Woocommerce ordering args
$ordering_args = WC()->query->get_catalog_ordering_args();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => '12',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
),
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array( esc_attr( $category_slug ) ),
'field' => 'slug',
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
)
)
);
if ( isset( $ordering_args['meta_key'] ) ) {
$args['meta_key'] = $ordering_args['meta_key'];
}
$products = new WP_Query($args);
woocommerce_reset_loop();
wp_reset_postdata();
return $products;
}
Pass in the slug and you'll get back a standard wordpress posts collection using the same ordering that you've configured in your Woocommerce settings.
I had similar problem as I wanted to get "Fabrics" product for a "Custom Page", here is the code I used.
<ul class="products">
<?php
$args = array(
'post_type' => 'product',
'posts_per_page' => 12,
'product_cat' => 'fabrics'
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
</ul><!--/.products-->
Using the above code means you get the default inline styles, classes and other necessary tags.

filter and CActiveDataProvider in CGridView

I have these code in index action of controller:
public function actionIndex()
{
$cid = #$_GET['cid'];
$country = Country::model()->findByPk($cid);
if($cid)
$dataProvider=new CActiveDataProvider('City', array(
'criteria'=>array(
'condition'=>'ci_co_id ='.$cid,
),
));
else
$dataProvider=new CActiveDataProvider('City');
$this->render('index',array(
'dataProvider'=>$dataProvider,
'country' => $country
));
}
and these in view/index.php file:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'city-grid',
'dataProvider'=>$dataProvider,
'filter' => $dataProvider,
'columns'=>array(
array(
'name' => ' ',
'value' => '$row + 1',
),
'ci_name',
'ci_pcode',
array(
'class'=>'CButtonColumn',
),
)
));
?>
but Yii gives me this error:
CActiveDataProvider and its behaviors do not have a method or closure named "getValidators".
What is the problem?
The filter has to be a class that extends CModel. However, you don't seem to be doing any actual filtering, so you can just comment the filter line of your CGridView out.
As a side note, you have major security hole in your criteria. You are leaving yourself wide open to an SQL injection attack.
Specify your criteria as follows to let the database handler properly escape the input:
'criteria'=>array(
'condition'=>'ci_co_id =:cid',
'params'=>array(':cid'=>$cid),
),

output child terms of current custom taxonomy term with images

Just wondering if anyone can help with this - banging my head for days...
From a custom taxonomy page (taxonomy_genre.php), I need to output the child terms with images.
I'm using the 'Taxonomy Images' plugin to set the image.
Code below - thanks in advance if anyone can give me some pointers!
code #1 outputs all terms including parent terms.
code #2 outputs the correct terms but with no images
essentially I'm trying to amalgamate the two.
code #1:
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args = array(
'taxonomy' => $current_term->taxonomy,
'child_of' => $current_term->term_id,
'term_args' => array(
'orderby' => 'id',
'order' => 'ASC',
'hierarchical' => 0,
),
);
$cats = apply_filters( 'taxonomy-images-get-terms', '', $args );
foreach ($cats as $cat) {
echo '<li><a href="' . get_category_link($cat) . '" title="'. $cat->name .'">' ;
echo wp_get_attachment_image( $cat->image_id, 'detail' );
echo $cat->name ;
echo '</a></li>';
}
code #2
$current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$cats = wp_list_categories( array(
'child_of' => $current_term->term_id,
'taxonomy' => $current_term->taxonomy,
'hide_empty' => 0,
'hierarchical' => false,
'depth' => 2,
'title_li' => ''
));
foreach ((array)$cats as $cat) {
$catdesc = $cat->category_description;
echo '<li>'. wp_get_attachment_image( $cat->image_id, 'detail' ) . $cat->cat_name . '</li>'; }
Sorted - used:
'parent' => $current_term->term_id,
instead of:
child_of => $current_term->term_id,

Resources