Create a plugin with new shortcodes derived from WooCommerce - wordpress

WooCommerce comes with standard shortcode [product_category].
I wanted to get a shortcode with the same behavior, but filtering per vendor (a custom taxonomy) instead of per category.
So I have replicated and modified the code of [product_category], directly in woocommerce file class-wc-shortcodes.php to create [product_vendor].
This way, it is working fine. But I know it is not proper because my shortcodes will be erased at each upgrade.
So I tried to create my own plugin to host the code of [product_vendor].
But simply moving the code into it, is not working (my website gets down). I also tried to declare global $woocommerce at the begining. Not working either. I guess it is because the code is refering to elements of class WC_Shortcodes and woocommerce functions... but I am not at ease with class so I don't know how to solve the issue...!
Here is the code I wrote in class-wc-shortcodes.php. Could you tell me what are the changes I should do so that this code is working in my own plugin ?
Many thanks
class WC_Shortcodes {
/**
* Init shortcodes.
*/
public static function init() {
$shortcodes = array(
'product' => __CLASS__ . '::product',
'product_page' => __CLASS__ . '::product_page',
'product_category' => __CLASS__ . '::product_category',
'product_vendor' => __CLASS__ . '::product_vendor', //new shortcode
...
}
(...)
/**
* Shortcode : List of products per vendor.
*/
public static function product_vendor( $atts ) {
$atts = shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'category',
'order' => 'asc',
'vendor' => '', // Slugs
'operator' => 'IN'
), $atts, 'product_vendor' );
if ( ! $atts['vendor'] ) {
return '';
}
// Default ordering args
$ordering_args = WC()->query->get_catalog_ordering_args( $atts['orderby'], $atts['order'] );
$meta_query = WC()->query->get_meta_query();
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => $atts['per_page'],
'meta_query' => $meta_query
);
$query_args = self::_maybe_add_vendor_args( $query_args, $atts['vendor'], $atts['operator'] );
if ( isset( $ordering_args['meta_key'] ) ) {
$query_args['meta_key'] = $ordering_args['meta_key'];
}
$return = self::product_loop( $query_args, $atts, 'product_cat' );
// Remove ordering query arguments
WC()->query->remove_ordering_args();
return $return;
}
(...)
private static function _maybe_add_vendor_args( $args, $vendor, $operator ) {
if ( ! empty( $vendor ) ) {
$args['tax_query'] = array(
array(
'taxonomy' => 'yith_shop_vendor',
'terms' => array_map( 'sanitize_title', explode( ',', $vendor ) ),
'field' => 'slug',
'operator' => $operator
)
);
}
return $args;
}
In my own plugin, I have "copy paste" the code, with minor changes. But if I activate this plugin, my site can't be launched at all. I also tried to include my function in "class WC_Shortcodes", or to add/remove the declaration of global variable $woocommerce... Here is what I wrote in my plugin :
function test_shortcode_in_my_plugin ($atts){
global $woocommerce;
$atts = shortcode_atts( array(
'per_page' => '12',
'columns' => '4',
'orderby' => 'category',
'order' => 'asc',
'vendor' => '', // Slugs
'operator' => 'IN' // Possible values are 'IN', 'NOT IN', 'AND'.
), $atts, 'product_vendor' );
if ( ! $atts['vendor'] ) {
return ''; //si aucune catégorie n'est spécifiée, ne rien retourner
}
// Default ordering args
$ordering_args = WC()->query->get_catalog_ordering_args( $atts['orderby'], $atts['order'] );
$meta_query = WC()->query->get_meta_query();
$query_args = array( // construction du tableau de résultats
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => $atts['per_page'],
'meta_query' => $meta_query
);
$query_args = self::_maybe_add_vendor_args( $query_args, $atts['vendor'], $atts['operator'] );
// Fonction qui ajoute la condition sur le "vendor" dans la requete
if ( isset( $ordering_args['meta_key'] ) ) {
$query_args['meta_key'] = $ordering_args['meta_key'];
}
$return = self::product_loop( $query_args, $atts, 'product_cat' );
// Remove ordering query arguments
WC()->query->remove_ordering_args();
return $return;
}
add_shortcode( 'test_shortcode', 'test_shortcode_in_my_plugin' );

Related

How can i get list of options of some attribute of products in one of the categories in wordpress+woocommerce?

