woocommerce_template_single_add_to_cart not working - woocommerce

I am trying to move woocommerce_template_single_add_to_cart section under the tab section. For this, I have done below coding
add_filter( 'woocommerce_single_product_summary', 'filter_grouped_cart');
function filter_grouped_cart(){
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
But when I have placed woocommerce_template_single_add_to_cart on the tab it's showing nothing.
add_filter('woocommerce_product_tabs', 'woo_new_product_tab');
function woo_new_product_tab($tabs) {
$tabs['related_products'] = array(
'title' => __('Models', 'woocommerce'),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
return $tabs;
}
function woo_new_product_tab_content() {
add_action('woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
}
Can anyone please help me how can i do this?

This might help:
add_filter( 'woocommerce_single_product_summary', 'filter_grouped_cart', 0);
function filter_grouped_cart(){
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// add it to new action hook... this will complete the "move"
add_action( 'woo_new_product_tab_content', 'woocommerce_template_single_add_to_cart', );
}
add_filter('woocommerce_product_tabs', 'woo_new_product_tab');
function woo_new_product_tab($tabs) {
$tabs['related_products'] = array(
'title' => __('Models', 'woocommerce'),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
return $tabs;
}
function woo_new_product_tab_content() {
// run our own action hook
do_action( 'woo_new_product_tab_content' );
}
An action hook is a point where your function will run. See attached image:
When you remove an action hooked to woocommerce_single_product_summary, that works, but adding it again on the same hook will not move it somewhere. Because that point is still there. Moving means you have to remove it from a that hook, then add it to another hook. It's like removing it on point A and placing it on point B.

Related

WooCommerce Checkout add default value to Billing fields if custom checkbox checked

I've already created a custom checkbox field in the checkout page with a name of billng_anonymouse, now I want when a customer checks this checkbox it should add a default value to billing_first_name and billing_last_name.
currently, I'm using this function to add a default value for billing fields
function zk_set_checkout_field_input_value_default($fields) {
$fields['billing']['billing_first_name']['default'] = 'First';
$fields['billing']['billing_last_name']['default'] = 'Last';
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'zk_set_checkout_field_input_value_default' );
but the problem is the default values are always there, how to show them using an IF function only if the checkbox is checked.
You can use custom_attributes in woocommerce_checkout_fields so you have a value stored in attribute to use on checkbox check and uncheck. try the below code.
function zk_set_checkout_field_input_value_default($fields) {
$fields['billing']['billing_first_name']['custom_attributes'] = array( 'data-default' => 'First' );
$fields['billing']['billing_last_name']['custom_attributes'] = array('data-default'=>'Last' );
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'zk_set_checkout_field_input_value_default' );
function add_custom_checkout_field( $checkout ) {
$current_user = wp_get_current_user();
$billng_anonymouse = $current_user->billng_anonymouse;
woocommerce_form_field( 'billng_anonymouse', array(
'type' => 'checkbox',
'class' => array( 'form-row-wide' ),
'label' => 'Billng anonymouse'
), $checkout->get_value( 'billng_anonymouse' ) );
}
add_action( 'woocommerce_before_checkout_billing_form', 'add_custom_checkout_field' );
function add_custom_js(){
?>
<script type="text/javascript">
(function($){
$(document).on('change', '#billng_anonymouse', function(event) {
if( $(this).is(':checked') ){
$('#billing_first_name').val($('#billing_first_name').data('default'));
$('#billing_last_name').val($('#billing_last_name').data('default'));
}else{
$('#billing_first_name').val('');
$('#billing_last_name').val('');
}
});
})(jQuery);
</script>
<?php
}
add_action( 'woocommerce_after_checkout_form', 'add_custom_js', 10, 1 );
Tested and works.

function to show purchase note in Woocommerce email deprecated

I am trying to show the purchase note beneath the product in the customer_processing_order email that Woocommerce generates.
I have added the following to my functions.php file:
function sww_add_images_woocommerce_emails( $output, $order ) {
// set a flag so we don't recursively call this filter
static $run = 0;
// if we've already run this filter, bail out
if ( $run ) {
return $output;
}
$args = array(
'show_purchase_note' => true,
);
// increment our flag so we don't run again
$run++;
// if first run, give WooComm our updated table
return $order->email_order_items_table( $args );
}
add_filter( 'woocommerce_email_order_items_table', 'sww_add_images_woocommerce_emails', 10, 2 );
This works, however it is printing an error message in the email stating the following:
"Notice: WC_Order::email_order_items_table is deprecated since version
3.0! Use wc_get_email_order_items instead. in /nas/content/staging/ishgamultisite/wp-includes/functions.php on line
3853"
if I change woocommerce_email_order_items_table to wc_get_email_order_items the function doesn't work.
I'm hoping someone can tell me how I should modify the code as I'm not sure?
Bit late but if anybody is still needing to show purchase notes, then there's an easier hook:
/**
* PURCHASE NOTE
* Edits the email order items args to show purchase notes
*/
function ag_add_wc_order_email_purchase_notes( $args ) {
$args['show_purchase_note'] = true;
return $args;
}
add_filter( 'woocommerce_email_order_items_args', 'ag_add_wc_order_email_purchase_notes', 10, 1 );
The filter woocommerce_email_order_items_args contains the arguments for what to display in the order emails.
$array — Optional. (callback) => array( 'order' => $order, 'items' => $order->get_items(), 'show_download_links' => $order->is_download_permitted() && ! $args['sent_to_admin'], 'show_sku' => $args['show_sku'], 'show_purchase_note' => $order->is_paid() && ! $args['sent_to_admin'], 'show_image' => $args['show_image'], 'image_size' => $args['image_size'], 'plain_text' => $args['plain_text'], 'sent_to_admin' => $args['sent_to_admin'], )
Source: http://hookr.io/filters/woocommerce_email_order_items_args/
Tested with WooCommerce 3.6.2 and works fine.
replace return $order->email_order_items_table( $args );
with
return wc_get_email_order_items( $order, $args );

Show product images on woocommerce new order email and completed order email

I'd like show images product in a new order and completed an order for customers.
I tried this code, but show big images in email, I need eg 100 x 100 px
function sww_add_wc_order_email_images( $table, $order ) {
ob_start();
$template = $plain_text ? 'emails/plain/email-order-items.php' : 'emails/email-order-items.php';
wc_get_template( $template, array(
'order' => $order,
'items' => $order->get_items(),
'show_download_links' => $show_download_links,
'show_sku' => $show_sku,
'show_purchase_note' => $show_purchase_note,
'show_image' => true,
'image_size' => array( 100, 50 ),
'image_size' => $image_size
) );
return ob_get_clean();
}
add_filter( 'woocommerce_email_order_items_table', 'sww_add_wc_order_email_images', 10, 2 );
I don't know if you already fixed this issue. I'm new here at StackOverflow and saw this question when looking for some other stuff :)
But I've just added this simple snippet to my functions.php:
/**
* Display Product Image in WooCommerce Order Emails
*
* #author James Kemp (Iconic)
*/
add_filter( 'woocommerce_email_order_items_args', 'iconic_email_order_items_args', 10, 1 );
function iconic_email_order_items_args( $args ) {
$args['show_image'] = true;
return $args;
}

add_action() but function is never called

I've created a custom wordpress plugin in which I'm defining a custom post type and at one point I'm calling add_action to set custom column in the admin.
The 'manage_[custompostname]_posts_columns' is working great, but the 'manage_[custompostname]_posts_custom_column' do not.
add_filter('manage_bbc_cp_game_posts_columns', bbc_cp_game_table_head');
function bbc_cp_game_table_head( $defaults ) {
$columns = array(
'team_id' => 'Equipe',
'opponent_id' => 'Adversaire',
'gamedatetime' => 'Date / heure',
'score' => 'Score'
);
return $columns;
}
add_action( 'manage_bbc_cp_game_posts_custom_column', 'bbc_cp_game_table_content', 10, 2 );
function bbc_cp_game_table_content( $column, $post_id ) { die('it should work'); }
The second add_action returns true, but the function is never called.
I'd really appreciate if any of you had an idea of what could be the issue, or how to debug it.
Thanks,

WordPress : Genesis Framework add Class

Are you able to add classes to the genesis themes structure?
For example:
do_action('genesis_before_header');
do_action('genesis_header');
do_action('genesis_after_header');
Where are these functions initialised and where can I add to them or create my own?
Example do_action( 'genesis_header' );, if you wish to change the class of your header or HTML mark up for example, use this codes.
THIS IS THE DEFAULT STRUCTURE
add_action( 'genesis_header', 'genesis_header_markup_open', 5 );
function genesis_header_markup_open() {
genesis_markup( array(
'html5' => '<header %s>',
'xhtml' => '<div id="header">',
'context' => 'site-header',
) );
genesis_structural_wrap( 'header' );
}
function genesis_header_markup_close() {
genesis_structural_wrap( 'header', 'close' );
genesis_markup( array(
'html5' => '</header>',
'xhtml' => '</div>',
) );
}
add_action( 'genesis_header', 'genesis_header_markup_close', 15 );
THIS IS YOUR CUSTOM STRUCTURE
remove_action( 'genesis_header', 'genesis_header_markup_open', 5 );
remove_action( 'genesis_header', 'genesis_header_markup_close', 15 );
add_action( 'genesis_header', 'genesis_header_markup_open_custom', 5 );
function genesis_header_markup_open_custom() {
genesis_markup( array(
'html5' => '<div %s>',
'xhtml' => '<div id="my-header">',
'context' => 'my-header',
) );
genesis_structural_wrap( 'header' );
}
function genesis_header_markup_close_custom() {
genesis_structural_wrap( 'header', 'close' );
genesis_markup( array(
'html5' => '</div>',
'xhtml' => '</div>',
) );
}
add_action( 'genesis_header', 'genesis_header_markup_close_custom', 15 );
Or simply add a custom function that contains your custom HTML mark-up and classes. Use this
add_action( 'genesis_header', 'opening', 6 );
add_action( 'genesis_header', 'closing', 14 );
function opening(){
echo '<div class="opening">'; //Opening div element
}
function closing(){
echo '</div>'; //Closing div element
}
Output
To know where is the genesis structure located, visit this folder
theme/genesis/genesis/lib/structure
Hope this will help.
If you just need a body class on a particular template, try
//* Add custom body class to the head
add_filter( 'body_class', 'my_body_class' );
function my_body_class( $classes ) {
$classes[] = 'YOUR-CLASS-HERE';
return $classes;
}
For other code snippets and use cases, try https://my.studiopress.com/snippets/custom-body-class/.

Resources