Shopkit Buy Button in other templates

Hello, i try to get the “buy button” to work in other templates/ snippets then the product.php. but it does not seem to work. Any suggestions what i need to do in order to get a buy button underneath the products in category.php for example ? Thx

Could you please post what you have tried so far?

<?php if(count($products) or $products->count()) { ?>
	<ul class="listing uk-container uk-padding-remove">
	  <?php foreach($products as $product): ?>
		<li class="uk-margin-right uk-margin-bottom">
				<?php 
					if ($product->hasImages()) {
						$image = $product->images()->sortBy('sort', 'asc')->first();
					} else {
						$image = $site->images()->find($site->placeholder());
					}
					$thumb = $image->thumb(['height'=>150]);
					$backgroundThumb = $image->thumb(['height'=>300,'width'=>300,'crop'=>true,'blur'=>true]);
				?>
				<div class="image" <?php if ($backgroundThumb) echo 'style="background-image: url('.$backgroundThumb->dataUri().');"' ?>>
					<img property="image" content="<?php echo $thumb->url() ?>" src="<?php echo $thumb->dataUri() ?>" title="<?php echo $product->title() ?>">
				</div>

				<div dir="auto" class="uk-margin-small-top">					
					<?php if ($product->brand() != '') { ?>
						<small class="brand" property="brand"><?php echo $product->brand() ?></small>
					<?php } ?>
					<h3 class="uk-margin-remove" property="name"><?php echo $product->title()->html() ?></h3>

		    		<?php
		    			$count = $minPrice = $minSalePrice = 0;
		    			foreach ($product->variants()->toStructure() as $variant) {
		    				// Assign the first price
		    				if (!$count) {
		    					$minVariant = $variant;
		    					$minPrice = $variant->price()->value;
		    					$minSalePrice = salePrice($variant);
		    				} else if ($variant->price()->value < $minPrice){
		    					$minVariant = $variant;
		    					$minPrice = $variant->price()->value;
		    					$minSalePrice = salePrice($variant);
		    				}
		    				$count++;
		    	

							// hasOptions
							$options = str::split($variant->options());
							if (count($options)) {
								$variant->hasOptions = true;
							} else {
								$variant->hasOptions = false;
							}

							// priceText
							if (inStock($variant)) {
								$variant->priceText = l::get('buy').' ';
								if ($saleprice = salePrice($variant)) {
									$variant->priceText .= formatPrice($saleprice);
									$variant->priceText .= '<del>'.formatPrice($variant->price()->value).'</del>';
								} else {
									$variant->priceText .= formatPrice($variant->price()->value);
								}
							} else {
								$variant->priceText = l::get('out-of-stock').' ';
								if ($saleprice = salePrice($variant)) {
									$variant->priceText .= formatPrice($saleprice);
									$variant->priceText .= '<del>'.formatPrice($variant->price()->value).'</del>';
								} else {
									$variant->priceText .= formatPrice($variant->price()->value);
								}
							}
						}
					?>

					<button <?php e(inStock($variant),'','disabled') ?> class="uk-button uk-button-primary uk-width-1-1" type="submit" property="offers" typeof="Offer">
						<?= $variant->price() ?>
						<link property="availability" href="<?php e(inStock($variant),'http://schema.org/InStock','http://schema.org/OutOfStock') ?>" />
					</button>
				</form>

				<?= $variant->stock() ?>/<?= $variant->full_stock() ?>

				</div>
		</li>
	  <?php endforeach ?>
	</ul>
<?php } ?>

I put the button in from the product.php and the code from the product.php controller. On click the cart does not get updated.

The submit button is part of the form below, so you can’t just copy the button, because the button alone does not know which script to call. The action attribute calls the /shop/cart page.

<form class="uk-form uk-panel uk-panel-box" method="post" action="<?= url('shop/cart') ?>">

								<!-- Schema.org markup -->
				            	<?php if($page->hasImages()) { ?>
				            		<link property="image" content="<?= $page->images()->first()->url() ?>" />
				            	<?php } ?>
				            	<link property="brand" content="<?= $page->brand() ?>" />

				            	<!-- Hidden fields -->
				            	<input type="hidden" name="action" value="add">
				            	<input type="hidden" name="uri" value="<?= $page->uri() ?>">
				            	<input type="hidden" name="variant" value="<?= str::slug($variant->name()) ?>">

								<h3 dir="auto" class="uk-margin-small-bottom" property="name" content="<?= $page->title().' &ndash; '.$variant->name() ?>"><?= $variant->name() ?></h3>

								<div property="description">
									<?php ecco(trim($variant->description()) != '',$variant->description()->kirbytext()->bidi()) ?>
								</div>

								<?php if ($variant->hasOptions) { ?>
									<select dir="auto" class="uk-width-1-1" name="option">
										<?php foreach (str::split($variant->options()) as $option) { ?>
											<option value="<?= str::slug($option) ?>"><?= str::ucfirst($option) ?></option>
										<?php } ?>
									</select>
								<?php } ?>

								<button <?php e(inStock($variant),'','disabled') ?> class="uk-button uk-button-primary uk-width-1-1" type="submit" property="offers" typeof="Offer">
									<?= $variant->priceText ?>
									<link property="availability" href="<?php e(inStock($variant),'http://schema.org/InStock','http://schema.org/OutOfStock') ?>" />
								</button>
				            </form>

@texnixe is right. Your button must POST to the cart page, and pass three variables: action, uri, and variant. You may also pass the option variable.

thx i managed to get it work - i missed passing the right uri, thats why the errors occured.

1 Like