Woocommerce: rewrite regular price (main price) on the product page - woocommerce

Please help me find solution how change regular price to my price on product page. I created few new fields for prices in other currences and whant update regular price on product page befor adding them to shoping cart.

Answer is:
update_post_meta( $post->ID, '_regular_price', $custom_price );
update_post_meta( $post->ID, '_price', $custom_price );

Related

Woocommerce Updating products price with php does not trigger price change notification

I was playing around with php code where i change price for a certain product. Also i am using a plugin that notify's by email once price fells below some amount. The product price updates with no issues also appears with new price in the product list view, but sadly does not trigger the notification plugin. The strange thing is when i go to product edit and click update i immediately get the notification email.
The php code i am using currently:
$ppt = $value / $divider;
$product = wc_get_product($product_id);
print_r($product);
// Mark product as updated
update_post_meta($product_id, '_price', $ppt );
update_post_meta($product_id, '_regular_price', $ppt );
update_post_meta($product_id, '_sync_updated', true );
$product->save();
}
wp_reset_query();
Thank you in advance!
You should use WC_Product setter instead update_post_meta.
Try this (not tested) :
$ppt = $value / $divider;
$product = new WC_Product( $product_id );
// Mark product as updated
$product->set_price( $ppt );
$product->set_regular_price( $ppt );
$product->save();
}
wp_reset_query();

Set a variation out of stock in all WooCommerce products

We have mugs printing system with WooCommerce and we display available printable colors as variations. I need to set out of stock a certain variation in all products, i.e., if we do not have red color availability then I want to set red variation out of stock in all the products.
I didn't find any simple solution for this except setting out of stock the certain variation in all products manually.
Any help is appreciated.
$out_of_stock_staus = 'outofstock';
// Updating the stock quantity
update_post_meta($product_id, '_stock', 0);
update_post_meta( $product_id, '_stock_status', wc_clean( $out_of_stock_staus ) );
wp_set_post_terms( $product_id, 'outofstock', 'product_visibility', true );
I think this meta value you suppose to manage for handling the stock for woocommerce product
I don't know regarding hook but this function may help you to figure out

Set Catalog visibility hidden woo-commerce

How to set Catalog visibility hidden in woo-commerce WordPress programmatically?
Like its mentioned here :
https://docs.woothemes.com/document/catalog-visibility-options/
But i can't find any hook or hack, that how to do it in PHP.
I have tried doing this for some days, and there is nothing about it online so I read the woocommerce documentation and discovered that in woocommerce 3.x.x the visibility is a taxonomy called "product_visibility".
To achieve that you should set taxonomy terms, for example:
//Set product hidden:
$terms = array( 'exclude-from-catalog', 'exclude-from-search' );
wp_set_object_terms( $post_id, $terms, 'product_visibility' );
//Set product visible in catalog:
$terms = 'exclude-from-search';
wp_set_object_terms( $post_id, $terms, 'product_visibility' );
//Set product visible in search:
$terms = 'exclude-from-catalog';
wp_set_object_terms( $post_id, $terms, 'product_visibility' );
All possible taxonomy terms:
"exclude-from-catalog"
"exclude-from-search"
"featured"
"outofstock"
The visibility is set in the custom field _visibility. You can change it with update_post_meta():
update_post_meta( $product_id, '_visibility', '_visibility_hidden' );
Possible values:
visible (Catalog & Search)
catalog (Catalog only)
search (Search only)
hidden (nowhere)

Build To Order Products in WooCommerce -- overriding product combinations

I am trying to set up a site with very complex "build to order" products. I am using WooCommerce but I realize that it might not be best solution, however, I am not a programmer so I'm trying to work with a pre-existing application. For reference, here is the original site that I'm redesigning: http://www.cabinetstogo.com/ic280Collectionfrm_multiple.asp?prodno=TOFFEE-NS*WC -- click on the tabs to see the detail.
I have explored all the Woo Extensions I can find such as Product Add Ons, Product Bundles, Grouped Products and Composite Products. Currently I am using Product Bundles with Composite Products here:
http://www.cabinetstogo.company/product/westminster-glazed-toffee-base-cabinets/ -- the layout is a bit messy but that isn't my issue, the issue is that while you can pick and choose individual products you can't select individual quantity.
With Grouped Products: http://www.cabinetstogo.company/product/grouped-test/ the layout is perfect but I can't add a Grouped Product to a Composite Product.
Ideally what I need to do is:
Create Product Bundles of simple products that have to be sold together
Organized these Product Bundles into Product Groups -- PROBLEM: Bundled products can't be added to Group Products
Add these Product Groups into a Composite Product for a master group of products like Base Cabinets -- PROBLEM: Grouped products can't be added to Composite Products
I have tried Product Add Ons two but the issue with that is there is no way to set individual SKUs for the Add Ons.
Setting them as Variable Products is another idea but you can't select more than one variation at a time. I have also considered the Gravity Forms Add On but that seems to just be for additional product details not the mix and match features.
I know there is no easy solution but any pointing in the right direction would help as I can think of all different ways to go about I don't know where to start.
It sounds like you're looking for an off the shelf plugin solution for a VERY complicated problem. You're going to need a programmer for this. A good basic plan could be:
FEATURE: Do custom business logic when a particular product is purchased.
When a customer adds something to the cart
Then do some custom business logic, like add "bundled" products to the cart
Here are some useful snippets functions:
Do something on cart add:
add_action( 'woocommerce_add_to_cart', 'custom_add_to_cart', 10, 2 );
function custom_add_to_cart( $cart_item_key, $product_id ) {
if( 123 == $product_id ) { ....
OOP:
add_action( 'woocommerce_thankyou', array($this, 'doSomething') );
Create a product:
$post_id = wp_insert_post( $post, $wp_error );
update_post_meta( $post_id, '_visibility', 'visible' );
update_post_meta( $post_id, '_stock_status', 'instock');
update_post_meta( $post_id, 'total_sales', '0');
update_post_meta( $post_id, '_downloadable', 'no');
update_post_meta( $post_id, '_virtual', 'yes');
update_post_meta( $post_id, '_regular_price', $regularPrice);
update_post_meta( $post_id, '_sale_price', "123" );

echo woocommerce stock quantity of a particular product id

As the title says, how do i echo the stock quantity of a particular product id? Let's say the product id is "1100" and i want the stock quantity to appear in a page that is not part of the product archive. Is that possible?
For example, in an empty page, it should appear as
"Product A leftover stock: 10"
I am just learning to code and have come up with the following, but no result appear:
function nntest(){
global $woocommerce;
global $product;
$product_id = 1100;
echo $product->get_stock_quantity();
}
Thanks!
You can use get_post_meta() function to get values from database.
this values are stored in wp_postmeta table.
$stock = get_post_meta( $post->ID, '_stock', true );
Another option if you want to work with the actual WC_Product class:
$product_id = 1100;
$product = wc_get_product($product_id);
echo $product->get_stock_quantity();
If you just want to check whether the product is in stock or out of stock use:
$stock = get_post_meta( $post->ID, '_stock_status', true );
It would either return 'instock' or 'outofstock' depending on the stock status.
If you have enabled the option "Enable stock management at product level" and you want stock quantity, use:
$stock = get_post_meta( $post->ID, '_stock', true );

Resources