Problem/Motivation

Report received & verified as reproducible at https://www.drupal.org/project/commerce_cart_redirection/issues/3496009


The Commerce Cart Redirection module allows the site admin to arbitrarily modify the text of the Add to cart button, this causes the add to cart button to lose its theming.

Thoughts

It seems that the class addition depends on the button text being "Add to Cart" fairly late in the rendering process.

In the Commerce Cart Redirection module, I modify the button text earlier in the workflow ( in a form_alter ), which results in the classes not being added as expected. I’ve implemented a workaround in the 3.3.x-dev version using string substitution on the rendered button markup in a post_render callback. It's functional but it's not a feel good solution and it may not be reliable.

Questions and Assistance Needed

  • Is there a recommended way within the Belgrade theme to ensure classes are applied to the button regardless of text changes?
  • Could this behaviour be decoupled from the button text, such as targeting the button via selectors or hooks?
  • Are there any hooks or methods I could use to safely modify the button text without interfering with class additions?

I'd like to make the module work with the Belgrade theme while maintaining flexibility for sites that already use custom button text. Any insights or guidance you can provide would be greatly appreciated.

Thanks!

Issue fork belgrade-3496129

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

andyd328 created an issue. See original summary.

majmunbog’s picture

I can create a custom hook belgrade_button_classes that we could use to override the button classes like this:

/**
 * Implements hook_belgrade_button_classes_alter().
 */
function YOUR_MODULE_NAME_belgrade_button_classes_alter(&$button_classes, $string, $variables) {
  if ($string === t('Add to cart')->render()) {
    $button_classes = [
      'btn',
      'btn-primary',
      'bi-btn-basket'
    ];
  }
}
lavanyatalwar’s picture

Assigned: Unassigned » lavanyatalwar

majmunbog’s picture

Status: Active » Needs review
majmunbog’s picture

Version: 2.0.x-dev » 2.x-dev
lavanyatalwar’s picture

Assigned: lavanyatalwar » Unassigned

sumi made their first commit to this issue’s fork.

rszrama’s picture

Title: Modifying 'Add to cart' button text » Support alternative Add to Cart button text for custom styling
Category: Support request » Feature request

  • majmunbog committed 2e493b43 on 2.x
    Issue #3496129: Support alternative Add to Cart button text for custom...
majmunbog’s picture

Code is merged. All seems to work well.

majmunbog’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.