Woocommerce: Is it possible to overide the settings for allowing to purchase out of stock products [closed]

The following hooked functions will allow you to make everything purchasable: // Change all products stock statuses to ‘instock’ add_filter( ‘woocommerce_product_get_stock_status’, ‘filter_get_stock_status_callback’, 10, 2 ); add_filter( ‘woocommerce_product_variation_get_stock_status’, ‘filter_get_stock_status_callback’, 10, 2 ); function filter_get_stock_status_callback( $stock_status, $product ){ return is_admin() ? $stock_status : ‘instock’; } // Enable backorders on all products add_filter( ‘woocommerce_product_get_backorders’, ‘filter_get_backorders_callback’, 10, 2 ); … Read more

Append a term to WooCommerce product existing product category terms

You just need to add a missing boolean argument in wp_set_object_terms() function like: wp_set_object_terms($post->ID, $term->term_id, ‘product_cat’, true); This time the term will be appended and not replaced. You should check that the term doesn’t exist yet on the product, before appending it like: add_action( ‘transition_post_status’, ‘new_product_add’, 10, 3 ); function new_product_add( $new_status, $old_status, $post ) … Read more

Set a default quantity input value for a variable product category in WooCommerce

On woocommerce_available_variation hook the $args array doesn’t include input_value argument. Also You are not using has_term() conditional function in the right way for product categories. So try the following code instead: add_filter( ‘woocommerce_available_variation’, ‘customizing_available_variation’, 10, 3 ); // Variations function customizing_available_variation( $args, $product, $variation ) { if( has_term( ‘flyer’, ‘product_cat’, $product->get_id() ) ) { $args[‘max_qty’] … Read more

Output a WooCommerce product custom field in WooCommerce using get_post_meta()

There are some errors as get_post_meta() first argument need to be a defined product ID here (and in your code post_ID is not even dynamic variable and will not give anything)… Try the following: <?php function check_gtin(){ if ( $gtin = get_post_meta(get_the_id(), ‘gtin’, true) ){ echo “gtin13:”. $gtin; } else { echo ‘identifier_exits:false’; } } … Read more

Set a specific default stock quantity on all WooCommerce new products

For “New” simple products on admin, the following embedded jQuery script will: trigger and enable “Stock management” at product level, set the “Stock quantity” to “1”, set the “Low stock threshold” to “0”. The code: add_action( ‘admin_footer’, ‘new_product_stock_default_setting’ ); function new_product_stock_default_setting() { global $pagenow, $post_type; // Targeting new product page (admin) if( $pagenow === ‘post-new.php’ … Read more

Unable to programmatically remove product from WooCommerce cart

I guess you should use your function on the hook woocommerce_add_to_cart instead of template_redirect Your function works fine for me. Try an alternative methow which also works: add_action(‘template_redirect’, function () { $product_id = 29690; foreach( WC()->cart->get_cart() as $key=>$item){ if($item[‘product_id’]==$product_id){ WC()->cart->remove_cart_item( $key ); } } // $product_cart_id = WC()->cart->generate_cart_id( $product_id ); // $cart_item_key = WC()->cart->find_product_in_cart( $product_cart_id … Read more

Display a list subcategories under the main product category thumbnails?

Ok so after some more searching online and using the right combination of search terms I stumbled across this guide here: https://www.themelocation.com/how-to-display-all-the-subcategories-from-a-specific-category-in-woocommerce/ From that guide I created this snippet of code that can be added to your theme’s functions.php file so that the subcategory list appears under the main category title in the main categories … Read more

How to prevent action in ajax request, when in admin side?

After testing a lot of things, I found this function, which works: function is_admin_request() { /** * Get current URL. * * @link https://wordpress.stackexchange.com/a/126534 */ $current_url = home_url( add_query_arg( null, null ) ); /** * Get admin URL and referrer. * * @link https://core.trac.wordpress.org/browser/tags/4.8/src/wp-includes/pluggable.php#L1076 */ $admin_url = strtolower( admin_url() ); $referrer = strtolower( wp_get_referer() ); … Read more