Is there any way to add more fields to cart display without hacking src/Utility.php? It's not very informative if your "product" titles are not clear without extra info stored in separate fields (like image, sku, color, size, etc).

Even the main picture for the module (D7 I believe) has some extra information.

A a configurable option to select one of existing view modes instead of hardcoded "title_link" would be very welcome.
Views support would be even better, if it's possible.

As of now I'd be glad to have some non-hacky way to add just one more field to /cart. Is there any?

Comments

scoff created an issue. See original summary.

scoff’s picture

Same issue with email templates. Using just node title in [basic_cart_order:products] is insufficient in some cases.
It's easy to fix but requires code modifications.

Actually this part is easy to override with a custom module.

/**
 * Implements hook_tokens_alter().
 */
function mytweaks_tokens_alter(array &$replacements, array $context, \Drupal\Core\Render\BubbleableMetadata $bubbleable_metadata) {
  $utility = new Utility();

  if ($context['type'] == 'basic_cart_order' && !empty($context['data']['node'])) {
    $get_cart = $utility::getCart();
    $cart = $get_cart['cart'];
    // Building the order details.
    $i = 0;
    $products = '';
    foreach ($cart as $nid => $node) {
      $title = $node->title->value;
      $title .= ' / '. $node->field_sku->value;
      // We don't have prices
//      $price_value = $node->add_to_cart_price->value;
//      $unit_price = $utility::formatPrice($price_value[0]['value']);
      $products .= ++$i . '. ' . $title . "\t x" . $get_cart['cart_quantity'][$nid] . "\n";
    }
    $replacements[$context['tokens']['products']] = $products;
  }
}
milton_segretti’s picture

I was able to get the nid from the basic_cart_delete_url in basic-cart-quantity-prefix.html.twig. From there using twig tweak and a view I was able to add a subtitle to the entries in the cart:

{% set subtitle = basic_cart.delete_url|trim('/cart/remove/', 'left') %}
{{ drupal_view('cart_subtitle_for_cart_contents', 'block_1' , subtitle  ) }}

The view is set to show the content type under the title when it receives an nid.

This all works great if I directly edit basic-cart-quantity-prefix.html.twig within the module folder. If I move it to theme/[theme_name]/templates it doesn't get picked up. That's where I am stuck.

scoff’s picture

This all works great if I directly edit basic-cart-quantity-prefix.html.twig within the module folder. If I move it to theme/[theme_name]/templates it doesn't get picked up. That's where I am stuck.

Only basic_cart_count_block and basic_cart_thank_you templates are currently can be overridden I believe. See here.

j.’s picture

Forgive my ignorance, I am new to working with OOPHP in D8. Could someone point me in the right direction as to how to add additional inputs to the cart form? I need to add a select input that displays 3-4 packaging choices for the product. The choices needs to be add to the cart, and then be able to modified on the actual cart page as well. Where would i start to add that functionality? Thank you.

scoff’s picture

@J. You mean product variants or addons? I don't believe Basic Cart is the right choice for that. We're talking about far more simple situation where product (node) title is not enough to identify actual item (SKU) in cart or in email sent to admins.

EducoWebDesign’s picture

@scoff thank you so much for providing your hook alter code. I wound up in the same place trying to add my SKU field to the basic_cart_order:products token.

I added the hook_tokens_alter code in my custom module and cleared the site cache and I still can't get the changes to be reflected in the email. Were there any other steps you had to take to get this to work?
I stopped being a big dummy and ran drush cr and this worked perfectly! Thank you!
Chris

norman.lol’s picture

Version: 8.x-6.x-dev » 8.x-8.x-dev
norman.lol’s picture

Title: Cart display with extra fields » Add option to select a view mode to display cart items
Assigned: Unassigned » norman.lol

Let's do it like this:

  1. Add a new option to the basic_cart.settings to have admins select a view mode to display the cart items: maybe cart_items_view_mode
  2. If a view mode is selected, than that view mode will be used to display the cart items
  3. If no view mode is selected, it will just follow the current logic of displaying the (linked) titles

I think this would make it really flexible for everybody, while at the same time backwards compatibility is maintained.

  • norman.lol committed f020f2aa on 8.x-8.x
    #3027461 Add option to select a view mode to display cart items
    
norman.lol’s picture

Assigned: norman.lol » Unassigned
Status: Active » Fixed
StatusFileSize
new79.96 KB

Screenshot of the newly added view mode option

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • norman.lol committed c4dcade7 on 8.x-8.x
    #3027461 Add separate view mode option for cart items in the Basic Cart...
norman.lol’s picture

StatusFileSize
new117.61 KB

Added another option to select a separate view mode for the "Basic Cart Block" block.

Screenshot showing the newly added view mode option for the Basic Cart Block block

  • norman.lol committed 5b646f7e on 8.x-8.x
    #3027461 Add missing schema for cart_items_block_view_mode

Status: Fixed » Closed (fixed)

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