Woocommerce - Group by first letter - wordpress

I am trying to display the products in my Shop page like that:
A
Asd
Asddd
B
Beer
Bear
and so on. I managed to do this for the categories by overriding and using the woocommerce_output_product_categories action and for them it works, but I want to do this for the products as well(since Woocommerce gives you the option to show products or categories in the shop page). Thank you!

There can be so many possible solutions. But for me this can be done like this:
add_action( 'woocommerce_shop_loop', 'wc_shop_loop', 30 );
function wc_shop_loop() {
global $product, $last_title_first_letter_95845949545454;
$title = $product->get_title();
if ( $last_title_first_letter_95845949545454 !== $title[0] ) {
$last_title_first_letter_95845949545454 = $title[0];
woocommerce_product_loop_end(); // let's close the loop.
echo '<h3>'.$last_title_first_letter_95845949545454. '</h3>'; // add a letter heading.
woocommerce_product_loop_start(); // open a new loop start.
}
}
Tested to work on shop page and product category page.
You will need to work on its css.

Related

WooCommerce - Open the new product that the category is selected

I've been searching all over the internet for hours how to automatically assign a category to new products. When you open the new product that the category is selected.
I have two product categories (Books and FIlms) and I would like that every time we open a new product, Books are selected. In this way it will directly show the custom fields of that product category and we will speed up the creation process.
It's possible?
thanks
UPDATE
I found a code that seems to work with the posts. Any idea how I can adapt the code for products and where should I put the category I care about?
https://wordpress.stackexchange.com/questions/243462/automatically-select-categories-on-new-post-based-on-get-value
When it comes to the checkbox that should already be checked when creating a new product, you can use jQuery
Replace in-product_cat-15 with in-product_cat-{YOUR-CAT-ID}
// Define the admin_head callback
function action_admin_head() {
global $post, $pagenow;
// Only on new product pages
if( $pagenow != 'post-new.php' ) return;
?>
<script>
jQuery(function($){
$( '#in-product_cat-15' ).attr( 'checked', true );
});
</script>
<?php
}
add_action( 'admin_head', 'action_admin_head', 10, 0 );
In product categories click the Make default action of your desired category to make it as default category. For more details refer the image:
You can visit this page using this URL (replace the domain with your domain):
www.example.com/wp-admin/edit-tags.php?taxonomy=product_cat&post_type=product

Woocommerce: only allow products from one tag per transaction

I have a WooCommerce site with the Responsive Supermarket Online Theme - Oswad installed. Each product has one of 4 tags corresponding to a different vendor. What I need to do when adding to cart:
Check the cart for products and get their tag.
If cart is empty, or the product tags match, allow adding to cart as per normal.
If the product has a different tag then that of the cart contents, disable the add to cart button.
Steps 1-3 should only occur once per transaction.
I know that I can check the tag with if ( has_term( 'tag', 'product_tag') ),
and in the add-to-cart.php, $product->isPurchasable() will toggle the visibility of the button.
My stumbling block is finding out how to put all these together to fulfill the requirement. I tried looking for how to restrict the cart items by categories but came up with dead ends. I looked for plugins but those restricted only the minimum/maximum product per order and not checking the category/tags of the items. I even tried to look at the hooks but ended up getting more confused.
Would appreciate it if I could get a pointer in the right direction.
One possible solution - at the add-to-cart.php:
global $woocommerce, $product;
$terms = wp_get_post_terms($product->id, 'product_tag');
$items = $woocommerce->cart->get_cart();
foreach($items as $item => $values){
$_product = $values["data"];
$_terms = wp_get_post_terms($_product->id, 'product_tag');
//echo "product slug: " . $terms[0]->slug;
//echo "cart slug: " . $_terms[0]->slug;
if ($terms[0]->slug !== $_terms[0]->slug){
//echo "NO MATCH";
$match = false;
}
}
and then add a if ($match) statement controlling the add to cart button: apply_filters('woocommerce_loop_add_to_cart_link', ...
Also, add the code to woocommerce/single-product/add-to-cart/simple.php and add the if statement to the location of the add to cart button.

Include only specific categories in WooCommerce product categories widget

I'm using WooCommerce plugin for Wordpress. It comes with a widget called WooCommerce Product Categories which can display a drop-down of all your product categories. I have searched online and found the following code which will exclude certain categories from appearing, categories with ID 16 and 20 in this snippet:
add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );
function woo_product_cat_widget_args( $cat_args ) {
$cat_args['exclude'] = array('16','20');
return $cat_args;
}
What I need is the opposite. I want a filter/function similar to above, but which enables me to specify which categories to include - i.e. exclude everything but the IDs that I specify.
You can try this;
add_filter( 'woocommerce_product_categories_widget_args', 'woo_product_cat_widget_args' );
function woo_product_cat_widget_args( $cat_args ) {
$cat_args['include'] = array('16','20');
return $cat_args;
}
actually you can use any of these arguments that listed on this page https://codex.wordpress.org/Template_Tags/wp_list_categories
Hope that helps!

