woocommerce plugin remove item - wordpress

i am new to woocommerce, actually i want to customize product display so i manually parse products from database i want to remove an item from woocommerce cart through product id, i have found this function,
$woocommerce->cart->get_remove_url($cart_item_key); in core woocommerce cart class, but i am unable to understand what i have to write in $cart_item_key, i have tried to product id or product url in it but its not removing product item from cart, can somebody help me please what i have write in this variable $cart_item_key,
/* Gets the url to remove an item from the cart.
*
* #return string url to page
*/
its description of that function, i have tried using full url of product page and also slug
Regards
Arsalan

I know its an old post but it may help someone.
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $cart_item ) {
if($cart_item['product_id'] == $your_product_id)
$woocommerce->cart->get_remove_url($cart_item_key);
}
Hope its helps..

Related

How to remove quantity from cart page in WooCommerce for downloadable items

Thanks in advance for anyone who can help with this. As I have many virtual/ downloadable items in my Woocommerce shopping page, is there a way to disable, or make the quantity field disappear, entirely but only for those types of items? I have had several occurrences where customers bought multiple of something that was digital, and since that didnt make much sense because they could download it multiple times, I wound up manually refunding the extra. To avoid this extra work, can qty get turned off if the downloadable checkbox is ticked on the product page, or something like that? And it just keeps the default qty of 1 during checkout.
Give this a try
function hide_downloadable_quantity_field_cart( $product_quantity, $cart_item_key, $cart_item ) {
if ( isset( $cart_item['data'] ) && $cart_item['data']->is_downloadable() ) {
$product_quantity = '';
}
return $product_quantity;
}
add_filter( 'woocommerce_cart_item_quantity', 'hide_downloadable_quantity_field_cart', 10, 3 );
It should remove the quantity field completely for downloadable items in the cart.

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

Woocommerce disable selling on products published by a specific user role

I'm going crazy for a few hours, and I need help. Please :) I would like to disable the sale of products added by a specific seller in woocommerce. I use Dokan, a plugin that transforms woocommerce into a market place. This is what I did and that seems to me a good approach: I have created a new role "nonsellingshop" which has the same rights as the default seller role created by Dokan. This role can therefore add products and their price. I would like products added by this specific role can not be purchased, and instead of the add to cart button appears a message saying 'This seller does not offer its products for sale online'. Of course for the sellers not having this role, it is necessary that the sale is possible. It is also necessary that if I change the role of a seller "nonsellingshop" to give it the normal role, the sale becomes possible.
Does anyone have an idea of ​​how I can do this?
Thank you a thousand times in advance for your help
So this what I've done so far (and that doesn't work) :
function get_seller_role_and_hide_cart_button_if_non_seller () { //gets the ID of the post $post_id = get_queried_object_id();
//gets the ID of the author using the ID of the post
$author_ID = get_post_field( 'post_author', $post_id );
//Gets all the data of the author, using the ID
$authorData = get_userdata( $author_ID );
//checks if the author has the role of 'subscriber'
if (in_array( 'boutiquenonvendeuse', $authorData->roles)){
add_filter( 'woocommerce_is_purchasable', '__return_false'); // DISABLING PURCHASE FUNCTIONALITY AND REMOVING ADD TO CART BUTTON FROM NORMAL PRODUCTS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation', 10); // REMOVING PRICE FROM VARIATIONS
remove_action('woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20); // REMOVING ADD TO CART BUTTON FROM VARIATIONS
}
} add_action ('init', 'get_seller_role_and_hide_cart_button_if_non_seller');

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