Drupal 8: Only add to cart if user has enough credits - drupal

I've a site where transactions are based purely on credits. No actual money is exchanged. In such a case, I want to restrict users from adding products to cart if they do not have credits to do so.
In steps of how this should be accomplished, here is the sequence of events which I think should work :
Whenever somebody tries to add a product :
Use Rule "Before adding product to cart"
I need to get the value of the following :
Get user credits
Get sum of current product + cart value
Check for condition if credits is greater than product + cart.
If true, add product to cart else display "Not enough credits".
I use the loyalty points module for the user credits.
How can I set up the rules for this scenario?

Instead of using a module I just used a custom_validation function on commerce_add_to_card_form as suggested by lamp5

Related

Insert Discount - Coupon with SQL in NopCommerce

I do use nopCommerce 3.9 and I would like to insert coupon code from my C# program code. There are requirements such as "Has shopping cart amount" and "Blocked on product". The insert is working fine, I can see the inserted records and I think there are logically right. (The same process happening if I create Discount from Nop admin panel.)When I open the inserted coupon from Nop admin the "Discount info" tab is perfect, on the "Requirements" tab I see the requirements "Requirement Customer has x.xx amount in their shopping cart" and the "Requirement Block Discount on Product", but without value. If I modify the shopping cart value only (I don't modify the "blocked products" on purpose) and save it and reload the page, than everything is good. The blocked products are appeared too.
When I check the data tables I don't see any changes there. The records are the same as I inserted. I guess there is another table(s), where I have to update something, but I cannot find which one it is.
I would appreciate for any help.
From the comments, I believe OP want to insert the new discount rule with two things:
Cart has minimum total X.
Cart has specific product(s) Y.
And the issue is,
Its working properly if they apply discount rule from admin panel,
however, applying it programmatically couldn't store specific
products on a single call.
There are different tables for discount in nopCommerce.
Discount - main table
DiscountRequirement - stores requirement information
Discount_AppliedToProducts - stores discounted products information
Creating a discount rule first time will add entry in discount table only. Then the id of discount table will be used for reference to the DiscountRequirement and Discount_AppliedToProducts table.
It was everything good with my process. The issue was the nopCommerce cache. The nopCommerce keeps the discounts in cache. After I clear the cache I can see my values.
Thanks for everyone who tried to help or had a thought!

WooCommerce - Change customer order after it has been placed and paid

Using three plugins:
WooCommerce
Subscriptio
WooEvents
I have created a product which allows for a customer to pay for an event over five instalments.
A customer has placed an order and made their first of the five payments via PayPal and has then contacted me to advise they have booked the wrong event.
The correct event is exactly the same except the dates are a month sooner.
I have added the following snippet to be able to edit 'processing' orders:
add_filter( 'wc_order_is_editable', 'wc_make_processing_orders_editable', 10, 2 );
function wc_make_processing_orders_editable( $is_editable, $order ) {
if ( $order->get_status() == 'processing' ) {
$is_editable = true;
}
return $is_editable;
}
From the order screen within the back-end of WordPress I can see that the the order can be edit by way of removing the product from the order or editing the meta data and cost.
The correct product that should have been ordered has a different product/variation ID.
My question is simply:
Should I remove the incorrectly ordered product from the order and add the correct product (both have the same properties with the exception of the courses dates); or
Should I just change the meta data and increase the stock levels for the incorrectly product back to X and reduce the stock level for the correct product?
Your second option is much better. Change the meta from database if its accessible and then manage the stock accordingly.
I have not yet done this, but according to this exchange ...
How can I add a product to an existing and paid Woocommerce order?
... you can set the order status to "On Hold" and make changes to the order itself. I presume you would then return the status to "Processing".
I would expect that if the application permits those changes, then it would be doing the background meta changes to inventory levels, etc., and spare you the problem of doing so (and the potential errors that may occur when messing with the data tables outside of the application).
Like I said, I haven't done this. But it might be worthwhile doing a little test to make sure it works as it seems to be described in the answer to the other question.
J

Custom loop for getting products flagged as new

I was looking for a way to build a custom loop to get the products flagged with the "new" label.
I could use the sort from the insert time, but that would always include a certain number of products, my goal here is to get those products that conform to the WooCommerce setting that says the product is considered new for a given number of days.
i have a free plugin - WooCommerce Product Badge, it has your requested feature to display "New" label with certain days defined.

Add custom calculated shipping to the total on checkout in Drupal Commerce?

I want to add custom calculated shipping value (based on the origin of the shipment of the book) to the total amount of the order. I can now calculate the shipping value based on the order ID in my custom module....
I want to be able to add this value to the order total on checkout, is that possible?
I have tried shipment rules, but it is not satisfactory as it only allows me to add a value to the line items in the order and not to total. How can I hook to the checkout process and add this amount to the total? Maybe I can add it as custom line items on checkout?
Also, since it is a custom shipment method with customs shipment companies, do I need to use any of the shipment modules in the first place ( I neither use flat service or any of the commerce shipment modules) ?
Try API of respective commerce/ubercart module. They must offer some API which allows you to modify the check out page

Pagination in application which use firebase.com as database

Front end application will use firebase.com as database.
Application should work like blog:
Admin add / remove posts.
There are "home" page which show several articles per page with "Next" and "Prev" buttons and page which show single article.
Also I need deep linking.
I.e. when user enter URL http://mysite.com/page/2/ I need that application show last articles from 10 to 20, when user enter URL http://mysite.com/page/20/ application show last articles from 200 to 210. Usual pagination.
Main question - is it possible to achieve this if use firebase.com.
I read docs at firebase.com, I read posts here. "offset()" and "count()" will be in the future, use priority, in order to know count of items and not load all of them use additional counter.
Based on this I suppose this:
ADD POST action:
get value of posts counter
set priority for new post data equals to value of posts counter
increase value of posts counter
DELETE POST action
get value of priority for post data
query posts data which have value of priority more than priority for post data which will be deleted and decrease their value
decrease value for posts counter
GET POSTS FROM x TO y
postsRef.startAt(null, x).endAt(null, y).on(... and so on)
Thing which I not like in this way are actions for DELETE POST. Because index of posts will be from 0 to infinity and post with less priority is considered as post which was created earlier so if I have 100000000 posts and if I need to delete 1st post then I need to load data for 99999999 next posts and update their priority (index).
Is there any another way?
Thanks in advance, Konstantin
As you mentioned, offset() will be coming, which makes your job easier.
In the meantime, rather than using startAt and endAt in combination, you could use startAt and limit in combination to ensure you always get N results. That way, deleted items will be skipped. You then need to check the last id of each page before moving to the next to find the proper id to start on.

Resources