Create a (simple) webshop

Rating

tags

Many people like to put something on sale, without having to use too complex modules and structures. With some creativity, you can actually do this.

Case: We sell pizzas. Each pizza has variations. So you can order the size and additional ingredients.

Test everything on  https://project.freewebsite.be/pizza-s

Screen001752.png

You choose the size and if necessary additional ingredients. The options are for each type of pizza different! A Frutti di Mare has other ingredients than the Hawaii. The price is calculated immediately.


Screen001753.png

Then you order using a contact form. Everything is completed automatically, unless the personal data of course. A mail will be automatically sent, both to us and to our customers.


Screen001754.png

The orders are already coming in cash .. cash ..

Screen001755.png

 

Method

Create a content type. Pizza size and Ingredients are taxonomy fields that have the potential  variations from the product (pizza). In what sizes is the pizza available (eg small size) and what ingredients are being used. (Ham, cheese, anchovies, ...).

Choice of size and choice Ingredient are fields that refer to the same vocabulary that the customer uses to make the choice. A prize table is shown with a table field. The 'Order' field is a text field that will be used to generate the button to link to the order form (= contact). Here we will use the Prepopulate module to support the choice of the node on the contact form. A contact form I use rather than a node form because you can mail to the sender (and us) without Rules. The body field will be used in order to make a calculation of the selected combination.

 

Screen001769.png

 

Make 2 Pizzas as an example. Set in the body field (for the moment), this (PHP mode): </ br> </ br> <p> <a style = "color: white; background: gray; padding: 5px;" href = "/ node / [node: nid] / edit"> Calculate a different combination </a> </ p>. The Token Filter module the node ID used as a variable here. It provides a link to edit the content.

Set anonymous user permission to change the nodes . The Field Permission module you use to hide or show the fields for anonymous users. The tab block View and Edit, you hide from  anonymous users.

Screen001773.png

You get this:

Screen001770.png

You can see that the choice of ingredients is limited to the ingredients of the kind of pizzaNormally you go see all the ingredients of all pizzas. This problem has been solved by using a reference view and use it the drop-down field.

Screen001771.png
Screen001772.png

 

Create now a new contact form with the following additional fields:

Screen001774.png

The order field which refers to the contact form looks like this:

<?php
$ingred1='[node:field_keuze_ingredient:0:entity]';
if("["== substr($ingred1,0,1)){
$ingred1=" ";
}
$ingred2='[node:field_keuze_ingredient:1:entity]';
if("["== substr($ingred2,0,1)){
$ingred2=" ";
}
$ingred3='[node:field_keuze_ingredient:2:entity]';
if("["== substr($ingred3,0,1)){
$ingred3=" ";
}
$ingred=$ingred1." ".$ingred2." ".$ingred3;

echo "<p> <a style =" color: white; background: gray; padding: 5px; ' href = ' https://project.freewebsite.be/contact/pizza_bestelling?edit[subject]=B... a pizza [node: title] & edit [message] = At what time you would like the order and edit [field_grootte]? = [node: field_keuze_grootte: 0: entity] and edit [field_extra_ingredienten] = '$ ingred.. "> Buy the selected pizza </a> </ p>";

?>

We use the Prepopulate module. The first step is a check for the three ingredients if they were indeed applied. If they are not used there is a code snippet with the token module. If not used the variable start with a [.

Prepopulate suffers with the Drupal cache. When a visitor makes a second choice, the first will be displayed. You solve this by providing a block on the contact forms that looks like this: <? Php \ Drupal :: service (page_cache_kill_switch) -> trigger (); ?>

Calculation

Configure the table (with a little Excel you can..) into a one on one table, where every choice has a unique price. Create a content type and read it in with feeds. So much easier ..

 
  Hawaii Seafood
size base ham mozzarella pineapple salami base mussels anchovy scampi mozzarella
small                       € 10.00           € 1.00                                                       € 1.00        € 1.50               € 1.20             € 12.00                     € 1.50           € 1.20        € 1.80                € 1.20
large                       € 15.00           € 2.00                                                       € 2.00        € 2.50               2,20 €             18,00 €                     € 2.50           2,20 €        € 3.80                € 2.90

 

The result:

 
Hawaii small base     € 10.00
Hawaii small ham        € 1.00
Hawaï small mozarella        € 1.00
Hawaii small pineapple        € 1.50
Hawaii small salami        € 1.20
Hawaii large base     € 15.00
Hawaii large ham        € 2.00
Hawaii large mozzarella        € 2.00
Hawaii pineapple wide        € 2.50
Hawaii Large salami        2,20 €
Seafood small basis     € 12.00
Seafood small mosseltjes        € 1.50
Seafood small Ansjovis        € 1.20
Seafood salad scampi's        € 1.80
Seafood small mozarella        € 1.20
Seafood large basis sea     18,00 €
Seafood large mosseltjes sea        € 2.50
Seafood large Ansjovis sea        2,20 €
King's Seafood scampi sea        € 3.80
Seafood large mozarella sea        € 2.90

 

The body field used for this calculation. I use php and the token filter to read the variables from Drupal.

<? php? 
$ title = '[node: title]; 
size = $ '[node: field_keuze_grootte: entity: name] "; 
if ( "[" == substr ($ size, 0.1)) { 
$ size = ""; 
echo "no pizza size selected"; 
}

if ($grootte!=" "){

$extra='[node:field_keuze_ingredient:0:entity:name]';
if("["== substr($extra,0,1)){
$extra=" ";
}
$extra2='[node:field_keuze_ingredient:1:entity:name]';
if("["== substr($extra2,0,1)){
$extra2=" ";
}
$extra3='[node:field_keuze_ingredient:2:entity:name]';
if("["== substr($extra3,0,1)){
$extra3=" ";
}

$ search = $ title. "". $ size. "". $ extra; 
$ zoekterm2 = $ title. "". $ size. "". $ extra2; 
$ zoekterm3 = $ title. "". $ size. "". $ extra3; 
$ title = $ title basis "" $ size.. "basic." 
if ($ extra = "!") { 
$ string = "Calculate for" $ title.. "" $ size "with extra" $ extra...; 

Else { 
echo "Calculate for" $ title ": € '.." $ Size.. " 

If ($ extra2 = "!") { 
$ String = $ string. "And" $ extra2.; 

If ($ extra3 = "!") { 
$ String = $ string. "And" $ extra3.; 

 $ Nids = \ Drupal entity :: query ( ' node ') -> condition (' type ',' pizza_prijzen) -> execute (); 
 $ nodes = \ Drupal \ node \ Entity \ Node :: load multiple ($ nids); 

$ Extra price = 0;

foreach ($nids as $nid) {
  $node = \Drupal\node\Entity\Node::load($nid); 
  $title = $node->title->value;

if ($ base == title $ title) { 
// echo "found title base"; 
 $ base price = $ node-> field_prijs-> value; 
}

  if ($ search term == $ title) { 
    $ extra price = $ + $ extra price node-> field_prijs-> value;   

 If ($ zoekterm2 == $ title) { 
    $ extra price = $ + $ extra price node-> field_prijs-> value;   

 If ($ zoekterm3 == $ title) { 
    $ extra price = $ + $ extra price node-> field_prijs-> value;   
}

}   
 $ = $ Cost $ base price + additional charges; 
    echo $ string "€" $ total price..; 

>? 
</ Br> </ br> <p> <a style = "color: white; background: gray; padding: 5px;" href = "/ node / [node: nid] / edit"> Calculate a different combination </a> </ p>