Get custom field value but hide first 19 and last 2 characters

Geeks and code Gods,
I am having an issue with my wordpress code. I have added 3 extra columns to the dashboard's pages screen.
One of which displays the custom field 'scode'. The scode custom field holds the cart66 shortcode which adds an 'add cart' button to the page it is assigned to.
The shortcode looks like this:
[add_to_cart item="HD-NL-7010"]
Each page has a different shortcode.
Every shortcode is the same length, 31 characters long.
However Id prefer not to show the whole shortcode in the column, only the product code *HD-NL-7010* section of the code.
Is it possible to hide the first 19 and last 2 characters. Basically removing [add_to_cart item=" and "]
Below is the code I added to function.php to add the custom columns to the pages table:
function test_modify_post_table( $column ) {
$column['scode'] = 'Product Code';
$column['Price'] = 'Price';
$column['Product_Image'] = 'Image';
return $column;
}
add_filter( 'manage_pages_columns', 'test_modify_post_table' );
function test_modify_post_table_row( $column_name, $post_id ) {
$custom_fields = get_post_custom( $post_id );
switch ($column_name) {
case 'scode' :
echo $custom_fields['scode'][0];
break;
case 'Price' :
echo '£' . $custom_fields['Price'][0];
break;
case 'Product_Image' :
echo $custom_fields['Product_Image'][0];
break;
default:
}
}
add_filter( 'manage_pages_custom_column', 'test_modify_post_table_row', 10, 2 );
Thank you for reading this post. Any help would be greatly appreciated.
P.S. I'm going to ask this in another post but does anyone know how to get the Product_Image to show as an image in the page column rather than the image attachment ID?
I might be misunderstanding, does PHP's substr help? Something like:
case 'scode' :
echo substr( $custom_fields['scode'][0], 19, 10 );
break;
For what it's worth, I think I'd store just the code (HD-NL-7010) in the custom field, rather than the shortcode code, then apply the shortcode where needed (with do_shortcode).

Translate product category|tag title for JigoShop with qTranslate

qTranslate creates additional language fields for products pages in JigoShop but not also for category|tags product as it does for posts.
If I put in the title of a menu item <!--:en-->title<!--:--><!--:fr-->title<!--:--> i'll get the translation I want. But when creting a new category|tag title the <!--:--> is striped out. How can I enable comments tags for cat|tag title?
Another option is to use [:en]Title[:fr]Titre in the same title field when creating a new category|tag product. On the admin panel I see the proper text for the language selected but for end user i see [:en]Title[:fr]Titre.
I found this link https://wordpress.stackexchange.com/questions/28165/translating-a-custom-taxonomy and according to this answer http://www.qianqin.de/qtranslate/forum/viewtopic.php? f=4&t=2045&start=0#p7380 I addet in functions.php
add_action('jigoshop_add_form', 'qtrans_modifyTermFormFor');
add_action('jigoshop_edit_form', 'qtrans_modifyTermFormFor');
Did not work. I don't see aditional translations fields for categories|tags in JigoShop.
The basic question is:
How do I translate product categories|tags in JigoShop using qTranslate?
I was having very similar problem. I found a solution here: http://www.qianqin.de/qtranslate/forum/viewtopic.php?f=3&t=4064&start=10#p12940
Basically, all you have to do is to rename all the categories as suggested in the answer above, then add to your theme's functions.php file:
function p_filter_categories( $categories ) {
if ( is_array( $categories ) ) {
foreach ( $categories as $i => $cat ) {
$categories[ $i ]->name = __( $cat->name );
}
} else {
$categories->name = __( $categories->name );
}
return $categories;
}
add_filter( 'get_the_categories', 'p_filter_categories', 10 );
add_filter( 'get_the_terms', 'p_filter_categories', 10 );
add_filter( 'get_term', 'p_filter_categories', 10 );
Hope that helps!
Not the ideal solution but works.
In JigoShop/edit product category/name:
[:en] Big [:fr] Grand
In functions.php
function translate_q ($echo) {
if (function_exists('qtrans_split')) {
$selectLanguage = qtrans_split($echo);
return $selectLanguage[qtrans_getLanguage()];
} else {
return $echo;
}
}
qtrans_split and qtrans_getLanguage are functions created by qTranslate.
From JigoShop plugin directory I opened jigoshop_template_functions.php, I grabbed from jigoshop_breadcrumb() function all the echos in $echo variable and at the end I have:
echo function_exists('translate_q') ? translate_q($echo) : $echo;
You will have to do the same thing in other places in JigoShop. I posted here to be a starting point.

Resources