I want to Adding items to a cart on quantity change with WooCommerce - wordpress

I am building an eCommerce website using WooCommerce. On the product page, in addition to the specific product, I have optional addons with quantity buttons beneath them to add to the cart.
I am trying to get these items to add to the cart every time the quantity is changers (i.e when someone hits the plus sign, it automatically adds the corresponding item to the cart).ideally whichever item I hit the plus sign for is the only item I want added to the cart.
image1
image2

According to this documentation, you can use a wordpress action
the hook name is called woocommerce_cart_item_set_quantity and you can use it like this
function custom_plugin_callback( $cart_item_key,$quantity,$cart_object) {
// (maybe) do something with the args.
// Add another item
$cart_object->add_to_cart( $product_id = 0, $quantity = 1, $variation_id = 0, $variation = array(), $cart_item_data = array() )
}
add_action( 'woocommerce_cart_item_set_quantity ', 'custom_plugin_callback', null, 3 );
Here, custom_plugin_callback() function would be called each time an item has his quantity updated, with the quantity and the cart id of the item
Many other cart actions are available in the .php sources files linked in this page Each of them can provide useful callbacks ( Check do_action() calls to get the hooks names )

Related

Add as separate cart row for a product with custom data in Woocommerce review order (checkout) and cart total (cart) pages

I currently need to sell a service with a subservice as option :
I have hadded a product panel & 2 meta for my option and its price.
If the user checks a checkbox in the product page, then the option is added to the cart page, the checkout page, thank you page, email and so on...
My problem is that i actually can update the initial product price in the product row, in cart & checkout tables but i need to :
1 : insert the additionnal service (the option) as a separate row in the checkout review order table and in the cart total table;
2 : dislay initial product price in the product row and udpdate only the total adding the option price in those 2 tables.
I'm really stuck here, any help would be great. Thank's
OK,
For the solution is quiet simple :
1 : add a product dropdow in admin tab to let user choose a linked product as a product option and register its ID a product meta;
2 : use woocommerce_add_to_cart_validation to intercept the option checkbox value from the product page and add the product option to cart from the initial product meta :
add_action( 'woocommerce_add_to_cart_validation', 'my_cart_validation' );
function my_cart_validation(){
if( isset( $_POST['my-service-option'] ) ){
WC()->cart->add_to_cart( get_post_meta( $_POST['add-to-cart'], 'service-option', true ) );
}
}
3 : If the optional service is not supposed to be displayed in the shop loop, just remove it from the WC Query :
add_action( 'woocommerce_product_query', 'remove_optional_services' );
function remove_optional_services( $q ){
$not_in = $q->get('post__not_in' );
array_push( $not_in, 75 ); // you can grab your product ids from an option or whatever fits your needs
$q->set('post__not_in', $not_in);
}
The good thing is you keep a simple control on taxes and prices with this method.

woocommerce product attributes dropdown not appear

i can't select product color or size before add product to cart .
all attributes showing like text not selection
i need to show product attributes dropdown list (options)
i try :
// Remove additional information tab
add_filter( 'woocommerce_product_tabs', 'remove_additional_information_tab', 100, 1 );
function remove_additional_information_tab( $tabs ) {
unset($tabs['additional_information']);
return $tabs;
}
enter image description here
Once you’ve created attributes you’ll need to use those to create variations on your product. I assume what you’re saying is that you click on an attribute and it then takes you to a page showing you any product that is using that attribute.
once your attributes are added to the product follow the rest of this doc to get variations created. Then you’ll have drop down options for those variations on your product.

WooCommerce select x number of items from large group for single product

I have a Woocommerce site where customers can order color samples with the following characteristics:
Select multiple colors from a list of over 100
First 3 choices are free
Each subsequent color choice will add 1 dollar to price of order
It does not seem appropriate in this case to make 100+ separate simple products for each color and bundle them (neither do we track inventory on samples) I just want customers to make their multiple choices in the context of one product called "Samples". I have looked at the bundled products page docs and none of these seem appropriate for what I am trying to do.
I solved this using a plugin and some custom code. The plugin is here, and the custom code counts the number of items in the grouping and modifies the price based on that number. In my case, I wanted each option to cost a dollar, but have the first 3 be free. I use a woo hook to change the price before adding it to the cart (this gets fired off when the add to cart button is clicked after selections are made):
add_action( 'woocommerce_before_calculate_totals', 'add_custom_price' );
function add_custom_price( $cart_object ) {
$target_product_id = 9516; // my product ID
foreach ( $cart_object->cart_contents as $key=>$value ) {
if ( $value['product_id'] == $target_product_id ) {
if (count($value['tmdata']['tmcp_post_fields'])>=3){
$value['data']->set_price($value['tm_epo_options_prices'] - 3);
} elseif (count($value['tmdata']['tmcp_post_fields'])==2){
$value['data']->set_price($value['tm_epo_options_prices'] - 2);
} elseif (count($value['tmdata']['tmcp_post_fields'])==1){
$value['data']->set_price($value['tm_epo_options_prices'] - 1);
}
}
}
}
Please note that above code is calling data (tm_epo_options_prices) provided by the plugin.

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