Exclude Backorder Products from WooCommerce Composite Options - wordpress

I'm using WooCommerce Composite Products to create customer configurable packages from existing items in our Woo shop. These packages need to consist of In Stock products only, ignoring anything that is set to backorder.
For "Composite Options" I am using product categories (we have too many products to add individually), however doing it this way adds all products that are currently on backorder and set to "Allow, but notify customer".
Ideally I need a solution that allows me to use product categories which ignores any products that are set to backorder.
I tried to edit an existing snippet which would create a filter for backorder products but was not able to make any difference to the front end visible products, I'm sure more needs to be done to make it work. This is the original snippet I edited:
add_filter( 'woocommerce_composite_component_options_query_args_current', 'sw_cp_exclude_out_of_stock_options' );
function sw_cp_exclude_out_of_stock_options( $args ) {
$args[ 'exclude_out_of_stock' ] = true;
return $args;
}
I'm pretty sure the following is the basis of the code needed to figure out how to exclude back order products from component options (taken from Composite Products Plugin Editor):
woocommerce composite products > includes > data > class-wc-product-composite-data-store-cpt.php Line 1040 + 1041:
`// See 'WC_CP_Component::exclude_out_of_stock_options'.
'exclude_out_of_stock' => false`
woocommerce composite products > includes > class-wc-cp-component.php Line 785 - 789:
` /**
* Controls whether out of stock component options should be hidden.
*
* #since 8.0.3
*
* #return boolean
*/
public function exclude_out_of_stock_options() {
return apply_filters( 'woocommerce_component_options_exclude_out_of_stock', 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ), $this );
}`
Can anyone help me with the automated solution to this problem?

Related

How do I add product attributes to the product page in WooCommerce?

I'm using WooCommerce and Elementor Pro. Is there any way to add specific product attributes (e.g. gross and net weight) below the Add to cart button?
It seems like an obvious thing but I haven't found options or snippets for it.
First you add attributes to product. Then you use this snippet in functions.php:
// maybe remove tab with attributes
add_filter( 'woocommerce_product_tabs', 'remove_info', 100, 1 );
function remove_info( $tabs ) {
unset($tabs['additional_information']);
return $tabs;
}
// show attributes
add_action( 'woocommerce_single_product_summary', 'summary_attributes', 35 );
function summary_attributes() {
global $product;
if ( $product->has_attributes() ) {
wc_display_product_attributes( $product );
}
}
Setting Up WooCommerce Attributes
go to Products > Attributes
Add the attribute name
Slug will automatically be created, and we can leave the rest of these options untouched.
Click “Add attribute” button and your attribute will be added.
Configure The Attributes
After creating attribute, now we can add different variation on your product. Click on the Configure Terms option.
Enter the variation
Add some short description
Click on the add new tab
To understand it in a better way you can follow this tutorial

hide gravityform field labels from Woocommerce email notifications

I want to filter out unwanted order item meta data from Woocommerce email notifications especially gravity forms field labels. With gravityforms and gravity forms product add on it is possible that any customer can engrave a product. licence plates for cars etc.
e.g. eins:M zwei:TU drei:2019 .
I want to hide the field labels (eins, zwei, drei) and the double points in the email-notifications.
https://einsautoschilder.de/wp-content/uploads/2019/09/itemmeta.png
With dt.variation{display:none;} CSS rule I can hide the labels on cart and checkout page, but not in the emails.
To learn more about woocommerce order item meta data i found a very good tutorial
by Igor Benic.
"How to manage order item data in WooCommerce" | https://www.ibenic.com/manage-order-item-meta-woocommerce
With the following code it is possible
1) to hide gravityform field labels from woocommerce email notifications,
2) to change the order item meta title
add_filter( 'woocommerce_order_item_display_meta_key', 'change_order_item_meta_title', 20, 3 );
/**
* Changing a meta title
* #param string $key The meta key
* #param WC_Meta_Data $meta The meta object
* #param WC_Order_Item $item The order item object
* #return string The title
*/
function change_order_item_meta_title( $key, $meta, $item ) {
// By using $meta-key we are sure we have the correct one.
if ( 'eins' === $meta->key ) { $key = ''; }
return $key;
}
the code works in functions.php of your theme

How to sort woocommerce categories & products without plugins?

