Closed procontentxyz closed 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
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.
🙏🙏🙏
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.
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.
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 ?
$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?
$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" );
That is weird, when I open the product page in the frontend, everything works fine
Where is the set_donable()
function defined?
could you please check on your end:
i thought like $product->set_virtual( "yes" ); >>>>>>>>>> $product->set_donable( "yes" ); will work.... 🗡️
so silly of me.... hehehe
yes it is checked
i am confused what happened on my side....
:(
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: