Woocommerce reduce stock quantity action hook - wordpress

i am using following code for Woocommerce Product publish, update hook and it's work perfect for me :
add_filter( 'wp_insert_post_data' , 'filter_post_data' , '99', 2 );
function filter_post_data( $data , $postarr ) {
$sum = $_POST['wpcf']['ptchq'] + $_POST['wpcf']['monivong'];
$_POST['_stock'] = $sum;
return $data;
}
i am add two custom text field and add that field sum in product stock quantity in admin side.
Now my question is that how can i get after order complete reduce stock quantity action hook. so using that hook i can reduce stock in my custom field in admin side product.
Thanks,
Ketan.

The hook that runs when stock is changed is
do_action( 'woocommerce_product_set_stock', $this );
You can see it in the Product Abstract

Related

Remove simple product from product data woocommerce

How can we remove simple product and variable product from product data dropdown in woocommerce.
You can use product_type_selector filter hook and unset to remove simple product type. check below code. code will go into the active theme functions.php file.
add_filter( 'product_type_selector', 'remove_product_types_simple' );
function remove_product_types_simple( $types ){
unset( $types['simple'] );
return $types;
}

How To Hide/Remove Additional Checkout Fields for Specific Products On Checkout Page?

I have installed WooCommerce Checkout & Account Field Editor plugin on my WordPress website for adding additional fields to my checkout page. The purpose of installing this plugin is to add three additional fields like how_you_heard_about_our_store, user_membership_level and user_refferer_name to the checkout page for getting additional details from my users.
This is working fine and the user can provide the required information as needed during checkout. However, there is one product Gift Certificate that I need to exclude these additional fields on it when this product is on the checkout page.
The purpose is to hide these fields on this product only. I have 4 different variations of this product and I need these fields to be hidden for each of its variations.
I have tried my following techniques but this is actually for default WooCommerce fields.
https://www.liquidweb.com/kb/way-conditionally-show-hide-checkout-fields-specific-products-product-categories-store/
Also, I have tried the following as well with no luck:
function custom_override_checkout_fields( $fields ) {
unset($fields['order']["how_heard"]);
unset($fields['order']["member_level"]);
return $fields;
}
Is there any specific action or filter for removing additional fields from my checkout page? Any help would highly be appreciated.
add_filter( 'woocommerce_checkout_fields' , 'hide_checkout_fields' );
function hide_checkout_fields( $fields ) {
foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
$id = $product->get_id();
$products = array(2516, 584, 2454); // Product Ids
if (in_array($id, $products))
{
unset($fields['order']['how_heard']);
unset($fields['order']['member_level']);
}
}
return $fields;
}

Woo commerce change titles on checkout page

Can anyone direct me to where I can find the file where I can change the "State/County" and "Zip Code" titles in a woocommerce checkout page?
WooCommerce offers hooks for this:
please check Customizing checkout fields using actions and filters
as you'll see on that page you can hook a function (that you'll save in your childs functions.php for example ) to WooCommerce checkout page and change the data that is available.
so your code will look something like:
/ Hook in
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Our hooked in function - $fields is passed via the filter!
function custom_override_checkout_fields( $fields ) {
$fields['shipping']['shipping_postcode']['label'] = 'My new postcode title';
$fields['shipping']['shipping_state']['label'] = 'My new state title';
return $fields;
}
other examples and fields on the linked page.

Woocommerce /Display Custom Field in Wordpress

I added some custom fields to the each product in Woocommerce, and I would like the data from the custom fields to show on the following receipt page (Order Details) after checkout is completed.
Just can't get it, not strong in php, why can't get anything. I've tried to use var_dump(get_post_custom($order->id)); i don't have my custom field in result.
Can somebody light me up?
Here is my code:
add_action( 'woocommerce_order_details_after_order_table', 'code_activation', 10, 1);
function code_activation($order){
echo '<p><strong>'.__('Activation code').':</strong> ' . get_post_meta( $order->id, 'activation_code', true ). '</p>';
}
You have added custom fields to the product. So custom fields will be associated with your product-id .
But you are trying to retrieve the custom fields using $order->id which is wrong. following code should help to retrieve the product id from order. And using the product id you can retrieve your custom field.
$orderItems = $order->get_items();
foreach($orderItems as $orderItem)
{
$product_id = $orderItem['variation_id'] ? $orderItem['variation_id'] : $orderItem['product_id'] );
}

Woocommerce customize the product name in the cart and order pages

Actually, I already got hook to customize the price in the cart page and all other page.
This is the hook to customize the product price
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$custom_price = 10; // This will be your custome price
foreach ( $cart_object->cart_contents as $key => $value ) {
$value['data']->price = $custom_price;
}
}
Actually, I am using woocommerce plugin. I want a hook to customize the product name displayed in the cart page and all other pages next to cart page.
I want to customize the Product name , i want to add the some static attributes to product name displayed in the cart page, order page ,order details page and all the pages next to cart page.
Thanks in advance
I wanted to share this as I managed to figure out something for my needs. (I only ever have a single item in the order as I've customised WooCommerce for holiday bookings.)
<?php
add_action( 'woocommerce_product_title', 'add_custom_name' );
function add_custom_name( $title ) {
return 'test name';
}
?>
Hopefully someone can elaborate on this further or take what has been done with the custom price code an properly re-purpose it for product names.

Resources