Adjust total price in commerce checkout

I have a store setup with 2 products. Public and private tours. Public tours are 10 for each unit.

Private tours on the other hand are only meant to collect a deposit. I need the total to be $50 no matter the quantity selected and the rest is paid on site.

I tried creating a custom PromotionOffer as seen below. I can create the promotion but it doesn’t seem to apply while in the cart/checkout. I even tried making it a coupon and when I apply the coupon it says its not valid, may have expired or been used. I have one condition set and that is they have a private tour in their cart.

<?php  namespace Drupaltour_reservationPluginCommercePromotionOffer;  use Drupalcommerce_orderAdjustment; use Drupalcommerce_pricePrice; use Drupalcommerce_promotionEntityPromotionInterface; use Drupalcommerce_promotionPluginCommercePromotionOfferPromotionOfferBase; use DrupalCoreEntityEntityInterface; use DrupalCoreFormFormStateInterface;  /**  * Discounts a cart to a fixed amount.  *  * @CommercePromotionOffer(  *   id = "tour_reservation_discount_to_fixed",  *   label = @Translation("Sets cart total to a fixed amount"),  *   entity_type = "commerce_order_item",  * )  */ class OrderFixedPrice extends PromotionOfferBase {    /**    * {@inheritdoc}    */   public function defaultConfiguration() {     return [       'amount' => NULL,     ] + parent::defaultConfiguration();   }    /**    * {@inheritdoc}    */   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {     $form += parent::buildConfigurationForm($form, $form_state);      $amount = $this->configuration['amount'];     // A bug in the plugin_select form element causes $amount to be incomplete.     if (isset($amount) && !isset($amount['number'], $amount['currency_code'])) {       $amount = NULL;     }      $form['amount'] = [       '#type' => 'commerce_price',       '#title' => $this->t('Amount'),       '#default_value' => $amount,       '#required' => TRUE,       '#description' => $this->t('Set cart price. Format: 9.99'),     ];      return $form;   }    /**    * {@inheritdoc}    */   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {     $values = $form_state->getValue($form['#parents']);     if ($values['amount']['number'] < 0) {       $form_state->setError($form, $this->t('Amount cannot be negative.'));     }   }    /**    * {@inheritdoc}    */   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {     parent::submitConfigurationForm($form, $form_state);      $values = $form_state->getValue($form['#parents']);     $this->configuration['amount'] = $values['amount'];   }    /**    * {@inheritdoc}    */   public function apply(EntityInterface $entity, PromotionInterface $promotion) {     $this->assertEntity($entity);     /** @var Drupalcommerce_orderEntityOrderItemInterface $order_item */     $order_item = $entity;     $target_amount = $this->getAmount();     $order_item->addAdjustment(new Adjustment([       'type' => 'promotion',       'label' => t('Discount'),       'amount' => $target_amount,       'source_id' => $promotion->id(),     ]));   }    /**    * Gets the offer amount.    *    * @return Drupalcommerce_pricePrice|null    *   The amount, or NULL if unknown.    */   protected function getAmount() {     if (!empty($this->configuration['amount'])) {       $amount = $this->configuration['amount'];       return new Price($amount['number'], $amount['currency_code']);     }   }  } 

Ideally, I dont want to use promotions and rather just adjust the price in the cart. Any ideas?

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Adjust total price in commerce checkout

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.