Shopware 6 : Check globally if customer is connected - twig

How to check globally if customer is logged in in all template of Shopware 6 (not for a specific route) ?
I want to check this variable using template twig.

You can use the following if-statement in any template to check whether a customer is currently logged-in:
{% if context.customer is defined %}
customer is logged in
{% endif %}

Related

How to save custom input field from checkout page to order in Shopware6 app?

I add custom input field to checkout page like this:
% sw_extends '#Storefront/storefront/page/checkout/confirm/confirm-shipping.html.twig' %}
{% block page_checkout_confirm_shipping_form %}
{{ parent() }}
<input type="text" id="some_info_text" name="some_info_text">
{% endblock %}
I need to save information from this input field to order. This is not plugin, it's an app integration.
It seems app doesn't have event subscribers.
How can I implement this field saving?
I've checked Shopware 6 app docs, but I haven't found right solution.
I think there is no way to do that. From Shopware App you can't influence which data Shopware will send.

Shopware 6: How to display product name in breadcrumbs

We want to add the product name to the breadcrumb list when current page matches a product detail page.
Unfortunately, it doesn't seem like the product is loaded yet when the breadcrumb is rendered. We have tried using dump() to see what variables are available and nothing related to the product.
Where should I look in order to include the product name in our breadcrumbs?
Have a look at storefront/base.html.twig. There you will see, that currently the breadcrumb-template gets passed the context and the category. If you want to also use some product-information, you have to overwrite this block like this:
{% block base_breadcrumb %}
{% sw_include '#Storefront/storefront/layout/breadcrumb.html.twig' with {
context: context,
category: page.product.seoCategory,
product: page.product
} only %}
{% endblock %}
Then you can use product in the breadcrumb-template.

Drupal 8 Twig User ID?

I'm new to Drupal & Twig and all I need is in my custom theme a twig expression to output the current user's ID. I can't find anything in the template comments, only if a user is logged in true / false.
Is there a simple way to get the ID of the current user? I'm not sure how to implement custom methods in a theme.
thanks!
Hello bobomoreno,
I would suggest you use the module Bamboo Twig.
The Bamboo Twig module provides some Twig extensions with some useful functions and filters aimed to improve the development experience.
You could then enable the sub-module Bamboo Twig - Loaders:
drush en bamboo_twig_loader -y
Finally, you will be able to use the Twig function bamboo_load_currentuser:
<!-- Get Current User -->
{% set user = bamboo_load_currentuser() %}
<div>{{ user.name.value }}</div>
<div>{{ user.uid.value }}</div>
You can find the complete official documentation there.
In your theme find file yourthemename.theme and add following code:
function yourthemename_preprocess(&$vars, $hook)
{
$vars['uid'] = \Drupal::currentUser()->id();
}
now if you edit your twig template for html, page, region, block, field, form element... you can use 'uid' token in your twig. It works for all hooks
If you only need the ID in user.html.twig, it's {{ user.id }}
Here's how D8 now works, in two lines of executable code:
<?php
// This code returns the current user ID.
$account = \Drupal::currentUser();
return $account->id();
The display name is not a field you can configure in {{ content }}. You can get it directly from the user entity:
{{ user.displayname }}
Reference for the php method: AccountInterface::getDisplayName
The Twig Tweak module is very small and yet very powerful. You can get the current user id with drupal_token('current-user:uid') I am using it to pass the current user id to a view like this:
{{ drupal_view('view_name', 'embed_1', drupal_token('current-user:uid')) }}

Grav CMS: how to show/hide parts of the page depending on conditions?

The Grav's documentation clearly describes how a whole page or a folder could be hidden from unregistered users. It also describes how a whole page could be seen only by particular user groups.
But what about pieces of a page, let's say, some links or a private info I want to show on some conditions?
Ok, for registered users I found a snippet at Login plugin docs:
{% if grav.user.authenticated %}
content for registered users goes here
{% endif %}
But going wider - how can I show/hide pieces of a particular page depending on some custom logic in PHP code, i.e. not necessarily user related?
I'm thinking about a twig/shortcode plugin, something like:
{% if some.custom.condition.or.PHP.function %}
hidden content goes here
{% endif %}
or
[hidden_if_something] hidden content goes here [/hidden_if_something]
But not sure how exactly this should be implemented. So working examples would be appreciated. Thanks.
There is a recipe in the Grav documentation here. This provides an example of how to render the output of a PHP code result in a twig template.
In the example they create a plugin, and implement a twig extension providing access to a php function. They can then simply call that php function like in a twig template.
{{ example() }}
Following that example, you can implement whatever logic you would like in php, and call the function in a twig if statement.
{% if example() == true %}
your conditional output
{% endif %

Membership Level issue in NationBuilder

I have a website built in NationBuilder. There I needed to make another Membership level. I have done that. But the issue is that it is not showing in "Who can view this page" dropdown in page settings. Anyone has any idea how to do that.?
Thanks in advance
It seems you can't control who can view this page via membership levels.
From a NationBuilder FAQ, How do I set up a five-tier membership site:
In NationBuilder... being a member is a binary state: you either are
or you are not a member. While NationBuilder allows you to sell
various membership levels, the database doesn't recognize those
various levels as distinct and different from a permissions level.
That means that if you make a page viewable to "Members" it will be
viewable to all members, equally.
There's really no good workaround for this that's based on membership
level of which I am aware. You can, however, use conditional liquid
tags to display different content on a given page to logged-in users
depending on what tags their NationBuilder profile has. Since each
membership level can be configured to automatically tag people, this
method could be employed to display different content to individuals
who have different membership levels (or none at all).
You can accomplish this by editing the page template. For instance, suppose you wished to restrict a page to only users with MEMBER_A membership.
{% assign hasPagePermission = false %}
{% for membership in request.current_signup.memberships %}
{% if membership.membership_type_name == "MEMBER_A" and (membership.status == "active" or membership.status == "grace period") %}
{% assign hasPagePermission = true %}
{% endif %}
{% endfor %}
{% if hasPagePermission == true %}
{{ page.basic.content }}
{% else %}
{% include 'access_denied' %}
{% endif %}
I realise that your original post was six years ago, however I post this in the hope that it will be helpful to others (or indeed me when I forget how to do this and end up searching for this again!)

Resources