Skip to main content
Share Your Experience: Take the 2024 Developer Survey
11 votes
Accepted

Compute price based on order item fields

The short answer: No Why: In the Drupal Commerce Slack Channel, I asked whether it would be possible to add Order Item to the Context class. This was the response: price resolvers are also called ...
Eric Chew's user avatar
  • 126
10 votes
Accepted

How do I get information about the payment method from the order object programmatically?

The Commerce Payment module adds the payment_method base field when it is enabled, see commerce_payment_entity_base_field_info https://github.com/drupalcommerce/commerce/blob/8.x-2.x/modules/payment/...
Matt Glaman's user avatar
  • 1,200
10 votes

How to display a button with “markup”?

You can use the inline_template to solve the problem: // Delete the other button. unset($form); $form['submit'] = [ '#type' => 'inline_template', '#template' => '<button type="button" ...
Adrian Cid Almaguer's user avatar
9 votes

How do I programmatically update the order status?

You want to use the State Machine methods of the Commerce Order class. It would be something like this, but it will depend on what you called your order states. $order_state = $orderObj->...
Chance G's user avatar
  • 2,601
9 votes
Accepted

How can I display search filters in a view?

Below are some guidelines to setting up each of your requirements in a view. Worth noting, in order to display each in the same row, you'll need to customize the view template to combine the exposed ...
Anson W Han's user avatar
  • 1,252
9 votes

d8 preprocess commerce product entity

You can use hook_preprocess_commerce_product like so: function mymodule_preprocess_commerce_product(&$variables) { $product = $variables['elements']['#commerce_product']; ... }
Clive's user avatar
  • 167k
9 votes

LogicException: The database connection is not serializable - when I try to open any "gear" on checkout flow page?

I experienced the same problem. It's not a commerce issue, and there is no general rule which module causes the problem. The problem probably occurs, because one of your checkout panes depend on a ...
Daniel Steindl's user avatar
8 votes
Accepted

How to load current user cart in Commerce 2?

It depends what you need, really. If you just need all carts for all stores, the cart provider service can give you that: $all_carts = \Drupal::service('commerce_cart.cart_provider') ->getCarts();...
Clive's user avatar
  • 167k
7 votes
Accepted

How do I change and translate the "completion message" in Commerce checkouts?

Template approach (recommended) As revealed in the comment by @bojanz, you can just make a copy of the template file in your theme: commerce/modules/checkout/templates/commerce-checkout-completion-...
Patrick Kenny's user avatar
7 votes
Accepted

How to print the product variation separately from the submit/pay button in a Commerce add to cart form?

