trigger the update woocommerce product action by code - php

In my site registered users can upload downloadable products in frontend by WPUF,and they can select if the products are free, selecting the value from a dropdown list
I've added this snippet of code in functions.php:
//function to update product status to 'publish'**
// and set the price to 0 for 'free' products**
add_action('wpuf_add_post_after_insert', 'change_post_status');
function change_post_status($post_id){
$project_type = get_post_meta($post_id, 'project_type', true);
if ($project_type == 'Free')
{
update_post_meta( $post_id, '_regular_price', 0);
$current_post = get_post( $post_id, 'ARRAY_A' );
$current_post['post_status'] = 'publish';
wp_update_post($current_post);
}
}
it seems to work, I can see the product as live after submission, ready to be downloaded , BUT I can't see the products in the woocommerce shop page, and the price (in this case "Free") isn't set. In the product page appears an additional tab "Additional information" that displays the product_cat.
The only thing that I've noticed in the database is that the metakey product_count_product_cat of wp_woocommerce_termmeta, isn't correctly updated.
To view the product correctly, in the woocommerce page, and in its page, with the correct price (in this case "Free") I need to enter as admin in Products, /edit and click the "Update" button. This solves everything.
Anyone know what function calls the Publish/Update woocommerce Products button?
thank you

Related

Show extra sections on a product page ONLY to users who already purchased that product (Elementor, Crocoblock, JetEngine)

I have a website with few WooCommerce products, and the product page is created with Elementor Pro. I want users to be able to click Add to Cart and buy the product normally.
But then when the user goes back to the product page (after buying the product), they would not see the the Add to Cart button (if will be hidden for those that purchased the product) and they will see another extra section.
How can I do this?
I also have Crocoblock and JetEngine that have a Dynamic Visibility functionality built in Elementor.
The easiest way to achieve this would be by creating a child theme. The child theme should contain 2 added functionalities.
For this to fully work you will need all users to be registered and NOT have checkout as a guest enabled. Also, this will only work for new orders. If you want to add compatibility for old orders you'd have to create a script that sets the meta value for all users that have purchased this product before the code was implemented.
First of we create a function that adds metadata to the purchasing user account if the product was in the cart. Add the following code to your child theme functions.php
add_action( 'woocommerce_thankyou', 'custom_add_user_meta');
function custom_add_user_meta( $order_id ){
$user_id = get_current_user_id();
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item_id => $item ) {
$product_id = $item->get_variation_id() ? $item->get_variation_id() : $item->get_product_id();
if ( $product_id == 12345 ) {
add_user_meta( $user_id, '_product_xyz_purchased', "true");
}
}
}
Now we add the following code to the same functions.php file to render additional content on the single product page of WooCommerce. NOTE: This step is for a more complete overview that can be implemented outside of Crocoblocks/Jetegine. The following step can be skipped because Jetengine allows setting conditions based on user_meta
add_action("woocommerce_after_single_product", 'render_additional_content', 50);
function render_additional_content() {
$user_id = get_current_user_id();
$product_purchased = get_user_meta( $user_id, '_product_xyz_purchased', true);
if($product_purchased == "true"){
echo "here you can put the HTML you want to display";
}
}

Woocommerce - Change Add to Cart Button text depending on some conditions

