cart

Need Woocommerce to only allow 1 product in the cart. If a product is already in the cart and another 1 is added then it should remove the previous 1

假装没事ソ 提交于 2019-11-27 07:52:55
I think this code should work but not exactly sure where to place it. Everywhere I have tried has failed so far... add_action('init', 'woocommerce_clear_cart'); function woocommerce_clear_cart() { global $woocommerce, $post, $wpdb; $url = explode('/', 'http://'.$_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]); $slug=$url[4]; $postid = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_status='publish' AND post_name = '$slug'"); if ($postid){ if ($postid == PRODUCTID1 || $postid == PRODUCTID2){ $woocommerce->cart->empty_cart(); } } } Unfortunately there is no 'action' hook before

Conditional progressive percentage discount based on cart item count in Woocommerce

我们两清 提交于 2019-11-27 07:14:11
问题 I would like to have a conditional progressive discount based on number of items in cart. After you added 2 products to the cart, you get a discount. More products you add and more discount you get. For example: 1 product – full price (No Discount) 2 products – full price with 5% discount of the combined price 3 products – full price with 10% discount of the combined price 4 products – full price with 15% discount of the combined price And so on … I have search over internet without any

Set WooCommerce cart expiration

我与影子孤独终老i 提交于 2019-11-27 06:23:48
问题 I would like to erase the cart content when the woocommerce session expires. I can see there's a variable setting the time in class WC_Session_Handler , however when it expires, products does not get removed from cart (i guess it behaves like this by design,it's not an error). So please tell me how can i set the session expiration time for woocommerce cart so, that cart content gets removed when it expires? 回答1: From what I can see, WooCommerce 2.0.20 has a scheduled maintenance job that runs

WooCommerce Cart Quantity Base Discount

自闭症网瘾萝莉.ら 提交于 2019-11-27 06:12:59
问题 In WooCommerce, how do I set a cart discount based on the total number of items in the cart? For example: 1 to 4 items - no discount 5 to 10 items - 5% 11 to 15 items - 10% 16 to 20 items - 15% 21 to 25 items - 20% 26 to 30 items - 25% I've search internet but not found any solution or plugins available. Thanks. 回答1: You can use a negative cart fee to get a discount. Then you will add your conditions & calculations to a acustom function hooked in woocommerce_cart_calculate_fees action hook,

Hide shipping methods for specific shipping class in WooCommerce

时间秒杀一切 提交于 2019-11-27 05:29:54
Essentially I'm trying to make the flat rate method Id flat_rate:7 disabled when there is cart items that have the shipping class "Roller" (ID 92 ). This is the code I tried: add_filter('woocommerce_package_rates', 'wf_hide_shipping_method_based_on_shipping_class', 10, 2); function wf_hide_shipping_method_based_on_shipping_class($available_shipping_methods, $package) { $hide_when_shipping_class_exist = array( 92 => array( 'flat_rate:7' ) ); $shipping_class_in_cart = array(); foreach(WC()->cart->cart_contents as $key => $values) { $shipping_class_in_cart[] = $values['data']->get_shipping_class

Change Cart total price in WooCommerce

假装没事ソ 提交于 2019-11-27 03:35:22
问题 I am running into issues with the cart total only displaying 0 Essentially what I am trying to do is only accept a deposit total of a certain amount after all cart items have been added to the carts subtotal. So for example if the customer adds $100 worth of items, they would only pay $10 initially or (10%) of the subtotal as the total value. I took the code from here: Change total and tax_total Woocommerce and customize it this way: add_action('woocommerce_cart_total', 'calculate_totals', 10

GET a coupon code via URL and apply it in WooCommerce Checkout page [closed]

北慕城南 提交于 2019-11-27 02:53:32
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed last year . I have a WooCommerce website and when customer add-to-cart a product, it is get redirected to checkout page, so cart page is not accessible. I would like to apply coupon via URL ( GET ) on checkout page, with something like https://example.com/?coupon_code=highfive . When customer

Get in WooCommerce cart the product ID of a cart item

爱⌒轻易说出口 提交于 2019-11-27 02:09:11
$cart_item = $woocommerce->cart->get_cart(); I have the above code. if I run print_r on cart_item I get a multi dimensional array: Array( [a6292668b36ef412fa3c4102d1311a62] => Array ( [product_id] => 6803 How do I get the product_id only? I tried $test = $cart_item['data']; print_r($test); Didn't work. To get the product ID of each cart item in the foreach loop (for a simple product): foreach( WC()->cart->get_cart() as $cart_item ){ $product_id = $cart_item['product_id']; } If it's a variable product, to get the variation ID : foreach( WC()->cart->get_cart() as $cart_item ){ $variation_id =

How to retrieve cart_item_data with WooCommerce?

北城余情 提交于 2019-11-27 00:45:53
问题 During the add_to_cart function, there is a filter to add "cart item data". The filter is woocommerce_add_cart_item_data . I expected to store my custom plugin data in this, so that the data is stored relative to the item and multiple products can be added with different data. This all seemed to work, but I am not able to retrieve the data. I can't figure it out. The data is there , I can see it in a serialized string, but I can't pull it out. echo '<pre>'; var_dump( WC() ); foreach( WC()-

Refresh / update minicart with AJAX in Woocommerce

廉价感情. 提交于 2019-11-26 23:41:18
问题 I’m trying to add this code to my WooCommerce setup that adds a shopping cart link wherever I put the PHP and then updates it upon changing items in the cart with AJAX: https://docs.woocommerce.com/document/show-cart-contents-total/ Here are the snippets: HTML - PHP: <a class="cart-customlocation" href="<?php echo wc_get_cart_url(); ?>" title="<?php _e( 'View your shopping cart' ); ?>"><?php echo sprintf ( _n( '%d item', '%d items', WC()->cart->get_cart_contents_count() ), WC()->cart->get