WT Custom RadicalMart add to cart
- Categories: Joomla plugins, RadicalMart
- Version: 1.0.0
- Date:
The plugin adds an array with the product IDs in the cart and their quantity to the output layouts in the list of products for the RadicalMart online store component for Joomla.
Description
The plugin adds an array with the product IDs in the cart and their quantities to the layouts in the list of products for the component of the RadicalMart online store on Joomla. In this layout templates/<your_ template>/html/com_radicalmart/category/default.php or default_grid.php it will be available
$cart_products = [];
if($this->category->plugins) {
$cart_products = $this->category->plugins->get('cart_products', []);
}
Which can then be passed to the product layout rendering as follows
<?php
// Файл templates/<твой_шаблон>/html/com_radicalmart/category/default_grid.php
// Find the line
echo '<div class="mb-3">' . LayoutHelper::render($layout, ['product' => $item, 'mode' => $this->mode]) . '</div>';
// and add to layout data array cart products
LayoutHelper::render($layout, ['product' => $item, 'mode' => $this->mode, 'cart_products' => $cart_products]) ;
In the final redefinition file, there is already a product layout (a list item in the list of products) templates/<your_ template>/html/layouts/components/radicalmart/products/item/grid.php. Your shopping cart items will already be available.
<?php
// File templates/<твой_шаблон>/html/layouts/components/radicalmart/products/item/grid.php
/**
* Layout variables
* -----------------
*
* @var object $product Product object.
* @var string $mode RadicalMart mode.
* @var array $cart_products RadicalMart products in the shopping cart. We add this line for documentation in the code.
*
*/
// This is a layout of a single product.
// Adding a check for the availability of an item in the cart
// @var bool $in_cart true if product is in cart
$in_cart = array_key_exists($product->id, $cart_products);
?>
<div class="card-footer border-0 bg-transparent">
<div class="d-flex flex-column justify-content-between gap-2">
<div>
<?php
if (!$hidePrice): ?>
<?php
if ($product->price['discount_enable']): ?>
<div class="small text-muted">
<s><?php
echo $product->price['base_string']; ?></s>
</div>
<?php
endif; ?>
<div class="h3 fw-bold m-0"><?php
echo $product->price['final_string']; ?></div>
<?php
elseif (empty($product->in_stock)): ?>
<span class="text-danger">
<?php
echo Text::_('COM_RADICALMART_NOT_IN_STOCK'); ?>
</span>
<?php
endif; ?>
</div>
<div>
<?php if (!$hidePrice && $mode === 'shop' && (int)$product->state === 1): ?>
<div radicalmart-cart="product" data-id="<?php echo $product->id; ?>">
<?php
/**
* Our edits. Hiding the add to cart button if the product is already in the cart.
* We display the buttons for changing the quantity and the quantity of the product itself
* This is a code snippet from the bucket layout,
* which we replaced with the standard quantity field.
*
*/
?>
<?php
/**
* We hide the +/- buttons if the product is not in the cart.
*/
?>
<div class="input-group <?php echo !$in_cart ? 'd-none':'';?>">
<a href="javascript:void(0);"
class="text-danger input-group-text text-decoration-none"
radicalmart-cart="quantity_minus">
<span class="icon-minus"></span>
</a>
<input radicalmart-cart="quantity" type="text" name="quantity" data-set="1"
class="form-control text-center"
step="<?php echo $product->quantity['step']; ?>"
min="<?php
echo $product->quantity['min'] ?? 1; ?>"
<?php
if (!empty($product->quantity['max'])) {
echo 'max="' . $product->quantity['max'] . '"';
} ?>
value="<?php echo $in_cart ? (int)$cart_products[$product->id]: $product->quantity['min']; ?>"
readonly="readonly"/>
<a href="javascript:void(0);"
class="text-success input-group-text text-decoration-none"
radicalmart-cart="quantity_plus">
<span class="icon-plus"></span>
</a>
</div>
<?php
/**
* Hiding the add to cart button if the product is already in the cart.
*/
?>
<button type="button" radicalmart-cart="add"
class="btn btn-outline-primary py-1 fs-6 fw-bold <?php echo $in_cart ? 'd-none':'';?>">
<?php
echo Text::_('COM_RADICALMART_CART_ADD'); ?>
</button>
</div>
<?php
elseif ($hidePrice || $mode === 'catalog'): ?>
<a href="/<?php
echo $product->link; ?>"
class="btn btn-primary fw-bold">
<?php
echo Text::_('COM_RADICALMART_READMORE'); ?>
</a>
<?php
endif; ?>
</div>
</div>
</div>
In order for the +/- buttons to be displayed after adding the product to the cart, you need to add javascript code to the cart page that will do this. So far I have added to the file templates/<your_ template>/html/com_radicalmart/category/default_grid.php However, you can put this code in a separate file and connect it using the WebAsset Manager.
// The trigger is triggered after adding a RadicalMart product to the cart
document.addEventListener('onRadicalMartCartAfterAddProduct', function(event) {
// Getting the contents of the ajax response
// cart is a shopping cart
const cart = event.detail
// entry is an item that has been added to the cart.
const product_id = cart.entry.product_id;
// the +/- buttons and the add to cart button in the product list
const product_buttons = document.querySelector(`[radicalmart-cart="product"][data-id="${product_id}"]`);
// the add to cart button in the list of products
let add_button = product_buttons.querySelector('button[radicalmart-cart="add"]');
// hiding the add to cart button
add_button.classList.add('d-none');
// showing the buttons +/-
let quantity_group = product_buttons.querySelector('.input-group');
quantity_group.classList.remove('d-none');
});
Joomla
- Extension type:
- Plugin
- Folder:
- Radicalmart
- Joomla version:
- 5.4.1