Prestashop cart save not working - php

i'm dealing with a problem, I need to change the carrier if the cart amount it > 500 , so , I'm hooking actionCartSave and checking the amount , but , when I do
$cart->id_carrier=(int)$carrier_id;
$cart->update();
The ajax stops responding , need to reload the page to see if a product has been added to the cart, but if i remove the $cart->update() , the carrier does not get updated. How can i solve this?

Maybe you are running into a loop. when you call update, the hook actionCartSave gets called again.
What you need to do is avoid updating the cart if the cart carrier is the same one as in your result
if ((int)$cart->id_carrier !== (int)$carrier_id) {
$cart->id_carrier = (int)$carrier_id;
$cart->update();
}
That way, you will avoid the infinite loop
Another (better) solution would be to use $cart->save(); because it doesn't call actionCartSave

Related

Clear Woocommerce cart when sending multiple items by url

I need the Woocommerce cart to be cleaned in case I send more than four items to it via url.
I came up with this code, but it only cleans the cart if there are already five items in it.
//in functions.php
add_filter( 'woocommerce_add_to_cart_validation', 'remove_cart_item_before_add_to_cart', 20, 3 );
function remove_cart_item_before_add_to_cart( $passed, $product_id, $quantity ) {
if( WC()->cart->get_cart_contents_count() >= 5 )
WC()->cart->empty_cart();
return $passed;
}
Another problem is that, even the code above that is not useful to me, does not work if I add multiple courses via url, as in:
https://exemple.net/cart?fill_cart=100,101,102,103,104,105,106
That is, the code in functions.php only works from the website, and not by url.
All I need is clear the cart when sending more than 4 items by url.
I prefer a solution in PHP, but a JS solution will do. Thanks for who can help me.
I found a solution using the free plugin Cart links for WooCommerce, and making a few change in its code.
I will describe below what I did:
1) To start install the plugin, this will allow you to add products using the fill_cart parameter in url (as described in my question)
2) edit the plugin file soft79-cart-links-for-woocommerce.php found at: yoursite/wp-content/plugins/soft79-cart-links-for-woocommerce/
3) go to line 141 or find the declaration of the method fill_cart
Inside the fill_cart method uncomment (or add) the code to clean the cart:
public function fill_cart( $fill_string ) {
$original_notices = wc_get_notices();
wc_clear_notices();
////Clear the cart
//WC()->cart->empty_cart(); <-- uncomment here!
Save the file. Now any new items that arrive via url must clean the cart first.

Add to cart function not working for first product when cart is empty

I am using { WC()->cart->add_to_cart($new_post_id) ;} to add a product ,but this function does not adding for the first time when cart is empty.If I add product using default add to cart button, Then after the above function works properly so can you please point out the issue . Basically session cookiees are not created with while adding via function but if the session cookkie is set then this works smoothly . can you guide me how to set these cookies in my case .
console Image
It looks like the quantity is missing of the product.
Try to use
WC()->cart->add_to_cart($new_post_id,quantity);
hope this will help you.
Try following:
session_start();
WC()->cart->set_cart_cookies();
WC()->cart->add_to_cart($new_post_id,quantity);

Woocommerce cart page default to one

I have my woocommerce page to sell a single product. I need to to go to the cart page with default value of 1 in cart page, so that when users click
www.test.com/cart
it never says "cart is Empty"!
Any ways to do that?
What do I need to do? Any ideas?
It sounds like you want to add a product to the user's cart even if they haven't actually added it themselves:
So that when users click www.test.com/cart it never says "cart is Empty"
For what it's worth, I don't think that's a fantastic idea from a UX point of view (or pretty much any other). However, it is possible.
Be warned: This code is largely untested, and it's triggered to run on init (although it should only run each time the cart is visited). Generally, I would advise against this - but it should do what you want:
function vnm_addThingToCart() {
// Check we're not in admin
if (!is_admin()) {
// Only do this on the cart page
if (is_page('cart')) {
global $woocommerce;
// You're only selling a single product, so let's make sure we're not going to add the same thing over and over
$myProductID = 22; // Or whatever
$productToAdd = get_product($myProductID);
// Make sure the product exists
if ($productToAdd) {
// Make sure there's stock available
$currentStock = $productToAdd->get_stock_quantity();
if ($currentStock > 0) {
// Add the product
$woocommerce->cart->add_to_cart($myProductID);
}
}
}
}
}
add_action('init', 'vnm_addThingToCart');

Prestashop 1.6 unable to find Order via Cart ID

I am using a payment module with Prestashop 1.6, everything was working fine until an upgrade happened. The payment server return the validation data to an url like "http://..../validation.php".
This file tries to get the order using the cart id that is returned by the server and obviously cant find it, hence the cart is not emptied and the order not created.
I added logs into that file to see what is received. The CartID (data['reference']) is correct.
writeMessage("Trying to get the order id using the cart:".$data['reference']);
writeMessage(Order::getOrderByCartId((int)($data['reference'])));
if ($id_order = intval(Order::getOrderByCartId((int)($data['reference']))))
{
writeMessage("Got the order by cart id.");
writeMessage("Got the order by cart id:".$id_order);
$order = new Order($id_order);
...
I am wondering if the call to Order::getOrderByCartId is correct. Is it the right way to call this function?
Any idea?
I can answer my question. The issue was not on the call to getOrderByCartId as it is normal that at this moment the order is not created yet.
The issue is actually on the validation function of the module, second part of the if.
$module->validateOrder(intval($data['reference']), $orderStatus, $amount, $module->displayName, $orderMessage, NULL, $id_currency, true, $customer->secure_key);

Magento cart not updating quantity in arabic store view

I have the following issue with a Magento 1.8.1 installation. I have two store views, English, which is default, and Arabic.
In the English store view if I add a product to the shopping cart I am able to modify the quantity by inputing the new quantity in the qty box and clicking Update Shopping Cart, which changes the quantity and the totals.
However, when I switch over to the Arabic store view, I can't update the quantity of any products. I input the desired quantity and press update, but the page reloads with only 1 piece of the product, and the totals are unchanged. I've tested and I've noticed that the cart only updates only for quantities over 31.
I've tried / checked the following:
This line is present in cart.phtml - getBlockHtml('formkey'); ?>
This line is present in cart.phtml - getChildHtml('form_before') ?>
There are no Javascript errors
I am using the default cart functionality, no Ajax or anything else.
I've tried replacing the cart.phtml file with the original one in the base folder, the behavior is the same.
I've tried adding the Store ID to the URL, it didn't fix the issue.
CLeared cache manually.
There are no errors in the php logs.
I've set up Manage stocks in Catalog/Inventory and reindexed, it didn't fix the issue.
Any ideas will be greatly appreciated.
Thanks,
Alex
Try to remove this code by overriding it on "local"
if (isset($params['qty'])) {
$filter = new Zend_Filter_LocalizedToNormalized(
array('locale' => Mage::app()->getLocale()->getLocaleCode())
);
$params['qty'] = $filter->filter($params['qty']);
}
from app\code\core\Mage\Checkout\controllers\CartController.php (line 183)
Create to overrride app\code\local\Mage\Checkout\controllers\CartController.php
And remove this also by overriding it on "local"
if (isset($data['qty'])) {
$cartData[$index]['qty'] = $filter->filter(trim($data['qty']));
}
from app\code\core\Mage\Checkout\controllers\CartController.php (line 428)
Create to override app\code\local\Mage\Checkout\controllers\CartController.php
Also remove this by overriding it on "local"
if (isset($params['qty'])) {
$filter = new Zend_Filter_LocalizedToNormalized(
array('locale' => Mage::app()->getLocale()->getLocaleCode())
);
$params['qty'] = $filter->filter($params['qty']);
}
from app\code\core\Mage\Checkout\controllers\CartController.php (line 327)
Create to override app\code\local\Mage\Checkout\controllers\CartController.php

Categories