I need help from an experienced programmer from here... I have a site that sells Montessori PDF's, Courses, a blog, and more - made from scratch by me. Can be checked at: https://insideoutmontessori.ro/
My issue is Add to Cart Button - I want to show different text depends on conditions - and is not :) Let me explain...
I use a snippet to modify Add to cart Button Text (CUMPARA - means Buy in english) - this is already done by the following code:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woocommerce_custom_single_add_to_cart_text' );
add_filter( 'woocommerce_product_add_to_cart_text', 'woocommerce_custom_product_add_to_cart_text' );
// To change add to cart text on single product page
function woocommerce_custom_single_add_to_cart_text() {
if ( ! is_admin()) {
return __( 'Cumpără', 'woocommerce' );
}
}
// To change add to cart text on product archives(Collection) page
function woocommerce_custom_product_add_to_cart_text() {
if ( ! is_admin()) {
return __( 'Cumpără', 'woocommerce' );
}
}
I use to put a product without price, to announce next PDF - is like a teaser. Now is not active because o don't work for the moment to something new. For this i manage to alter price to be "In lucru" - means Work in progress in English - but i do not manage to change the button text from CUMPARA to VEZI DETALII (means MORE DETAILS in English). This product is not purchasable and I don't manage to use this condition to change the button text...
Also - for products already added to cart i want to change the Add to cart Button text to ADAUGAT IN COS - means Added to Cart in English - for the moment when the user came back to all products page and browse thru them. I manage before to do this (code below works but...) but i receive some errors (Critical ones) and after change the code i receive only something with JSON message in a red box, in Editor, when i try to save a page witch contains woocommerce shortcodes... I deactivate this snippet to lose the error.
The code is below:
add_filter('woocommerce_product_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
add_filter('woocommerce_product_single_add_to_cart_text', 'wc_product_add_to_cart_text', 10, 2 );
function wc_product_add_to_cart_text( $text, $product ){
if ( ! is_admin()) {
$product_cart_id = WC()->cart->generate_cart_id( $product->get_id() );
$in_cart = WC()->cart->find_product_in_cart( $product_cart_id );
if ( $in_cart ) {
$text = "Adăugat în coș";
}
return $text;
}
}
So - the bottom line is that I need a snippet that changes the Add to Cart Button text depending on these three conditions, simultaneously:
if the product is in the cart - the button will show: Adaugat in cos (Already in the cart)
if the product is without a price - the button will show: Vezi detalii (See more details)
otherwise - button will show: Cumpara (Buy)
I think this is the correct approach - i mean this needs to be done with one function that alters button text depends on that three conditions.
Thanks!

Show selected out of stock products In Woocommerce

I'm trying to find a solution to a client demand, without success. She's asking me how to show a selected number of out of stock products on her online store. By default, the Woocommerce setting is set to "hide out of stock products", but she wants to select some of her products and show them (even with 0 stock, because she wants to tell their customers that those few products will be available soon -there is a text for this-).
We have tried with a very simple snippet using the hook woocommerce_product_is_visible that we thought it would work, but there is something that we are missing...
This is de code:
// [WooCommerce] Show some out of stock products even the hide option is active
add_filter( 'woocommerce_product_is_visible', 'keep_showing_specific_out_of_stock_product_list', 10, 2 );
function keep_showing_specific_out_of_stock_product_list( $visible, $product_ID ){
$product_list = array( 18013, 18050 ); // Insert the products IDs that want to show
return in_array( $product_ID, $product_list )? true : $visible;
}
Any help is appreciated.
Why you don't simply use a Woocommerce shortcode like:
1) In the Wordpress text editor of a page or a post (or in a widget):
[products ids="18013,18050"]
2) In any PHP code file:
echo do_shortcode( "[products ids='18013,18050']" );
The products out of stock are displayed just like in this real example:

Woocommerce - Disable Add to Cart Conditionally on Custom Field

Everything I can find on removing the WooComm Add To Cart button will remove not just the add to cart button but also the pricing/variations, aka the whole add to cart area.
My goal is to enable/disable the ability to purchase a product with a checkbox/selector on the product info page. BUT I still have to be able to see the product variation pricing and the variation drop down menu.
This is important. The pricing shown under the product title, for a variation, will be something like $20.00 - $40.00 and not until you select the variation choice will it show the price next to the add to cart button.
So far I have things working wherein I can remove the add to cart area — variations and all — conditionally on my custom field, but I have no idea how to hide/disable click/remove just the add to cart button and allow variations to still be chosen with the variation price displayed.
function remove_add_to_cart(){
if(get_post_meta(get_the_ID(), 'woo_callforinfo', true)) {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
} add_action('woocommerce_single_product_summary','remove_add_to_cart');
Here's what I did. The conditional IF statement is because I have a RETAIL shop with variable products that I don't want affected.
function remove_add_to_cart(){
if ( has_term( 'wholesale', 'product_tag' ) ) {
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
}
}
add_action('woocommerce_single_variation','remove_add_to_cart');
Added this to get rid of the 'Sorry..' message if price is not set.
add_filter( 'gettext', 'customizing_product_variation_message', 10, 3 );
function customizing_product_variation_message( $translated_text,
$untranslated_text, $domain )
{
if ($untranslated_text == 'Sorry, this product is unavailable. Please choose a different combination.') {
$translated_text = __( '-type anything you want here, or leave a space- ', $domain );
}
return $translated_text;
}
Just add the following code in your functions.php and you will find button hidden
I don't know whether my solution is perfect. But it works. Normally if is_purchasable is returned to the filter woocommerce_is_purchasable, the ‘Add to Cart’ button is displayed, and if false is returned the button is hidden.
So, you just need to add the following:
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
// Write code to access custom field value in this function
// let $custom_value be the value from checkbox
return ($custom_value == false ? false : $is_purchasable);
}
No incompatibility issues would creep up.

How can I replace the “Add to cart” button with an “Call to order” button and stop working on some product?

I am using woocommerce plugin in a wordpress site to sell some products. There are two types of products. some products will have add to cart button, but some will have call to order button instead of add to cart. But Both type of products will show the price and discounted price.
I am using some code to change the text
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' );
function woo_custom_cart_button_text() {
return __( 'Call to order', 'woocommerce' );
}
that works. But I when I am using this code to change the url or say stop the button to add to cart that is not working.
add_filter( 'woocommerce_product_add_to_cart_url', 'woo_more_info_link' );
function woo_more_info_link( $link ) {
global $product; // switches link in all cases, i.e. in plugins
$link = get_permalink( $product->id );
return $link;
}
I actually want that the add to cart button should not working and should say only call to order.
Can anybody tell me how to stop add to cart from working.

Categories