count how many times a class appears in a page with codeception - codeception

I'm using Codeception to run acceptance tests and I need to count how many times a button with ".remove" class appears in one page. Such button is located inside a html table and the count depends on how many items are in the cart.
Below is the code I'm trying to use:
$I->amOnPage("/cart/");
$table = $I->grabTextFrom(".//*[#id='cart']/table");
$rows = explode("<tr>", $table);
$rcount = count($rows);
while ($rcount >= 0) {
$I->click(".remove");
$rcount--;
}
$I->see("Your shopping cart is empty.");

I know this is a bit late, but you should try $I->seeNumberOfElements as explained here:
http://codeception.com/docs/modules/WebDriver#seeNumberOfElements

$arrayProducts = $I->grabMultiple(XPATH_PRODUCTS_BOX);
$sumProducts = count($arrayProducts);
$I->comment("In category $selectCategory is total product: $sumProducts");
$I->seeNumberOfElements(XPATH_PRODUCTS_BOX, $sumProducts);
return $sumProducts;

Related

Automatically increase stock after order - Prestashop

I manually manage my stocks on Prestashop. I am looking for a solution to automatically return the initial stock after a sales order.
For example, a product is ordered in two copies with an initial stock of 7. I would like the stock to remain at 7 after the order and not at 5.
Do you know a technique that would allow me to realize this automatically?
Put a Hook on Order Confirmation (displayOrderConfirmation) in a new module (you can generate one at https://validator.prestashop.com/) and check whats inside the cart then put it again in your stocks :
public function hookDisplayOrderConfirmation($params) {
$order = $params['order'];
$cart = new Cart($order->id_cart);
$products = $cart->getProducts();
foreach ($products as $product) {
$removed_qty = (int) $product['quantity'];
$past_qty = (int) StockAvailable::getQuantityAvailableByProduct($product['id_product'], $product['id_product_attribute']);
$new_qty = $removed_qty + $past_qty;
StockAvailable::setQuantity($product['id_product'], $product['id_product_attribute'], $new_qty);
}
}

multi store, manual activation account with prestashop

I use multi-store option with prestashop. I would like to pass customers in the second store to manual activation after registration.
Actually I set $customer->active = 0; in authentication.php.
all registration customer in both websites are inactive after registration.
Is there a way to set $customer->active = 0; just for one website.
I think to get shop_id but I don't know how to develop my idea.
In Prestashop 1.6 :
You can get the id_shop with the Context object.
So, I think you can do something like this :
If you know the id_shop (suppose the id_shop = 1)
if (Context::getContext()->shop->id == 1) {
$customer->active = 0;
} else {
$customer->active = 1;
}
Hope it helps.
EDIT
Updated answer to get the id_shop from context because the Customer object doesn't handle it until it's added.
RE-EDIT
In the Customer class (/classes/Customer.php) customize the add() function.
Add this line around the line 212 (after the "last_passwd_gen" declaration) :
$this->active = ($this->id_shop == 3) ? false : true;
But the best solution for you is to create an override of the function.

Bigcommerce paging through orders

I am pulling back one months of data using:
$page = 1;
$filter = array('page' => $page,'limit' => 250,'min_date_created' => $startDate );
$orders = Bigcommerce::getOrders($filter);
I am passing in $startDate as 2014-01-06 (which works for the orders).
The order has approx 600 so I need to page through the results.
I am using:
$filterCount = array ('status_id' => 2 ,'min_date_created' => $startDate);
$count = Bigcommerce::getOrdersCount($filterCount);
I should then be able to divide $count by 250 to give me my number of pages. However $count is always 8773 no matter how I play around with $filterCount.
Am I doing something dumb here ?
Mr Warby.
You want to call getOrders with your filter parameters instead of getOrdersCount as that resource always returns the total number of orders.
Looking at your code you are using the PHP Library.
Looking at the getCount method it doesn't take a 'filter' argument.
public static function getCount($path)
{
$response = self::connection()->get(self::$api_path . $path);
if ($response == false || is_string($response)) {
return $response;
}
return $response->count;
}
As you said I would use $reponse->count / 250 to give you your pages.
Instead of +1 to this I would use $pages = ceil($reponse->count / 250) then inside your FOR statement use LESS THAN EQUAL TOO and this will always give you the correct amount of pages.
When looping through the pages check for no reponse as your are filtering it between dates the actual pages needed is likely to be a lot less. if an empty object is received you could then break the loop. (I think they are sorted oldest -> newest).
Hope this helps.

Magento cart error "Maximum one item to add the shopping cart."

I checked the option in the system -> config -> Inventory and it's set to 1000 the Maximum Qty Allowed in Shopping Cart i can add the same product so it supplements the Qty of that product by one more, but if i add another product i get this Maximum Qty Allowed in Shopping Cart , any idea how can i fix the cart so i will be able to add more items to the cart ?
PS: i have Magento ver. 1.6.1.0
Thanks all in advance!
I would like to recommend you to do the following:
First check system -> config -> Inventory settings for all your stores. Typically users get such errors when the settings are inappropriate on different store leves.
But if you get this error adding another product, then go to the Inventory tab of your product and check whether the Maximum Qty Allowed in Shopping Cart is populated correctly.
Your error 'Maximum one item to add the shopping cart.' is quite unique and it's actually a custom error not native to Magento. I've found it in some code of a client of mine. You'll have to look for the following code blocks in a few files:
if ($cartItems >= 1) {
Mage::getSingleton('core/session')->addError($this->__('Maximum one item to add the shopping cart.'));
$this->_goBack();
return;
}
and
if ($params['product'] != $productId) {
if ($cartItems >= 1) {
$this->_getSession()->addError($this->__('Maximum one item to add the shopping cart.'));
$this->_goBack();
return;
}
}
and
if ($params['product'] != $productId) {
if ($cartItems > 1) {
Mage::getSingleton('checkout/session')->addError($this->__('Maximum one product to add the shopping cart.'));
$this->_redirect('checkout/cart');
return;
}
}
and
if ($item->getProductId() != $productId) {
if ($cartItems >= 1) {
$this->_getSession()->addError($this->__('Maximum one item to add the shopping cart.'));
$this->_goBack();
return;
}
}
You'll be likely to find them in /app/code/local/{Name}/{Module}/controllers/Checkout/CartController.php /app/code/local/{Name}/{Module}/controllers/Checkout/OnepageController.php /app/code/local/{Name}/{Module}/controllers/Sales/OrderController.php
Mentionably, {Name} is not necessarily limited to one extension... I've found it in multiple, perform a search all through all the files in /app/code/local to be sure.
In order to 'fix' it, either change the '1' in if ($cartItems > 1) { to some other (higher) number, or comment that if statement out and replace it with if(false) {.
if ($params['product'] != $productId) {
if ($cartItems > 1) {
Mage::getSingleton('checkout/session')->addError($this->__('Maximum one product to add the shopping cart.'));
$this->_redirect('checkout/cart');
return;
}
}

SQL to Magento model understanding

Understanding Magento Models by reference of SQL:
select * from user_devices where user_id = 1
select * from user_devices where device_id = 3
How could I perform the same using my magento models? getModel("module/userdevice")
Also, how can I find the number of rows for each query
Following questions have been answered in this thread.
How to perform a where clause ?
How to retrieve the size of the result set ?
How to retrieve the first item in the result set ?
How to paginate the result set ? (limit)
How to name the model ?
You are referring to Collections
Some references for you:
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-5-magento-models-and-orm-basics
http://alanstorm.com/magento_collections
http://www.magentocommerce.com/wiki/1_-_installation_and_configuration/using_collections_in_magento
lib/varien/data/collection/db.php and lib/varien/data/collection.php
So, assuming your module is set up correctly, you would use a collection to retrieve multiple objects of your model type.
Syntax for this is:
$yourCollection = Mage::getModel('module/userdevice')->getCollection()
Magento has provided some great features for developers to use with collections. So your example above is very simple to achieve:
$yourCollection = Mage::getModel('module/userdevice')->getCollection()
->addFieldToFilter('user_id', 1)
->addFieldToFilter('device_id', 3);
You can get the number of objects returned:
$yourCollection->count() or simply count($yourCollection)
EDIT
To answer the question posed in the comment: "what If I do not require a collection but rather just a particular object"
This depends if you still require both conditions in the original question to be satisfied or if you know the id of the object you wish to load.
If you know the id of the object then simply:
Mage::getModel('module/userdevice')->load($objectId);
but if you wish to still load based on the two attributes:
user_id = 1
device_id = 3
then you would still use a collection but simply return the first object (assuming that only one object could only ever satisfy both conditions).
For reuse, wrap this logic in a method and place in your model:
public function loadByUserDevice($userId, $deviceId)
{
$collection = $this->getResourceCollection()
->addFieldToFilter('user_id', $userId)
->addFieldToFilter('device_id', $deviceId)
->setCurPage(1)
->setPageSize(1)
;
foreach ($collection as $obj) {
return $obj;
}
return false;
}
You would call this as follows:
$userId = 1;
$deviceId = 3;
Mage::getModel('module/userdevice')->loadByUserDevice($userId, $deviceId);
NOTE:
You could shorten the loadByUserDevice to the following, though you would not get the benefit of the false return value should no object be found:
public function loadByUserDevice($userId, $deviceId)
{
$collection = $this->getResourceCollection()
->addFieldToFilter('user_id', $userId)
->addFieldToFilter('device_id', $deviceId)
;
return $collection->getFirstItem();
}