i want to make this thing - get a list of options of some attribute of products in category with known id. So, the category id is $catid, the attribute is $attr.
So, to my mind, i have to do these steps:
0) get all the products ids from category with id
get all attributes of products with these ids
if product's attribute is $somename, get it's option, store it in array $options
Finally there must be an array like [s,m,l,xl,xxl] for example, if the searching attribute is "size" and the category is "t-shirts".
So what is done:
function get_options_of_attribute() {
$catid = "6776"; // T-shirts
$attr = "size";
$all_ids = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $current_cat_id,
'operator' => 'IN'
)
),
));
$ids = '';
$values = '';
foreach ( $all_ids as $id ) {
$ids .= $id.' ';
foreach( wc_get_product_terms( $id, $attr ) as $attribute_value ){
$values .= $attribute_value.' ';
}
}
// return $ids;
return $values;
}
add_shortcode( 'listofoptions', 'get_options_of_attribute' );
Nothing happens. The part of code, which generates the $ids is working, something wrong with wc_get_product_terms() i think...
Finally i solved it with:
function get_options_of_attribute() {
$current_cat_id = "6776"; // T-shirts
$attr = "size";
$all_ids = get_posts( array(
'post_type' => 'product',
'numberposts' => -1,
'post_status' => 'publish',
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $current_cat_id,
'operator' => 'IN'
)
),
));
$ids = '';
$value = array();
$values = '';
foreach ( $all_ids as $id ) {
$ids .= $id.' ';
$value[] = wc_get_product_terms($id,'pa_'.$attr, array( 'fields' => 'names'))[0];
}
$uniquevalue = array_unique($value);
foreach ( $uniquevalue as $uv ) {
$values .= $uv.' ';
}
return $values;
}
add_shortcode( 'listofoptions', 'get_options_of_attribute' );

WooCommerce get all products with SKU

I want to get a list of products with sku and post id with this code, everything seems fine and ok with this code :
$statuses = array('publish', 'draft');
// Args on the main query for WC_Product_Query
$args = [
'status' => $statuses,
'orderby' => 'name',
'order' => 'ASC',
'limit' => -1,
];
$vendor_products = wc_get_products($args);
$list_array = array();
foreach ($vendor_products as $key => $product) {
if ($product->get_type() == "variable") {
// Args on product variations query for a variable product using a WP_Query
$args2 = array(
'post_parent' => $product->get_id(),
'post_type' => 'product_variation',
'orderby' => array( 'menu_order' => 'ASC', 'ID' => 'ASC' ),
'fields' => 'ids',
'numberposts' => -1,
);
foreach ( get_posts( $args2 ) as $child_id ) {
// get an instance of the WC_Variation_product Object
$variation = wc_get_product( $child_id );
if ( ! $variation || ! $variation->exists() ) {
continue;
}
$list_array[] = array(
'sku' => $variation->get_sku(),
'postid' => $variation->get_id()
);
}
} else {
$list_array[] = array(
'sku' => $product->get_sku(),
'postid' => $product->get_id()
);
}
}
I have total 1660 (470 published and 1,190 drafted) products but it's just returns 501 products and i don't know why!
this is my products in woocommerce:
this is the final result of query :
this is the result of Janki's code
// Woocommerce get all products
$args = array(
'post_type' => array('product','product_variation'),
'meta_key' => '_sku',
'post_status' => array('publish','draft'),
'meta_query' => array(
array(
'key' => '_sku',
'compare' => 'EXISTS',
),
),
);
$products = new WP_Query($args);
/* using this code you can get all products */
/* this may help you to get details */

Create a page with child pages and have each child have it's own page template

I have some code that creates pages with child pages in WordPress. What i would like to do is have each child have its own page template.
As you see below, i am creating child pages of Script and Assets. Currently the child page is created with one page_Full.php added to everything but i would like to have Script and Assets have their own page template. Ideas would be helpful.
Thanks in advance.
function CreatePage(){
$post_title = $_POST['ProjectName'];
$post_excerpt = $_POST['ProjectDiscript'];
$tags = $_POST['TypeOption'];
$pages = array(
array(
'name' => $post_title,
'title' => $post_title,
'child' => array(
'script' => $post_title.'_Script',
'assets' => $post_title.'_Assets'
)
)
);
$template = array(
'post_type' => 'page',
'post_status' => 'publish',
'post_author' => $user_id,
'post_excerpt' => $post_excerpt,
'tags_input' => array($tags),
);
foreach( $pages as $page ) {
$exists = get_page_by_title( $page['title'] );
$my_page = array(
'post_name' => $page['name'],
'post_title' => $page['title'],
'page_template' => 'page_Full.php'
);
$my_page = array_merge( $my_page, $template );
$id = ( $exists ? $exists->ID : wp_insert_post( $my_page ) );
if( isset( $page['child'] ) ) {
foreach( $page['child'] as $key => $value ) {
$child_id = get_page_by_title( $value );
$child_page = array(
'post_name' => $key,
'post_title' => $value,
'post_parent' => $id,
'page_template' => 'page_Full.php'
);
$child_page = array_merge( $child_page, $template );
if( !isset( $child_id ) ) wp_insert_post( $child_page );
}
}
}
wp_die();
}
add_action( 'wp_ajax_CreatePage','CreatePage' );
Had the idea to create an array for every child page - and it seams to work. Perhaps there is still a better way but for the time being - This does the trick!
$pages = array(
array(
'name' => $post_title,
'title' => $post_title,
'child' => array(
'script' => array(
'Pname' => $post_title.'_Script',
'Ptemplate' => 'page_Full_Script.php'
),
'assets' => array(
'Pname' => $post_title.'_Assets',
'Ptemplate' => 'page_Full_Assets.php'
),
'settings' => array(
'Pname' => $post_title.'_Settings',
'Ptemplate' => 'page_Full_Settings.php'
)
)
)
);
Here is the child with sub arrays.
if( isset( $page['child'] ) ) {
foreach( $page['child'] as $key[] => $value ) {
$child_id = get_page_by_title( $value );
$child_page = array(
'post_name' => $value['Pname'],
'post_title' => $value['Pname'],
'post_parent' => $id,
'page_template' => $value['Ptemplate'],
);
$child_page = array_merge( $child_page, $template );
if( !isset( $child_id ) ) wp_insert_post( $child_page );
}
}
And here is where the child array is set as var to be iterated through so that the info can be inserted into the child_page var to be merged and the post inserted.
Thanks to any who took the time to review.
Cheers

