WooCommerce inquiry if no price available - wordpress

I'm helping a good friend setting up a WooCommerce shop. Since the shop is going to be bigger and the products are pretty variable and customizable we are not able to provide/configure all prizes from the beginning.
However we would like all products to be in the shop and ad an inquiry lead form in case no price is available.
Since I never programmed with WooCommerce I was wondering that is the right hook to implement such an functionality?

Had the exact same issue and couldn't find a plugin or a solution anywhere so I figured a workaround myself:
You need to edit file
/wp-content/themes/your-theme-name/woocommerce/single-product/add-to-cart/simple.php
(if it's not there just copy it from woocommerce plugin
/wp-content/plugins/woocommerce/templates/single-product/add-to-cart/simple.php)
and on line 14 where it says
if ( ! $product->is_purchasable() ) return;
you need to comment it out and write something like
if ( ! $product->is_purchasable() ) {
// put your code here
return;
}
and in the //put your code here line you can enter for example a shortcode for a form or a more complicated solution would be to put code for a button that when clicked will open up a popup form.
Still working on that ;)

Maybe too late, but I have had same issue currently.
Here is there code, Woocommerce uses to check if a product can be purchased:
https://github.com/woocommerce/woocommerce/blob/master/includes/abstracts/abstract-wc-product.php#L1404
Notice about: && '' !== $this->get_price()
/**
* Returns false if the product cannot be bought.
*
* #return bool
*/
public function is_purchasable() {
return apply_filters( 'woocommerce_is_purchasable', $this->exists() && ( 'publish' === $this->get_status() || current_user_can( 'edit_post', $this->get_id() ) ) && '' !== $this->get_price(), $this );
}
So you need to write a filter like this to override default:
add_filter( 'woocommerce_is_purchasable', function ( $is_purchasable, $product ) {
return $product->exists() && ( 'publish' === $product->get_status() || current_user_can( 'edit_post', $product->get_id() ) );
}, 10, 2 );

Try the following code snippet. You just have to put it in your functions.php. You don't need a plugin or to overwrite WooCommerce files in your child theme.
// Inquiry link if no price available
function add_inquiry_link_instead_price( $price, $product ) {
if ( '' === $product->get_price() || 0 == $product->get_price() ) :
return ''.__( 'Jetzt anfragen' ).'';
endif;
}
add_filter( 'woocommerce_get_price_html', 'add_inquiry_link_instead_price', 100, 2 );

Related

Add custom product option to woocommerce orders list

I am using the "custom product options" plugin and created a datepicker for customers to choose a requested ship date. I need to create a column on my orders admin page for their selection so I do not have to go into the order to find the date. I have found a few different threads that are similar but they don't really apply to my exact situation. Would someone be able to provide some help on this? Apologies for a lack of detail that is getting me downvotes, but If I knew what I needed I wouldn't be here asking what to do. I am a novice, cut me a little slack. Datepicker can be seen here.
https://chrish148.sg-host.com/product/butterscotch-oatmeal-copy/
current layout looks like this.
current layout image
This is the code that I used for one of my WooCommerce sites to get order-colors:
add_filter( 'manage_edit-shop_order_columns','shopOrder',10 );
function shopOrder($columns)
{
$columns['custom_color_column'] = "Färger";
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'populateShopOrder' );
function populateShopOrder( $column ) {
global $the_order, $post;
if( $column == 'custom_color_column' ) {
global $post, $the_order;
if ( empty( $the_order ) || $the_order->get_id() !== $post->ID ) {
$the_order = wc_get_order( $post->ID );
}
if ( empty( $the_order ) ) {
return;
}
$items = $the_order->get_items();
foreach ($items as $item) {
// load meta data - product attributes
foreach ($item->get_meta_data() as $metaData) {
$attribute = $metaData->get_data();
// attribute value
$value = $attribute['value'];
if($value != null && $attribute['key'] == "pa_farg"){
echo "- " . $value;
if(count($items) > 1) echo "<br>";
}
else return;
}
}
}
}
However this might not be the exakt case for you. Since I do not know the exact plugin that you are using I can not know for sure.
However it should be close.
If you can figure out the attribute names then you could switch it from "fa_farg" to that, or you could link the plugin thet you are using and I will take a look at it.

WPML how to add a featured image without duplicate to a translated post (wordpress)?