This is how I fixed this issue: Added this preprocess function in my .theme file function YOURTHEME_preprocess_commerce_product(&$variables) { // Get the commerce product. $product = $...
Fons Vandamme's user avatar
7 votes
Accepted

How do I programmatically load an order by its properties?

The best way to do this is by using by loadByProperties based on Token field: $orders = \Drupal::entityTypeManager() ->getStorage('commerce_order') ->loadByProperties(['field_token_id' =>...
Ajay Reddy's user avatar
  • 3,849
6 votes
Accepted

How do I bypass cart and go straight to checkout?

If you use the last development version of commerce, and look for file commerce\modules\cart\src\Form\AddToCartForm.php which lives under the Drupal\commerce_cart\Form namespace, you can see that ...
Iman Kiani's user avatar
6 votes

How can I programmatically count the number of products in the cart?

In Drupal 8, carts are special orders, related to a user (like regular orders). In a custom module, you can fetch the logged in user orders, filter them by a convenient boolean field called 'cart', ...
Florian Motteau's user avatar
6 votes
Accepted

How do I programmatically change the user ID in a order?

It should be as simple as: $order->setCustomer($user)->save(); Or if you only have the user ID and don't want to load the account object: $order->set('uid', $uid); $order->save();
Clive's user avatar
  • 167k
6 votes
Accepted

Remove commerce item from cart

Orders have a removeItem method defined on their interface. You just need to pass the loaded order item to that method, and then delete the orderitem. /** * Removes an order item. * * @param \...
Matt Glaman's user avatar
  • 1,200
5 votes
Accepted

If Drupal Commerce Cart is not Empty, add a class to body

Using this resource and thanks to @rémy I come up with this solution: function THEME_preprocess_html(&$vars) { global $user; $cart = commerce_cart_order_load($user->uid); foreach (...
Sohail's user avatar
  • 1,544
5 votes
Accepted

Send email to customers after particular period of time?

Answering my own question. I have finally done this using hook_default_rules_configuration() I have created a test rule with static values, then exported it and used the hook mentioned above to ...
Mohit Wadhwa's user avatar
5 votes
Accepted

How to customize order confirmation e-mail

Order receipt emails are powered by Twig templates, just like regular pages. This helps us avoid the inflexibility of tokens and their lack of conditional support ("Only show the Shipping information ...
Bojan Zivanovic's user avatar
5 votes
Accepted

Add an input field to the product view

Instead of adding the field to the product, consider adding the field to order items. Then make it available in 'manage form display' -> 'add to cart'. in Drupal 7 this functionality was provided by ...
Kojo's user avatar
  • 3,523
5 votes
Accepted

Can kint be used to debug templates?

The object/array is too big for your server resources to handle the default kint settings. Edit modules/devel/kint/kint/config.default.php and set $_kintSettings['maxLevels'] to a lower number to ...
No Sssweat's user avatar
  • 31.3k
5 votes
Accepted

Is it possible to collect credit card info and address info on separate pages in Commerce 2?

Creating a checkout flow plugin may solve your problem. Create a Plugin in your custom module like an example shown below: namespace Drupal\my_checkout_flow\Plugin\Commerce\CheckoutFlow; use Drupal\...
Vishal Patil's user avatar
  • 1,905
5 votes

Add a product image to the cart form

This can be accomplished from the front end by simply editing your commerce_cart_form view and using relationships to "relate" your way backwards from the order item to the product display. ...
tonytheferg's user avatar
5 votes
Accepted

In Commerce 2, how can I load an order item's 'purchased entity' object given its ID?

If your $order_item is an instance of the OrderItem class you should be able to do: $product_variation = $order_item->getPurchasedEntity(); $title = $product_variation->getTitle(); If you have ...
Chance G's user avatar
  • 2,601
5 votes

How do I programmatically load an order by its properties?

you will need to use Drupal EntityQuery to get the order id and load order using its id, the following code might help : $query = \Drupal::entityQuery('commerce_order') ->condition('field_token_id....
Alireza Tabatabaeian's user avatar
5 votes

Commerce limit the quantity of product to 1

What you will want to do is add an event subscriber to the Add to Cart event (https://github.com/drupalcommerce/commerce/blob/8.x-2.x/modules/cart/src/Event/CartEvents.php#L30). Here is an example ...
Matt Glaman's user avatar
  • 1,200
5 votes
Accepted

How to enable or disable the "Add to Cart" button for products based on the role of the store owner?

I think the function you need is: MYMODULE_form_commerce_order_item_add_to_cart_form_alter() source I have a similar setup, but I am using the Variation Cart Form module, so some of the details are ...
BWagner's user avatar
  • 405
5 votes
Accepted

How to display the path without alias?

You have to set the option alias: $link = Link::createFromRoute( $this->t('my business'), 'entity.commerce_store.canonical', ['commerce_store' => $store_id], ['alias' => TRUE, '...
4uk4's user avatar
  • 100k
5 votes

Could not find package with stability dev in a version installable using your PHP version

The package that triggers the error is drupalcommerce/project-base. If you check the composer information for that package you see: "require": { "php": ">=7.0.8", "ext-curl": "*...
sanzante's user avatar
  • 6,981

Only top scored, non community-wiki answers of a minimum length are eligible