Issue with function and post ID

I am using the following function/shortcode in order to output an average global rating using ACF :
function get_average_rating($post_id) {
$rating_sum = 0;
$reviews_of_post = get_posts( array(
'post_type' => 'avis',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'produit',
'value' => $post_id,
'compare' => '=',
),
),
) );
if ( empty( $reviews_of_post ) ) {
return 0;
}
foreach ( $reviews_of_post as $review ) {
$rating_sum += get_field( 'note_client', 'post_' . $review->ID);
}
return number_format((float)($rating_sum / count( $reviews_of_post )),1, ',', '');
}
add_shortcode( 'note-clients', 'get_average_rating');
It always return 0 except when I manually input the post ID like :
'meta_query' => array(
array(
'key' => 'produit',
'value' => 1234,
'compare' => '=',
),
),
How can I fix this ?
Thanks a lot !
Declare global $post; before your function.
Wordpress uses $post for many of its functions within the loop.
To avoid any conflicts later you should consider use of wp_reset_query
1 . Change key 'key' => 'produit' to 'key' => 'product',
2 .
foreach ( $reviews_of_post as $review ) {
$rating_sum += get_field( 'note_client', 'post_' . $review->ID);
}
**to**
foreach ( $reviews_of_post as $review ) {
$post_id = 'post_' . $review->ID ;
$rating_sum += get_field( 'note_client', $post_id );
}
Confirm ACF key (note_client) is correct
3 . Change this
add_shortcode( 'note-clients', 'get_average_rating');
to
add_shortcode( 'average_rating', 'get_average_rating');
Please try with your post static id:
$post_id = 9; //add here your static post id
$reviews_of_post = get_posts( array(
'post_type' => 'avis',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'produit',
'value' => $post_id,
'compare' => '=',
),
),
) );

How add arguments to query

i use a Toolset plugin to make view on WP and they give us possibility to manipulate with API.
I try to add argument to my view but it doesn't work.
here is the function :
add_filter( 'wpv_filter_query', 'add_city_tax', 99, 3 );
function add_city_tax( $query_args, $view_settings, $view_id )
{
if($view_id == 7706)
{
$args = array(
'tax_query' => array
(
array
(
[taxonomy] => 'ville',
[field] => 'id',
[terms] => Array
(
[0] => 220
),
[operator] => 'IN'
),
[relation] => 'OR',
),
);
}
$query_args = new WP_Query( $args );
return $query_args;
}
the page make an error
The method you are declaring args is not correct. You should not create array like that. Please check following example.
function add_city_tax( $query_args, $view_settings, $view_id ) {
if($view_id == 7706) {
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'ville',
'field' => 'id',
'terms' => array( 220 ),
'operator' => 'IN',
),
'relation' => 'OR',
),
);
}
$query_args = new WP_Query( $args );
return $query_args;
}
And here doc
Description
When displaying a View listing posts, this filter is applied to the arguments being generated by the View settings before they are passed to the WP_Query class.
Views filters - wpv_filter_query
Note that the filters that you can add to the View are also hooked here, each of them using a different priority that gets up to 100. To ensure that your filter runs after them, you would need to use a higher priority number.
Remember that the filter can take three parameters. If you pass more than one, you need to specify it in your code using the fourth argument:
1
add_filter( 'wpv_filter_query', 'my_callback_function', 99, 3 );
Arguments
array $query_args Τhe query arguments as in WP_Query.
array $view_settings The View settings.
int $view_id The View ID.
No it doesn't work like this.
i have tried like this :
function add_city_tax( $query_args, $view_settings, $view_id ) {
if($view_id == 7706) {
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'ville',
'field' => 'id',
'terms' => array( 220 ),
'operator' => 'IN',
),
'relation' => 'AND',
),
);
}
$query_args[] = $args ;
$msg = '<pre>' . print_r($query_args, true) . '</pre>';
mail('franck#efficonex.fr', 'test', $msg);
return $query_args;
}
add_filter( 'wpv_filter_query', 'add_city_tax', 99, 3 );
But it is the same result without filter.
here is the example of api
//Return only posts from the current author when listing posts of type company:
add_filter( 'wpv_filter_query', 'prefix_show_only_current_author' );
function prefix_show_only_current_author( $query_args ) {
global $current_user;
$types = (array) $query_args['post_type'];
if ( !is_admin() && in_array( 'company', $types ) ) {
$query_args['author'] = empty( $current_user->ID ) ? -1 : $current_user->ID;
}
return $query_args;
}

Resources