Jump to content

Module - Shop for Processwire - Selectable options and the cart


NooseLadder
 Share

Recommended Posts

Hi,

I am putting together a site using Apeisa's Shop for Processwire. I need some help to modify the shoppingcart and possibly the checkout module.

I would like a size selector (dropdown) on some of the product pages (I suppose it could be anything size, colour etc, but in this case it is size only).

-Product List from children (Created with template and php file)

                         -Product 1 (created with template without php file so they are not visible on site, but with the same fields as the top level page)

                         -Product 2

                         -Product 3

Each product is a size variation only and the size dropdown on the top level page uses the following code:

if("$page->children->product_size") {
                        $sizes = $page->children;
                        echo "<p class='size_select'>Select required size: <select name='size'>";
                        foreach($sizes as $size) {
                            echo "<option value='{$size->id}'>{$size->product_size}</option>";
                    }
                            echo "</select></p>";
                            
                    }
 

The shopping uses the current page for the product details. I would like to post the selected size and details from the Product (1,2 or 3) based on what is selected from the dropdown.

I hope that makes sense.

Link to comment
Share on other sites

if("$page->children->product_size") {

This is not a valid statement, in PHP or in ProcessWire. If it were valid in ProcessWire, then you could just remove the quotes and it would work. Either that, or you could surround it with "{$page->children->product_size}". However, there is no "product_size" property of PageArray, and $page->children is a PageArray. I'm thinking that maybe you meant this?

if($page->child("product_size>0")->id) {
Link to comment
Share on other sites

Oh, I thought I read somewhere on this forum that by enclosing the if in quotes it returned true or false. I'm obviously no expert so I will take your advice.

Maybe its a fluke but this returns a list of sizes from the children, so I am a bit confused?

if("$page->children->product_size") {
                        $sizes = $page->children;
                        echo"<ul>";
                        foreach($sizes as $size) {
                            echo"<li>{$size->product_size}</li>";
                            }
                        echo "</ul>";
 

The product size is a text field because the client is using this sort of sizing 5/6, 7/8. 9/10 (age groups) etc.

I would be grateful if you clarify the validity of the code.

My main problem is how to post the sizes from the children to the shoppingcart module.

Link to comment
Share on other sites

Apart from the if statement, the code is perfectly valid. In this case It's working because by enclosing the statement in quotes it becomes a string, and a string is always true. The problem is when you need it to be also false, because it never will...

Ryan's example won't work because 5/6, 7/8 (etc) are less than 0.

Maybe by testing if there is at least one child where the field is not empty?

if($page->children->get("product_size!=")->id)
Link to comment
Share on other sites

As Ryan stated this code is wrong. 

if("$page->children->product_size") {
 

It doesn't make sense to put such code in quotes, no matter what. I think you mix this up with variables in

echo "<p>$somevar</p>"; 
 

Or in a selector

$pages->find("template=$tpl");
 
 

Putting those in single quotes wouldn't work because they won't get parsed by php.

However looking at your code, as Digo mentioned the if statement will alway be true, as it's just a string and not 0, null or false.

So if you write this:

if($page->children->product_size) {
 

Better now but still wrong, as Ryan said there's no product_size property or method in a PageArray.

$page->children is a PageArray! 

children is getting all children and not a single page to say it differently.

Ok, now we got that out of the way and if you want to check if there's children with a product_size field not empty?

$sizes = $page->children->find("product_size!=");
if(count($sizes)) {
    // there's children with size found
    echo "<p class='size_select'>Select required size: <select name='size'>";
    foreach($sizes as $size) {
        echo "<option value='{$size->id}'>{$size->product_size}</option>";
    }
    echo "</select></p>";
}
 

This should get you there.

Link to comment
Share on other sites

Thank you all  for your explanations, I understand it now.

$sizes = $page->children->find("product_size!=");
                    if(count($sizes)) {
                        echo "<p style='font-size:90%;'>Select your size: <select name='size'>";
                        foreach($sizes as $size) {
                            echo "<option value='{$size->id}'>{$size->product_size}</option>";
                    }
                            echo "</select></p>";
                            
                    }
 

The next step is to get the selected $size variable into the shoppingcart module so that it is part of the cart and checkout. Can anyone help with that?

Link to comment
Share on other sites

Now I have a page structure like this:

-products

       - Girls

            -Girls Nightware

                  -Pyjamas type 1 Display 

                            - Pyjamas type 1 size 5/6 

                            - Pyjamas type 1 size 7/8

                            - Pyjamas type 1 size 9/10

The template for Pyjamas type 1 Display shows the single product with a drop-down selector  showing the sizes from the children.

So the user selects the size on page Pyjamas type 1 Display. The reason for this is so I don't have to display duplicate pages of the same product for each size variation.

I would like to pass the size selected to the shopping cart and checkout but this is a bit complicated for me to modify the modules. It looks like the module uses the current page as the product and I don't know how to change this.

I need help please.

Link to comment
Share on other sites

I think you could just create the "add cart" functionality yourself, on template level. Like this:

<form method="post" action="./">

<input type="radio" value="10001" name="sc_product_id"><label>Product variant 1</label>
<input type="radio" value="10002" name="sc_product_id"><label>Product variant 2</label>

<input type="number" class="sc_qty" name="sc_qty" value="1"> Qty <input type="submit" value="Add to basket">
</form>

It doesn't matter what is your form action, the shopping cart module looks for sc_qty on all pages. sc_product_id value should be the page id of the variant you are adding to a cart.

Link to comment
Share on other sites

Thanks Apeisa. Implemented as follows:

<?php $sizes = $page->children->find("product_size_selector!=");
                    if(count($sizes)) {
                        
                        echo "<form method='post' action='./'>
                        
                        <p style='font-size:90%;'>Select your size: ";
                        
                        foreach($sizes as $product) {
                            
                        echo "<input  type='radio' value='{$product->id}' name='sc_product_id'><label style='padding-right:0.5em;'>{$product->product_size_selector->title}</label>";
                        }
                        echo "</p>";
                        echo "<p><input type='number' name='sc_qty' value='1'/>
                            <input type='submit' value='Add to basket') /></p>
                            
                            </form>";
                        } ?>
 

BTW, In module PaymentExample.module this line was missing which prevented the module title appearing in the select payment method dropdown:

public function init() { // line 17
$this->title = $this->_("Payment Example"); // This line was missing
 

In PaymentExample.module you output the order details with a comment

 //  No need to really do anything like this on payment module, this is here just for educational purposes):

Maybe we can turn this into an invoice to email to the customer and/or for the customer to print as a receipt?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...