Add product attribute to Woocommerce's blocks in Gutenberg - wordpress

EDIT I: I have found the file where the old plugin Woocommerce Blocks sets the blocks: https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/master/src/BlockTypes/FeaturedCategory.php But where is it in the Woocommerce library?
EDIT II: Question in short:
How do you customize the Woocommerce Blocks to show more data than the build in functionality?
------------- background ------------
If you search for adding custom attributes for Woocommerce Blocks you find a lot of WordPress examples for this.
For example, this, where the answer points out, that you can add attributes by using the blocks.registerBlockType. But how to do this for Woocommerce Blocks?
I want to be able to add a data field to the HTML output. The data field should then call a product attribute and show if it exists.
So when you use the Woocommerce Blocks on your front page - for example, the size will be shown underneath the add to cart button - as in the image.
As you might know the functionality of showing/hiding the price, add-to-cart-button, reviews are already there, when you choose a Woocommerce Block on the editing site.
But I haven't found the place where this functionality is created.
This would also be a great help actually - if you could show me where in the Woocommerce Github library the blocks are being created. Maybe I can figure out my self how to filter through them and add the functionality
I know - based on a Udemy course - how to create a custom plugin and create a new blog-type, save and edit.
But I need to figure out what Woocommerce namespace is, how they create their blocks, and what their data is called. The Woocommerce developer handbook is not saying anything about this - not what I've found.
I've been searching the internet for three days now, and I just don't understand that I can't seem to find ANYTHING on this. That nobody else wants to customize this functionality in Woocommerce. I know it is a new function (blocks) in the core, but still.
I just need to be pointed in the right direction.

I was dealing with the exact same problem as you and I found the answer by digging deeply on the WC blocks plugin repo.
I found that you have to apply a filter to this hook: woocommerce_blocks_product_grid_item_html
The original HTML is this:
<li class="wc-block-grid__product">
<a href="{$data->permalink}" class="wc-block-grid__product-link">
{$data->image}
{$data->title}
</a>
{$data->badge}
{$data->price}
{$data->rating}
{$data->button}
</li>
So that way you can filter the html code and modify it by adding this chunk of code to your functions.php and customizing it to fit your needs
function wc_add_date_to_gutenberg_block( $html, $data, $product ) {
$dateStr = get_post_meta($product->get_id(), 'ticket_start_time', true);
$date = new DateTime($dateStr);
$data->date = "<p>Date: " . $date->format('d-m-Y H:i') . "</p>";
$output = "
<li class=\"wc-block-grid__product\">
<a href=\"{$data->permalink}\" class=\"wc-block-grid__product-link\">
{$data->image}
{$data->title}
</a>
{$data->date} <- I added this one
{$data->badge}
{$data->price}
{$data->rating}
{$data->button}
</li>
";
return $output;
}
add_filter("woocommerce_blocks_product_grid_item_html", "wc_add_date_to_gutenberg_block", 10, 3);

I'm not totally clear on what you're asking. You reference Gutenberg Blocks often, but have linked to a WooCommerce repository that doesn't have any Gutenberg Blocks.
But if I'm understanding you correctly, you're looking for the PHP template that controls products. You can find in content-product.php
You'll see a lot of calls to do_action which is core to WordPress hooks as used in plugin development.
<li <?php wc_product_class( '', $product ); ?>>
<?php
/**
* Hook: woocommerce_before_shop_loop_item.
*
* #hooked woocommerce_template_loop_product_link_open - 10
*/
do_action( 'woocommerce_before_shop_loop_item' );
/**
* Hook: woocommerce_before_shop_loop_item_title.
*
* #hooked woocommerce_show_product_loop_sale_flash - 10
* #hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
/**
* Hook: woocommerce_shop_loop_item_title.
*
* #hooked woocommerce_template_loop_product_title - 10
*/
do_action( 'woocommerce_shop_loop_item_title' );
/**
* Hook: woocommerce_after_shop_loop_item_title.
*
* #hooked woocommerce_template_loop_rating - 5
* #hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
/**
* Hook: woocommerce_after_shop_loop_item.
*
* #hooked woocommerce_template_loop_product_link_close - 5
* #hooked woocommerce_template_loop_add_to_cart - 10
*/
do_action( 'woocommerce_after_shop_loop_item' );
?>
</li>
If you do a search for the action hooks defined in content-product.php, you'll find them defined in wc-template-hooks.php. Hooks are named actions that functions are added to. For example if you look into the woocommerce_after_shop_loop_item action, you'll find these two functions being attached to it.
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
The woocommerce_template_loop_product_link_close and woocommerce_template_loop_add_to_cart functions are defined in wc-template-functions.php
You could create an entirely new content-product.php file in your theme by creating a file in yourtheme/woocommerce/content-product.php, however you then lose a lot of the built in power and compatibility of WooCommerce.
Better would be to remove then add new actions to the woocommerce_after_shop_loop_item hook. For example, woocommerce_template_loop_product_link_close is currently defined as:
function woocommerce_template_loop_product_link_close() {
echo '</a>';
}
You could overwrite this by doing this in your functions.php file:
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
function custom_template_loop_product_link_close() {
echo 'Hello world!</a>';
}
add_action( 'woocommerce_after_shop_loop_item', 'custom_template_loop_product_link_close', 5 );
I hope this helps.