I have about 400+ products categories & 5000+ Products in my woocommerce list and I modify them regularly so can't use "category and Taxonomy order" or similar plugins and it must be done programmatically.
Need to sort categories and products by name (title).
I'm trying to use this code but it's not working:
add_filter('woocommerce_shortcode_products_query', 'my_woocommerce_catalog_orderby');
function my_woocommerce_catalog_orderby( $args ) {
$args['orderby'] = 'title'; // or name, etc
$args['order'] = 'asc';
return $args;
}
Also not this one:
add_filter('woocommerce_get_catalog_ordering_args', 'my_woocommerce_catalog_orderby');
...
Woocommerce products settings, widgets, etc. didn't work for me.
I saw some similar questions but not working answer.
Any idea?
After a few days struggling I came up with a total solution for woocommerce sorting.
I found a part of solution here for products to make a custom sort option:
https://docs.woocommerce.com/document/custom-sorting-options-ascdesc/
Which adds an option to woocommerce catalog sorting list. To change it check:
Woocommerce Settings -> Products Tab -> Display -> Default Product Sorting
and select your desired sorting option which you added using the solution above.
Then, for categories (or actually sub categories), add the code below to functions.php :
add_filter( 'woocommerce_product_subcategories_args', 'custom_woocommerce_get_subcategories_ordering_args' );
function custom_woocommerce_get_subcategories_ordering_args( $args ) {
$args['orderby'] = 'title';
return $args;
}

Woocommerce is it possible to disable quantity field on cart page, for some product?

Basically, I finished building custom plugin for my client.
the only thing after products added to cart, before the checkout.
user able to change the quantity of the products, is it possible to display the selected quantity, but disabled the options to read only so client will able to see the quantity in cart page that he selected but can't change it?
and to apply this only to products that I used with my plugin either product ids or better category id because all the products there.
other product display and able to change quantity regular
by the way its regular products not virtual and not Sold Individually i need to find a way to limit clients to change quantity for some products only in cart page!, and not in product page.
I really appreciate any help.
As mentioned in the comment, you can use the woocommerce_cart_item_quantity filter for that. So that might look something like this:
function 668763_change_quantity_input( $product_quantity, $cart_item_key, $cart_item ) {
$product_id = $cart_item['product_id'];
// whatever logic you want to determine whether or not to alter the input
if ( $your_condition ) {
return '<h3>' . $item['quantity'] . '</h3>';
}
return $product_quantity;
}
add_filter( 'woocommerce_cart_item_quantity', '668763_change_quantity_input', 10, 3);
This would be just a simple example to replace the input with a h3 element containing the quantity. It can easily be adjust to alter the quantity input element to your liking.

Woocommerce Add Product To Cart Multiple Times But As Different items

I have some custom code that adds an item to the cart, a variable product. If i add the same product twice it just increases the quantity. I would like it to add the product again as a different item in the cart.
How would i go about doing that? The item is being added to the cart using a link like domain.com?add-to-cart=3434&variation_id=4434 and so on.
The system i have developed is a product designer. So i may want to pick the same product but design it in different ways, and then add the same variation to the cart.
Is there also a way to send a unique identifier in the url to split these items up?
I want to do it by using add_to_cart but every time i do that with the variation attributes, the shipping ends up with a bug, it basically can't seem to find shipping methods:
$woocommerce->cart->add_to_cart(522,1, 523,array("attribute_colour" => "colour","attribute_size" => "a3", "attribute_invoice-numbering" => "yes", "attribute_quantity-column" => "yes", "attribute_cc-type" => "duplicate"));
While that code adds the item to the cart, it causes the following:
Warning: Invalid argument supplied for foreach() in /home/****/public_html/wp-content/plugins/woocommerce/includes/class-wc-shipping.php on line 291
There doesn‘t seem to be any available shipping methods. Please double check your address, or contact us if you need any help.
It doesn't make any sense, because the standard add to cart code in woocommerce uses add_to_cart and has none of these issues. The same if i use a URL, it works fine!
Try this !
/*
* #desc Force individual cart item
*/
function force_individual_cart_items( $cart_item_data, $product_id ){
$unique_cart_item_key = md5( microtime().rand() );
$cart_item_data['unique_key'] = $unique_cart_item_key;
return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data','force_individual_cart_items', 10, 2 );
/*
* #desc Remove quantity selector in all product type
*/
function remove_all_quantity_fields( $return, $product ) {
return true;
}
add_filter( 'woocommerce_is_sold_individually', 'remove_all_quantity_fields', 10, 2 );
I can't tell yet if the shipping array issue is related. But, if you take a look at the beginning of the add_to_cart() method in the cart class:
// Generate a ID based on product ID, variation ID, variation data, and other cart item data
$cart_id = $this->generate_cart_id( $product_id, $variation_id, $variation, $cart_item_data );
// See if this product and its options is already in the cart
$cart_item_key = $this->find_product_in_cart( $cart_id );
Basically, what we're seeing here is that the item must be completely unique in order to add it to the cart again. Otherwise, it will just up the quantity of the item already in the cart. So, to get around this, you will want to make the add_to_cart() parameters unique... probably via the final, $cart_item_data array.

Resources