wc-donation / wc-donation-platform

Donation Platform for WooCommerce unleashes the power of WooCommerce for your online fundraising, crowdfunding & crowdsponsoring
https://www.wc-donation.com/
GNU General Public License v2.0
41 stars 11 forks source link

How to create donation project automatically after user register? #14

Closed procontentxyz closed 2 years ago

procontentxyz commented 2 years ago

i noticed: If we create product using code, Donation dont get active,

when we hit publish manually, donation become active.

please guide for below issue:

  1. Why we have to publish product manually to make product donation active, If product is already published using code.
jonas-hoebenreich commented 2 years ago

Hi, The problem is probably that you are not adding the product appropriately for WooCommerce: https://wordpress.stackexchange.com/questions/137501/how-to-add-product-in-woocommerce-with-php-code

procontentxyz commented 2 years ago

Could you please guide whats wrong with above code OR whats missing.

Because everthing is working right, only product donation become active after hitting publish button manually not with programmatically publish.


Also, am i updating meta for donation correctly??

Please check last two update_meta lines & correct me if i am doing it wrong.

🙏🙏🙏

jonas-hoebenreich commented 2 years ago

While a product is a custom post type, WooCommerce adds some additional logic using the wc_product_meta_lookup table, such as the product price, whether the product is available, etc. This additional metadata is set as soon as someone manually updates the product. For example, your code is still missing a price: update_post_meta( $post_id, '_price', '1' ); I think your link is from a time when the wc_product_meta_lookup table did not exist.

The Donation Platform for WooCommerce specific code looks pretty good to me.

jonas-hoebenreich commented 2 years ago

Also, I would be grateful if you could post support-related questions in the WordPress support forum in the future: https://wordpress.org/support/plugin/wc-donation-platform/ This will make the questions easier to find for other users.

procontentxyz commented 2 years ago

ok i will remember to post on support forum!

only help me last time here on github. please

Here is the new code with latest CRUD method to create product:

, function create_product(){

// Create an empty instance of the WC_Product
$product = new WC_Product_Simple(); // <=== Simple product

$product->set_name( 'Product Title' );
$product->set_slug( 'product-title' );
$product->set_status( "publish" );
$product->set_virtual( "yes" );
$product->set_regular_price( "1" ); 
$product->set_catalog_visibility( "visible" ); 
$product->set_description( "" ); 
$product->set_short_description( "" ); 

$product->update_meta_data("_donable", "yes");    <----------------  not working

$product->update_meta_data("wcdp-settings[0]", "1");

$product->update_meta_data("wcdp-settings[1]", "[100,200,500,1000]");

$product->save(); // Save (publish) new product 

}; ,

everything is now working with only 1 issue, i shown above.

could you tell me why donation product is not selected using any one of above two ?

jonas-hoebenreich commented 2 years ago
$product = new WC_Product_Simple();

$product->set_name( 'Post Title' );
$product->set_slug( 'slug' );
$product->set_status( "publish" );

$product->set_virtual( "yes" );
$product->set_regular_price( "1" ); //must
$product->set_catalog_visibility( "visible" ); //must
$product->set_description( "" ); // (optional)
$product->set_short_description( "" ); // (optional)

$product->update_meta_data("_donable", "yes");

$product->update_meta_data("wcdp-settings[0]", "1");
$product->update_meta_data("wcdp-settings[1]", "[100,200,500,1000]");

$product->save();

When I run this code with code snippets, it works flawlessly. What exactly is not working for you?

procontentxyz commented 2 years ago

$product->update_meta_data("_donable", "yes");

This is not working, i have to manually set product as donable by checkbox

i also tried this:

$product->set_donable( "yes" );

jonas-hoebenreich commented 2 years ago

That is weird, when I open the product page in the frontend, everything works fine Where is the set_donable() function defined?

procontentxyz commented 2 years ago

could you please check on your end:

  1. Open auto created product & check if donation product checkbox is CHECKED or not ?

i thought like $product->set_virtual( "yes" ); >>>>>>>>>> $product->set_donable( "yes" ); will work.... 🗡️

so silly of me.... hehehe

jonas-hoebenreich commented 2 years ago

yes it is checked

procontentxyz commented 2 years ago

i am confused what happened on my side....

:(