viniciusrtf
Forum Replies Created
-
I actually like this type of error messages, it’s way better than technical “bla bla bla whatever() on line 666”, and shows that this is a known issue, so you don’t have to lose time opening a bug ticket.
@wisdmlabs and @terrytsang your code seems to be the finest solution. Thanks a lot. How do I exclude the old snippets I’ve posted here?
Forum: Reviews
In reply to: [WP safely disable directory browsing] Doesn't block access to filesLet’s talk about not being a dick and making the world a better place. You could start by forking the project code into a better one, actually implementing the behavior you need. That would be a contribution. Placing low ratings on plugins (coded by a volunteer btw) because they don’t do things they don’t even intend, it is not.
Forum: Reviews
In reply to: [WP safely disable directory browsing] Doesn't block access to filesThe plugin is called “WP safely disable directory browsing“. I don’t even have to install it to find out that this plugin does exactly what it intends to do, you said yourself. The problem is that you’ve just decided that this plugin should do more, even if its basic documentation makes clear that it does not.
Read first, complain later.
Do we have a deal?
It is important to mention that I had some issues using the code above with products configured to be selled individually.
Here it is some improvements to the first code. I’m sure there are better ways to catch the GET and POST requests, as well better ways of using array functions from PHP, but this is the best I can do right now.
/** * Hook: Empty cart before adding a new product to cart WITHOUT throwing woocommerce_cart_is_empty */ add_action ('woocommerce_add_to_cart', 'woocommerce_empty_cart_before_add', 0); function woocommerce_empty_cart_before_add() { global $woocommerce; // Get 'product_id' and 'quantity' for the current woocommerce_add_to_cart operation if (isset($_GET["add-to-cart"])) { $prodId = (int)$_GET["add-to-cart"]; } else if (isset($_POST["add-to-cart"])) { $prodId = (int)$_POST["add-to-cart"]; } else { $prodId = null; } if (isset($_GET["quantity"])) { $prodQty = (int)$_GET["quantity"] ; } else if (isset($_POST["quantity"])) { $prodQty = (int)$_POST["quantity"]; } else { $prodQty = 1; } // If cart is empty if ($woocommerce->cart->get_cart_contents_count() == 0) { // Simply add the product (nothing to do here) // If cart is NOT empty } else { $cartQty = $woocommerce->cart->get_cart_item_quantities(); $cartItems = $woocommerce->cart->cart_contents; // Check if desired product is in cart already if (array_key_exists($prodId,$cartQty)) { // Then first adjust its quantity foreach ($cartItems as $k => $v) { if ($cartItems[$k]['product_id'] == $prodId) { $woocommerce->cart->set_quantity($k,$prodQty); } } // And only after that, set other products to zero quantity foreach ($cartItems as $k => $v) { if ($cartItems[$k]['product_id'] != $prodId) { $woocommerce->cart->set_quantity($k,'0'); } } } } }
In case you’re seeking for a PHP-based solution, put this on your theme’s functions.php:
add_action ('woocommerce_add_to_cart', 'woocommerce_empty_cart_before_add', 0); function woocommerce_empty_cart_before_add() { global $woocommerce; // Get 'product_id' and 'quantity' for the current woocommerce_add_to_cart operation $prodId = (int)$_POST["add-to-cart"]; $prodQty = (int)$_POST["quantity"]; // Get the total of items for each product in cart $cartQty = $woocommerce->cart->get_cart_item_quantities(); // Empty cart before adding the new product to the cart if ($cartQty[$prodId] != $prodQty) { $woocommerce->cart->empty_cart(); $woocommerce->cart->add_to_cart($prodId,$prodQty); } }
We do not get an infinite loop, because
($cartQty[$prodId] != $prodQty)
fails when$woocommerce->cart->add_to_cart()
trigger thewoocommerce_add_to_cart
action for the second time.