Related

How to disable comments in WordPress blog for non admin users?

In my WordPress website I'm allowing comment section under all articles. The requirement is to allow only for logged in users. If a user is viewing the article without logging in, Now it asks his name and email along with the comments. And it immediately reflects in the comment section. What I want is any one option from below.
Logged out user can comment. But it will be reflect only if admin accepts.
Logged out user doesn't have comment section.
How to achieve anyone requirement?
What you're trying to implement are defaults, already built-in, Wordpress behaviours.
You can either manually set that under settings > discussion and settings > general or implement the following function in your function.php.
Either use init or after_switch_theme as action hook.
<?php
/**
* Define default theme options.
* #link https://codex.wordpress.org/Option_Reference
*/
add_action( 'init', function() {
/**
* comment_moderation
* Before a comment appears, an administrator must always approve the comment.
* #param Integer
*/
update_option( 'comment_moderation', 1 ); // ... default: 0
/**
* comment_registration
* Users must be registered and logged in to comment
* #param Integer
*/
update_option( 'comment_registration', 1 ); // ... default: 0
} ); ?>

Woocommerce hook priority on single product page [duplicate]

This question already has an answer here:
WooCommerce action hooks and overriding templates
(1 answer)
Closed 2 years ago.
I would like to edit the template of my Single product page.
I tried to use the WC hook woocommerce_single_product_summary in my child's functions.php to add custom field right below the title, but unfortunately the hooks behave strange, as described below.
I tried to add this line of code:
add_action('woocommerce_single_product_summary', 'display_speaker_name', 6);
function display_speaker_name() {
echo '<p>' . get_field('speaker_name') . '</p>';
}
But what it does is, that it displays the custom field on the top of the summary section above the title. When i try to change the priority to 11, it displays at the bottom of the section, below the categories. I looked into the WC documentation and the priority of woocommerce_template_single_title hook should be 5. So if I understand that correctly, the proper priority for my custom hook should be between 6-9, if I want to display it below the title, right?
I even looked to the wc_template_hooks.php and the priority of the default WC hooks is set properly there. Also I tried to remove the original WC title hook and then add it with the priority 5, but unfortunately it did not work as well.
Do you have any ideas, how to solve this problem? I am using OceanWP theme. Thank you!
EDIT: See comments on the first answer for the solution.
From /templates/content-single-product.php
<div class="summary entry-summary">
<?php
/**
* Hook: woocommerce_single_product_summary.
*
* #hooked woocommerce_template_single_title - 5
* #hooked woocommerce_template_single_rating - 10
* #hooked woocommerce_template_single_price - 10
* #hooked woocommerce_template_single_excerpt - 20
* #hooked woocommerce_template_single_add_to_cart - 30
* #hooked woocommerce_template_single_meta - 40
* #hooked woocommerce_template_single_sharing - 50
* #hooked WC_Structured_Data::generate_product_data() - 60
*/
do_action( 'woocommerce_single_product_summary' );
?>
</div>
So to go after the whole thing, you have to use a priority higher than 60.
add_action('woocommerce_single_product_summary', 'display_speaker_name', 70);

WooCommerce - How to remove sidebar

