I want to add the parent price to all variations using "Create variations from all attributes > go"
- but when I add action it won't even show my debug echo text.
I'm trying to add an action to this hook
do_action( 'product_variation_linked', $variation_id );
Here: public function create_all_product_variations
in this file: class-wc-product-data-store-cpt.php line 1189
Instead of echoing my debug text it just messes up the alert confirmation and doesn't allow the created variations to show using Ajax. They are created though. They display fine on refresh.
Any idea why the hook doesn't operate as one would expect?
I guess it has something do do with Ajax?
I guess plan B is to write a function that updates all variations. Could also generate the variations after import too and bypass the whole product > go GUI?
Thanks
Related
I've added a 'Customize Now' button and few dropdowns after the 'add to cart' button using woocommerce_after_add_to_cart_button.
But now when I try to hide the 'add to cart' button (which I have to for a specific scenario my website need) using woocommerce_is_purchasable, the 'Customize Now' button and dropdowns are also hidden. Is there any proper order/sequence to do this?
Filter to add the Customize button and dropdowns:
add_action('woocommerce_after_add_to_cart_button', array($this, 'pn_get_calc_and_customize_btn'));
Filter to remove the add to cart button:
add_filter('woocommerce_is_purchasable', array($this, 'pn_hide_add_to_cart_button'), 10, 2);
As add-to-cart templates display condition is:
if ( ! $product->is_purchasable() ) {
return;
}
2 ways:
1) Use instead woocommerce_single_product_summary hook with a priority between 30 and 40:
add_action('woocommerce_single_product_summary', array($this, 'pn_get_calc_and_customize_btn'), 35 );
Then your function output code should be embedded in a custom <form> and you will need to add some more code to save the data in cart or elsewhere…
2) To remove cart button, use woocommerce_product_is_in_stock filter hook instead of woocommerce_is_purchasable so you will have to change a bit your hooked function code too...
add_filter('woocommerce_product_is_in_stock', array($this, 'pn_hide_add_to_cart_button'), 10, 2);
I have two suggestions here:
The first one will be to try to add the priority to your add_action() as well. As per documentation, the lower the number, the earlier the execution. I would try to add a greater priority to add_action() and try to force the woocommerce_after_add_to_cart_button to be executed after your filter. However, I don't know if removing the button also inhibits the filter (it might be).
Another suggestion I may have is to override the default template for the page (i don't know if you're editing the shop page or the single_product page) and have some if{}else{} login in there to show hide buttons based on the situation.
I don't know if either of these solutions is any good for you but this was just my tough and how I would tackle it.
Hope it helps in any way
I have been searching and found a lot of various answers, however, I have not found a definitive answer.
I need to run a function right after a post is done saving to the database. This includes every aspect of the post including post metas. I have tried to hook into save_post but that seems to run my function before post metas are saved. I have also tried post_updated and updated_postmeta, but my function doesn't seem to run on either of them.
Another thing to note, I need to have access to the post ID inside my function.
Edit, My plugin uses the Advanced Custom Fields plugin and the function I have coded uses update_field to either create new post metas or update existing one based on some stuff. This code works. When I run the function at the post_updated hook the function seems to run but nothing happens. If I add die() to the end of my function my code works, but die kills the page and all I am left with is a blank white page at the url wp-admin/post.php. So adding die allows my function to work and I am not sure why it would not work without die.
I would comment your post, but I cannot because I dont have 50 rep.
Do you mean the_post?
https://codex.wordpress.org/Plugin_API/Action_Reference/the_post
function my_the_post_action( $post_object ) {
// modify post object here
}
add_action( 'the_post', 'my_the_post_action' );
it ought to have the post Id
https://developer.wordpress.org/reference/hooks/the_post/
Okay I found how to make publish_post work.
For custom post type you need to replace the "post" by the post type slug.
Example with the custom post type "Recipe" with "recipe" slug.
add_action('publish_recipe', 'test_publish_post', 10, 2);
function test_publish_post($post_id, $post){
wp_die($post_id);
}
Don't forget to wp_die() or die(); else you will be redirected and you won't see your var_dump();
I was able to
fix my issue. It turns out that save_post does seem to run after post metas are saved. My problem actually came from something else inside my code that I was able to fix by changing how I handled that part of my script.
I am trying to AJAXify my Woocommerce cart but I am failing hard and I really can't find any help or documentation (I searched for days). So any help is desperately appreciated.
I tried two approaches:
Custom AJAX Function
When changing the quantity (on the cart page), a AJAX call is fired (-> admin-ajax.php) which triggers a function in my functions.php:
function setQty() {
global $woocommerce;
WC()->cart->set_quantity( $_POST['itemKey'], $_POST['quantity'], true);
echo json_encode(array('totalCount'=>WC()->cart->get_cart_contents_count(), 'total'=>$woocommerce->cart->total)); }
The echoed JSON String contains the correct number of items in cart but the total amount is 0. When I reload the page the altered quantity is not saved.
Same with
WC()->cart->add_to_cart($product_id, $quantity) or adding WC()->cart->calculate_totals()
I tried it this way so i can format the values I need to update my cart how i want.
What am I missing here in order to get the cart update saved?
The woocommerce built-in AJAX update function doesn't trigger, because I have a custom design and I think it needs a specific HTML-structure in order to work properly but i can't find any documentation or examples. Also I need the updated values (total amount, total count etc.) in a custom format. So far it just passes me "updated fragments" as predefined html code.
How do i need to build the HTML code of my cart.php that the ajax update will work? And how do i alter the returning values?
Thank you very much in advance.
I'm not sure what you'd like to update, but I guess you want to update the totals amount of products in the basket.
This example replaces and updates the HTML
Let's say you have a div where your total amount is in:
<div class="cart-totals"><?php echo WC()->cart->get_cart_contents_count(); ?></div>
Add this to your functions.php:
// Mini Cart update with AJAX
add_filter( 'woocommerce_add_to_cart_fragments', 'custom_cart_count_fragments', 10, 1 );
function custom_cart_count_fragments( $fragments ) {
$fragments['div.cart-totals'] = '<div class="cart-totals">' . WC()->cart->get_cart_contents_count() . '</div>';
return $fragments;
}
Note: You are replacing not adding
Is it a mini cart/counter you are trying to create or a full page to review/remove products?
If it is a mini-cart/cart counter you wanted, I've created a repo under https://github.com/samisonline/ajax_cart for you that has the basic functionality baked in. This may not be the exact format you're looking for, but it shows the woocommerce functionality at work, as well as the needed markup for AJAX to work!
I've seen many tutorials dealing on how to customize woocommerce's checkout page by adding or removing fields.
But what I want is to place a link or button on the Woocommerce Checkout page saying "Return to Cart" (obviously linking to the cart page) but I want it placed just after the "Your Order" section, (the section where you review your order). I want it there because I want it along with a text saying something like "If you want to change your order return to Cart".
If I edit the actual checkout page and add the link there, it shows all the way to the bottom so maybe I have to add code to the theme's functions file? Any guidance will be greatly appreciated.
Thank you.
EDIT:
Ok, I've found a very crappy way of doing it.
I just added this line to the review-order.php file located in woocommerce/templates/checkout/ , right after the shop_table class:
<?php echo "<strong>If you'd like to change your order, go back to <a href='http://www.mysite.com/cart/'>My Cart</a></strong><br />"; ?>
This does the trick, but everytime I update woocommerce I will have to added it again.
Any suggestion of a more practical and intelligent way of doing it?
Create a child theme.
Put this in the child theme's functions.php
/**
* Add link back to cart after order review on Checkout
*/
add_action( 'woocommerce_review_order_before_payment', 'my_back_to_cart_link' );
function my_back_to_cart_link(){
//get the cart link
global $woocommerce;
$cartUrl = $woocommerce->cart->get_cart_url();
//the HTML markup to add
$backToCartLink="<p class='backtocart'><a class='button alt' href='".$cartUrl."'>".__('Edit Cart','wooint')."</a></p>";
echo $backToCartLink;
}
Well, if you created a child theme you could have put that line in your child's functions.php and then the only way an update would affect it is if they changed the coding.
In a WordPress woocommerce template, this line outputs woocomerce product description / excerpt
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>
Adding my HTML after that line outputs my HTML outside the div that holds the excerpt/description of the product. I am trying to add a div inside the content as if it was added in the editor.
Basically, I want to add something in all products but because I have many products already, I want a way to insert it in template file or something just once and applied to all.
Anyone know how can I do that?
You could add a filter that adds the div, before the woocommerce software adds it. Try something like this, inside your <theme>/functions.php file:
function add_my_div($desc) {
return '<div class="my-div">'.$desc.'</div>';
}
add_filter('woocommerce_short_description', 'add_my_div', 0, 1);
Notice it is on priority 0. This should run before most other filters. The default value for priority is 10, and I am pretty sure that is the priority that WC adds it's wrapper div.
Hope this helps.
jquery .append did the trick for me
http://api.jquery.com/append/
#loushou Thanks for your comment. It looks like your suggestion could work also but I haven/t tried it yet. Will do If I got the time and update my findings here.
Thanks