I translated a post using WPML.
The problem is that it doesn't add a featured image to the translated post.
It takes up too many size to duplicate images using a WPML media transltion,
so I'd like to add a featured image without duplicate to a tranlsated post (wordpress).
Would you please let me know how to solve this problem?
Thank you.
In case there is someone who needs this.
I solved the problem using the foolowing code.
add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
function my_duplicate_on_publish( $post_id ) {
global $post;
// don't save for autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// dont save for revisions
if ( isset( $post->post_type ) && $post->post_type == 'revision' ) {
return $post_id;
}
if ($post->post_type=='your-post-type') {
//we need this to avoid recursion see add_action at the end
remove_action( 'wp_insert_post', 'my_duplicate_on_publish' );
// make duplicates if the post being saved
// #1. itself is not a duplicate of another or
// #2. does not already have translations
$is_translated = apply_filters( 'wpml_element_has_translations', '', $post_id, $post->post_type );
if ( !$is_translated ) {
do_action( 'wpml_admin_make_post_duplicates', $post_id );
}
//must hook again - see remove_action further up
add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
}
}

How to check WooCommerce thank you page

In Wordpressis_page() can check page by ID,name or by slug but how can we check WooCommerce thank you page whether its a part of checkout page.
Also We have a lot of WooCommerce conditional tags but cant find something solve my issue
for example I try
if(is_page('checkout')) {
//some thing for only checkout page
}else if(is_page('thankyou') && !is_page('checkout')){
//some thing for only thank you page but not on checkout page
}else{
//some thing for all other page
}
This sample code may work:
if ( is_checkout() && !empty( is_wc_endpoint_url('order-received') ) ) {
...
}
I think better to use endpoint like
if ( is_wc_endpoint_url( 'order-received' ) ) {
global $wp;
//Get Order ID
$current_order_id = intval( str_replace( 'checkout/order-received/', '', $wp->request ) );
echo $current_order_id;
}

Multiple Content Filters Gravity View Plugin For Gravity Forms Plugin For WordPress

We are working with filters using the Gravity View plugin for Gravity Forms for WordPress. Not super good at filters yet, so hoping to get a little help with this if possible. We have a filter when added, that filters out certain content, based on the Gravity Form field ID. In the example below, we are filtering out the Gravity Form field with an ID of 18.
Example:
add_filter( 'gravityview/fields/custom/content_before', 'my_gv_custom_content_before', 10, 1 );
function my_gv_custom_content_before( $content ) {
$id = '18';
global $gravityview_view;
extract( $gravityview_view->field_data );
if( empty( $entry[ (string)$id ] ) ) {
return '';
}
return $content;
}
This works. Then taking it a step further, we are told we can add something to this filter to filter out multiple Gravity Form field ids using this something like this added to our example above. In this example, filtering Field 18 & 26:
if( ( false !== strpos( $content, '{MY_FIELD_NAME:18}') && empty( $entry['18'] ) ) || ( ( false !== strpos( $content, '{MY_FIELD_NAME:26}') && empty( $entry['26'] ) ) ) {
return '';
}
What would the two look like combined? For example, if we wanted to filter field id 18, 26 and perhaps a third field 34? Using the first part of the code example and then the second example, what would they look like combined? Could anyone provide an example?
Have been playing with this and not having much luck combining it all into one filter. Keep getting errors and I am sure we don't have something quite right. Thank you in advance.
Got it worked out, here is the code in case anyone needs it.
/** Modify the content returned from the Custom Content field */
add_filter( 'gravityview/fields/custom/content_before', 'my_gv_custom_content_before', 10, 1 );
/**
* Removes the custom content field's content in case a certain entry field is empty
*
* #param string $content Custom Content field content
* #return string
*/
function my_gv_custom_content_before( $content ) {
global $gravityview_view;
extract( $gravityview_view->field_data );
// field_id => merge_tag
$validation = array(
'18' => '{Custom Background:18}',
'26' => '{New YouTube Video Test:26}',
'28' => '{New Vimeo:28}'
);
foreach( $validation as $id => $merge ) {
if( false !== strpos( $content, $merge ) && empty( $entry[ (string)$id ] ) ) {
return '';
}
}
return $content;
}

Woocommerce Hook after product insertion in backend

I've looked through Woocommerce API Docs to see if there's any way to manipulate the product after it gets inserted in the admin backend of Wordpress but couldn't find any. Maybe I missed it?
I need to pick product data and send it to an external API, and obviously, handle it on update and delete...
Is there any way/hook I can use?
This goes more into what I want. Taken from this answer in WP forum. Thanks to the author
add_action('transition_post_status', 'wpse_110037_new_posts', 10, 3);
function wpse_110037_new_posts($new_status, $old_status, $post) {
if(
$old_status != 'publish'
&& $new_status == 'publish'
&& !empty($post->ID)
&& in_array( $post->post_type,
array( 'product')
)
) {
//add some cde here
}
}
If You add a new product in woocommerce then send product_id
add_action('draft_to_publish','my_product_update');
function my_product_update( $post ) {
if($post->post_type == "product"){
$pid=$post->ID;
//your code
}
}

Resources