Reduce WooCommerce Item Inventory By Attribute Value - php

I have a setup with Woocommerce "Variable Products" where the only variation is 'size' attribute: 15 grams, 100 grams, 250 grams. What I want to do is use that variation amount to pass to the Woo wc-stock-functions, so that when a product variation '15 grams' is purchased, the overall stock goes down by 15, not 1.
Inside Woo, there is file wc-stock-functions (http://hookr.io/plugins/woocommerce/3.0.6/files/includes-wc-stock-functions/) - and this even gives a filter, woocommerce_order_item_quantity. I want to use this to multiply the inventory number by the # of grams, and to reduce inventory this way by grams.
I'm trying this:
// define the woocommerce_order_item_quantity callback
function filter_woocommerce_order_item_quantity( $item_get_quantity, $order,
$item ) {
$original_quantity = $item_get_quantity;
$item_quantity_grams = $item->get_attribute('pa_size');
// attribute value is "15 grams" - so remove all but the numerals
$item_quantity_grams = preg_replace('/[^0-9.]+/', '', $item_quantity_grams);
// multiply for new quantity
$item_get_quantity = ($item_quantity_grams * $original_quantity);
return $item_get_quantity;
};
// add the filter
add_filter( 'woocommerce_order_item_quantity',
'filter_woocommerce_order_item_quantity', 10, 3 );
but am getting internal server error as a response right now.
Does anyone have an idea of what I'm doing wrong with the above code? Thanks for any help.

First error is in $item->get_attribute('pa_size'); as $item is an instance of WC_Order_Item_Product object and get_attribute() method doesn't exist for WC_Order_Item_Product Class.
Instead you need to get the an instance of the WC_Product object using get_product() method from WC_Order_Item_Product Class…
So your code should be:
add_filter( 'woocommerce_order_item_quantity', 'filter_order_item_quantity', 10, 3 );
function filter_order_item_quantity( $quantity, $order, $item )
{
$product = $item->get_product();
$term_name = $product->get_attribute('pa_size');
// The 'pa_size' attribute value is "15 grams" And we keep only the numbers
$quantity_grams = preg_replace('/[^0-9.]+/', '', $term_name);
// Calculated new quantity
if( is_numeric ( $quantity_grams ) && $quantity_grams != 0 )
$quantity *= $quantity_grams;
return $quantity;
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
Note: This hooked function is going to reduce the stock quantity based on that new returned increased quantity value (in this case the real quantity multiplied by 15)

Related

Custom Woocommerce product weight calculation from dimensions only for specific shipping methods

I am trying to set up a dimensional weight counting method when the dimensional weight (width x height x length) of the product is greater than the weight (actual weight).
I found Custom Woocommerce product weight calculation from dimensions answer code that works perfectly.
However I would like to make it works only for specific shipping method(s).
Any advice or help?
add_filter( 'woocommerce_product_get_weight', 'custom_get_weight_from_dimensions', 10, 2 );
function custom_get_weight_from_dimensions( $weight, $product ) {
$chosen_shipping = WC()->session->get('chosen_shipping_methods')[0];
// How to restrict to specific shipping methods?
$dim_weight = $product->get_length() * $product->get_width() * $product->get_height() / 5000;
return $dim_weight > $weight ? $dim_weight : $weight;
}
You can use the following targeting specific shipping methods only (see at the end, how to get your shipping method rate ids):
add_filter( 'woocommerce_product_get_weight', 'custom_get_weight_from_dimensions', 10, 2 );
function custom_get_weight_from_dimensions( $weight, $product ) {
if ( ! is_admin() ) {
// Here set your targeted shipping method rate Ids
$targetted_shipping_ids = array( 'flat_rate:12', 'flat_rate:14' );
// Get chosen shipping method(s)
$chosen_shipping_methods = (array) WC()->session->get('chosen_shipping_methods');
$matched_shipping_methods = array_intersect( $chosen_shipping_methods, $targetted_shipping_ids );
if( ! empty($matched_shipping_methods) ) {
$dim_weight = $product->get_length() * $product->get_width() * $product->get_height() / 5000;
}
}
return isset($dim_weight) && $dim_weight > $weight ? $dim_weight : $weight;
}
Code goes in functions.php file of the active child theme (or active theme). Tested and works.
How to get the shipping method rate ID:
To get the related shipping methods rate IDs, something like flat_rate:12, inspect with your browser code inspector each related radio button attribute value like:

Enable decimal quantities and stock for WooCommerce products

I want to change the default quantity from the products, from 1 to 0,1 but I can't seem to figure it out.
I tried the following:
function custom_quantity_input_args($args, $product) {
$args['input_value'] = 0.10;
$args['min_value'] = 0.10;
$args['step'] = 0.10;
$args['pattern'] = '[0-9.]*';
$args['inputmode'] = 'numeric';
return $args;
}
The problem with this is that modifies the quantity input from cart as well, which isn't what I want.
To be more specific I want the following:
when I go to the product page I want to show 0,1;
when I go to the cart page I want to show the current quantity;
The solution I mention above shows 0,1 both in product page and in cart page.
I found another solution, but it shows the current quantity both in product and in cart which, again, it's not what I want.
Any ideas?
Based on Decimal quantity step for specific product categories in WooCommerce answer code, try the following revisited code:
// Defined quantity arguments
add_filter( 'woocommerce_quantity_input_args', 'custom_quantity_input_args', 9000, 2 );
function custom_quantity_input_args( $args, $product ) {
if( ! is_cart() ) {
$args['input_value'] = 0.1; // Starting value
}
$args['min_value'] = 0.1; // Minimum value
$args['step'] = 0.1; // Quantity steps
return $args;
}
// For Ajax add to cart button (define the min value)
add_filter( 'woocommerce_loop_add_to_cart_args', 'custom_loop_add_to_cart_quantity_arg', 10, 2 );
function custom_loop_add_to_cart_quantity_arg( $args, $product ) {
$args['quantity'] = 0.1; // Min value
return $args;
}
// For product variations (define the min value)
add_filter( 'woocommerce_available_variation', 'filter_wc_available_variation_price_html', 10, 3);
function filter_wc_available_variation_price_html( $data, $product, $variation ) {
$data['min_qty'] = 0.1;
return $data;
}
// Enable decimal quantities (in frontend and backend)
remove_filter('woocommerce_stock_amount', 'intval');
add_filter('woocommerce_stock_amount', 'floatval');
Code goes in functions.php file of the active child theme (or active theme). Tested and works.

Set bookable product base cost programatically in Woocommerce Bookings

I am trying to calculate the Base cost of a WooCommerce bookable product and managed to get it done using this:
function modify_baseprice() {
global $post;
$productid = $post->ID;
$product = new WC_Product($productid);
$product_block_price = $product->wc_booking_block_cost;
$product->wc_booking_cost = ($product_block_price*0.6) + 100;
$pricing_data = update_post_meta( $productid, '_wc_booking_cost', $product->wc_booking_cost);
return $pricing_data;
}
add_action( 'woocommerce_bookings_after_booking_base_cost', 'modify_baseprice', 10, 3 );
It does calculate the Base cost correctly but I need to refresh the page twice to see it appearing on the Base cost field. Is there a way that I can get it to appear after the first save?
Since Woocommerce 3 release, CRUD objects have been implemented. It's the case of WC_Product object and also for Woocommerce Bookings plugin. So you can use available Getters and setters methods as properties are not anymore accessible (in most cases).
The following code use this better way (The cost is set in the product without any need of refreshing the page):
add_action( 'woocommerce_process_product_meta_booking', 'modify_bookable_product_base_cost', 100, 1 );
function modify_bookable_product_base_cost( $product_id ){
// Get an instance of the WC_Product object
$product = wc_get_product( $product_id );
// We check that we have a block cost before
if ( $product->get_block_cost() > 0 ){
// Calculation
$new_booking_cost = ( $product->get_block_cost() * 0.6 ) + 100;
$product->set_cost( $new_booking_cost ); // Set the new calculated cost in the product
$product->save(); // Save the product data
}
}
Code goes in function.php file of your active child theme (active theme). Tested and works.

Prevent customers from ordering from more than 3 Vendors in Woocommerce

I have found similar solutions to this, for example: "How to limit orders to one category". I have tried to modify the code but it's not specific enough.
In my case, each Vendor is defined by a product attribute value, 8 Terms at all. If the cart contains products from more than 3 different Terms, I need to set up a message that says "Sorry, you may only order from 3 different Vendors at a time".
This is what I'm using as a starting point:
add_action( 'woocommerce_add_to_cart', 'three_vendors' );
function three_vendors() {
if ATTRIBUTE = SELECT A VENDOR; NUMBER OF TERMS > 3 {
echo "Sorry! You can only order from 3 Vendors at a time.”;
}
}
The middle line is me filling in the blanks using non-php language.
I am looking for a way to define the amount of variations in the cart. If this is not possible to do with Attributes, I am open to use Categories instead.
Does anyone know how to do this?
Update: Is not possible to manage variations with woocommerce_add_to_cart_valisation hook
Instead we can use a custom function hooked in woocommerce_add_to_cart filter hook, removing the last added cart item when more than 3 vendors (items) are in cart:
// Remove the cart item and display a notice when more than 3 values for "pa_vendor" attibute.
add_action( 'woocommerce_add_to_cart', 'no_more_than_three_vendors', 10, 6 );
function no_more_than_three_vendors( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) {
// Check only when there is more than 3 cart items
if ( WC()->cart->get_cart_contents_count() < 4 ) return;
// SET BELOW your attribute slug… always begins by "pa_"
$attribute_slug = 'pa_vendor'; // (like for "Color" attribute the slug is "pa_color")
// The current product object
$product = wc_get_product( $variation_id );
// the current attribute value of the product
$curr_attr_value = $product->get_attribute( $attribute_slug );
// We store that value in an indexed array (as key /value)
$attribute_values[ $curr_attr_value ] = $curr_attr_value;
//Iterating through each cart item
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// The attribute value for the current cart item
$attr_value = $cart_item[ 'data' ]->get_attribute( $attribute_slug );
// We store the values in an array: Each different value will be stored only one time
$attribute_values[ $attr_value ] = $attr_value;
}
// We count the "different" values stored
$count = count($attribute_values);
// if there is more than 3 different values
if( $count > 3 ){
// We remove last cart item
WC()->cart->remove_cart_item( $cart_item_key );
// We display an error message
wc_clear_notices();
wc_add_notice( __( "Sorry, you may only order from 3 different Vendors at a time. This item has been removed", "woocommerce" ), 'error' );
}
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and should works for you. It will check for the attribute values regarding your specific attribute and will remove last added cart item for more than 3 attribute values.
The only annoying thing is that I can't remove the classic added to cart notice for the moment. I will try to find another way…

Get the minimum variation price in WooCommerce

I am a beginner trying to learn Wordpress and have a variable product with prices:
cod Rs 250
online Rs 200
I want to use the minimum price in my themes functions.php.
How will I be able to fetch or get the minimum value?
I want to use in the below code :
function filter_gateways($gateways){
$payment_NAME = 'cod'; //
$min_variation_price = ''; <--------------- minimum variation price
global $woocommerce;
foreach ($woocommerce->cart->cart_contents as $key => $values ) {
// Get the terms, i.e. category list using the ID of the product
$terms = get_the_terms( $values['product_id'], 'product_cat' );
// Because a product can have multiple categories, we need to iterate through the list of the products category for a match
foreach ($terms as $term) {
// 20 is the ID of the category for which we want to remove the payment gateway
if($term->term_id == $min_variation_price){
unset($gateways[$payment_NAME]);
// If you want to remove another payment gateway, add it here i.e. unset($gateways['cod']);
break;
}
break;
}
}
return $gateways;
}
To get the minimum variation active price in WooCommerce from a WC_Product_Variable object:
$variation_min_price = $product->get_variation_price();
It seems that you are trying here to unset 'cod' payment gateway when a cart item min variation price is matching with his product category ID.
Using the WordPress conditional function has_term() will really simplify your code (it accepts term Ids, term slugs or term names for any taxonomy (as 'product_cat' here for product categories).
I suppose that your function is hooked in woocommerce_available_payment_gateways filter hook…
I made some changes in your code as it's a bit outdated and with some errors:
add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
// Not in backend (admin)
if( is_admin() )
return $available_gateways;
foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
// Get the WC_Product object
$product = wc_get_product($cart_item['product_id']);
// Only for variable products
if($product->is_type('variable')){
// Get the Variation "min" price
$variation_min_price = $product->get_variation_price();
// If the product category match with the variation 'min" price
if( has_term( $variation_min_price, 'product_cat', $cart_item['product_id'] ) ){
// Cash on delivery (cod) payment gateway
unset($available_gateways['cod']); // unset 'cod'
break; // stop the loop
}
}
}
return $available_gateways;
}
Code goes in functions.php file of your active child theme (or theme) or also in any plugin file.
This should work (but I can't really test it as it's very specific regarding the product categories ID and the min variation prices)…
All related price methods for variable product type:
// All prices: multidimensional array with all variation prices (including active prices, regular prices and sale prices).
$prices = $product->get_variation_prices();
$prices_for_display = $product->get_variation_prices( true ); // For display
// The min or max active price.
$min_price = $product->get_variation_price(); // Min active price
$min_price_for_display = $product->get_variation_price('min', true); // Min active price for display
$max_price = $product->get_variation_price('max'); // Max active price
$max_price_for_display = $product->get_variation_price('max', true); // Max active price for display
// The min or max regular price.
$min_price = $product->get_variation_regular_price(); // Min regular price
$min_price_for_display = $product->get_variation_regular_price('min', true); // Min regular price for display
$max_price = $product->get_variation_regular_price('max'); // Max regular price
$max_price_for_display = $product->get_variation_regular_price('max', true); // Max regular price for display
// The min or max sale price.
$min_price = $product->get_variation_sale_price(); // Min sale price
$min_price_for_display = $product->get_variation_sale_price('min', true); // Min sale price for display
$max_price = $product->get_variation_sale_price('max'); // Max sale price
$max_price_for_display = $product->get_variation_sale_price('max', true); // Max sale price for display
Related answers:
Disable WooCommerce Payment methods if cart item quantity limit is reached
I think you are looking for that :
add_filter( 'woocommerce_variable_sale_price_html', 'get_min_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'get_min_variation_price_format', 10, 2 );
function get_min_variation_price_format( $price, $product ) {
$min_variation_price = $product->get_variation_regular_price( 'min');
return wc_price($min_variation_price);
}

Categories