I need to remove sidebar into my woocommerce store.
I have tried with backend in option theme and in each category but resultless.
I tried also:
1.file wp-template-hooks.php
removed--> do_action(..
2.file archive-product.php
removed--> do_action(..
insert in file function.php in theme dir and function.php in woocommerce dir
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
into database there is 2 tables with 2 fields serialised but is a risk change this.
resulless.
I finished the ideas.
Where is saved into db the visibility of sidebar? or the variable?
Can you help me?
Thanks
Ale
I just wanted to update this post for WooCommerce version 3.2.3 - 2017
OK the WooCommerce Plugin includes a folder with all the template files if you want to make changes to the template on a custom theme or child theme you need to copy all of the desired template into a folder called woocommerce in your root theme folder. This will overwrite the plugin templates and will allow for WooCommerce updates with overwriting your custom changes. These templates have all of the do_actions and hooks in the comments so it makes it easy to understand how it's working.
That said WooCommerce has hooks that allow for you to add or remove blocks of code you can check out the API Docs here.
To remove the side bar you need to add this into your functions.php file in your theme setup function
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
Please, add this code to your functions.php
function mb_remove_sidebar() {
return false;
}
add_filter( 'is_active_sidebar', 'mb_remove_sidebar', 10, 2 );
Add to functions.php the following line:
add_action( 'init', function () {
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10 );
} );
You have already integrated WooCommerce in your theme?
If not, try to do this three steps for integration WooCommerce in your theme.
After that, remove get_sidebar(); from your_theme/woocommerce.php
It works fine for me. Screenshot here.
Hope it helps.
Further to #maksim-borodov's answer, I wanted to hide the sidebar conditionally, i.e. only on Product pages. Here's how:
function remove_wc_sidebar_conditional( $array ) {
// Hide sidebar on product pages by returning false
if ( is_product() )
return false;
// Otherwise, return the original array parameter to keep the sidebar
return $array;
}
add_filter( 'is_active_sidebar', 'remove_wc_sidebar_conditional', 10, 2 );
Add to functions.php the following line:
remove_action( 'woocommerce_sidebar', 'woocommerce_get_sidebar', 10);
I am using GeneratePress (free version), so the other solutions didn't work for me.
This page gave me the answer: https://docs.generatepress.com/article/sidebar-layout/#sidebar-filter
add_filter( 'generate_sidebar_layout', function( $layout ) {
// If we are on a woocommerce page, set the sidebar
if ( function_exists( 'is_woocommerce' ) && is_woocommerce() ) {
return 'no-sidebar';
}
// Or else, set the regular layout
return $layout;
} );
This can be done by woocommerce sidebar plugin. If you want to remove by code add this code to functions.php,
if (!class_exists( 'WooCommerce' ) ) {return;}
if(is_cart() || is_checkout() || is_product() || is_shop() || is_account_page()){
?>
<style type="text/css">
#secondary {
display: none;
}
#primary {
width:100%;
}
</style>
<?php
}
}
Here is how to remove the sidebar :-
go to wp-content/plugins/woocommerce
in file single-product.php remove the following code -
<?php
/**
* woocommerce_sidebar hook
*
* #hooked woocommerce_get_sidebar - 10
*/
do_action('woocommerce_sidebar');
?>
next edit the file archive-product.php and remove the following code -
<?php
/**
* woocommerce_sidebar hook
*
* #hooked woocommerce_get_sidebar - 10
*/
do_action('woocommerce_sidebar');
?>
(tip* before deleting this, copy and paste the entire code from this page into a seperate text file on your HD...that way you can always paste the original code back into this folder if anything goes wrong)

Remove Sidebar Woo Commerce in Roots WP Theme

I am working with the Roots WP theme. In any case I have installed Woo Commerce as well and I am trying to get it configured to NOT show any sidebar on all Woo Commerce pages.
I have gone through this entire tutorial twice: http://roots.io/using-woocommerce-with-roots/
It does not address how to remove the sidebar for Roots/WooCommerce, just how to remove the duplicate headers, footers and sidebars. Check! I have that accomplished; now I just want to remove the sidebar all together.
I have added the archive-product.php, single-product.php pages into the Roots theme and inserted this line of code:
<?php woocommerce_content(); ?>
I have edited the lib/config.php file to not show a side bar on certain themes.
array(
'template-custom.php',
'template-page.php',
'template-shop.php',
'archive-product.php',
'single-product.php'
)
No avail!
I have done everything that I can possibly think of to remove the side bar. Anyone have any suggestions?
Thanks!
You can add any of the WooCommerce conditionals to the first array in the sidebar config of /lib/config.php.
I would start by adding is_woocommerce to remove the sidebar from all WooCommerce pages.
Example:
function roots_display_sidebar() {
$sidebar_config = new Roots_Sidebar(
/**
* Conditional tag checks (http://codex.wordpress.org/Conditional_Tags)
* Any of these conditional tags that return true won't show the sidebar
*
* To use a function that accepts arguments, use the following format:
*
* array('function_name', array('arg1', 'arg2'))
*
* The second element must be an array even if there's only 1 argument.
*/
array(
'is_404',
'is_front_page',
'is_woocommerce' // New Conditional for WooCommerce Pages
),
/**
* Page template checks (via is_page_template())
* Any of these page templates that return true won't show the sidebar
*/
array(
'template-custom.php'
)
);

Wordpress add_filter problem depending on permalinks setting

I wrapped code from this page: http://shibashake.com/wordpress-theme/add-tags-and-categories-to-your-wordpress-page into a Wordpress plugin, in order to allow adding categories and tags to WP pages.
It works with one exception - the filtering from the categories widget fails if my permalinks are set to default, e.g.
myblog.com/?cat=8
With a different permalink structure, e.g.
myblog.com/category/news
all is fine.
Here's the plugin code - how might I change the my_expanded_request function to accomodate all permalink types?
<?php
/**
* #package Categories and Tags For Pages
* #version 0.1
*/
/*
Plugin Name: Categories and Tags For Pages
Plugin URI: http://wordpress.org/#
Description: Expands category and tag options to include pages
Author: Me
Version: 0.1
*/
function add_page_cats_and_tags() {
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');
}
add_action( 'admin_init', 'add_page_cats_and_tags' );
function my_expanded_request($q) {
if (isset($q['tag']) || isset($q['category_name'])) {
$q['post_type'] = array('post', 'page');
}
return $q;
}
add_filter('request', 'my_expanded_request');
?>
I'm using this plugin - TagPages - and it works like a charm

Resources