Skip to content

Commit

Permalink
Merge pull request #60 from DogByteMarketing/main
Browse files Browse the repository at this point in the history
Added: Brand and onsale attribute
  • Loading branch information
crftwrk authored Nov 7, 2023
2 parents 3dfb977 + b49d3d3 commit 27fd9af
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 72 deletions.
30 changes: 15 additions & 15 deletions templates/sc-swiper-card-autoplay.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
function bootscore_swiper_autoplay($atts) {

ob_start();
extract(shortcode_atts(array(
$atts = shortcode_atts(array(
'type' => 'post',
'order' => 'date',
'orderby' => 'date',
Expand All @@ -55,19 +55,19 @@ function bootscore_swiper_autoplay($atts) {
'excerpt' => 'true',
'tags' => 'true',
'categories' => 'true',
), $atts));
), $atts);

$options = array(
'post_type' => $type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts,
'category_name' => $category,
'post_parent' => $post_parent,
'post_type' => sanitize_text_field($atts['type']),
'order' => sanitize_text_field($atts['order']),
'orderby' => sanitize_text_field($atts['orderby']),
'posts_per_page' => is_numeric($atts['posts']) ? (int) $atts['posts'] : -1,
'category_name' => sanitize_text_field($atts['category']),
'post_parent' => is_numeric($atts['post_parent']) ? (int) $atts['post_parent'] : '',
);

$tax = trim($tax);
$terms = trim($terms);
$tax = trim(sanitize_text_field($atts['tax']));
$terms = trim(sanitize_text_field($atts['terms']));
if ($tax != '' && $terms != '') {
$terms = explode(',', $terms);
$terms = array_map('trim', $terms);
Expand All @@ -81,8 +81,8 @@ function bootscore_swiper_autoplay($atts) {
));
}

if ($id != '') {
$ids = explode(',', $id);
if ($atts['id'] != '') {
$ids = explode(',', sanitize_text_field($atts['id']));
$ids = array_map('intval', $ids);
$ids = array_filter($ids);
$ids = array_unique($ids);
Expand Down Expand Up @@ -112,7 +112,7 @@ function bootscore_swiper_autoplay($atts) {

<div class="card-body d-flex flex-column">

<?php if ($categories == 'true') : ?>
<?php if ($atts['categories'] == 'true') : ?>
<?php bootscore_category_badge(); ?>
<?php endif; ?>

Expand All @@ -131,7 +131,7 @@ function bootscore_swiper_autoplay($atts) {
</p>
<?php endif; ?>

<?php if ($excerpt == 'true') : ?>
<?php if ($atts['excerpt'] == 'true') : ?>
<p class="card-text">
<a class="text-body text-decoration-none" href="<?php the_permalink(); ?>">
<?= strip_tags(get_the_excerpt()); ?>
Expand All @@ -145,7 +145,7 @@ function bootscore_swiper_autoplay($atts) {
</a>
</p>

<?php if ($tags == 'true') : ?>
<?php if ($atts['tags'] == 'true') : ?>
<?php bootscore_tags(); ?>
<?php endif; ?>

Expand Down
55 changes: 43 additions & 12 deletions templates/sc-swiper-card-product.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,46 @@
* [bs-swiper-card-product]
*
* Optional:
* category="cars, boats" Will pull products matching these categories (Default: '')
* id="1, 2, 3" Will show products matching these ids (Default: '')
* posts="12" Specify how many products will be shown (Default: 12)
* orderby="date" Specify how products will be ordered by (Default: date)
* order="DESC" Specify if products will be ordered ASC or DESC (Default: DESC)
* featured="true" Will pull featured products (Default: false)
* outofstock="false" Will hide out of stock products (Default: true)
* orderby="date" Specify how products will be ordered by (Default: date)
* posts="12" Specify how many products will be shown (Default: -1)
* id="1, 2, 3" Will show products matching these ids (Default: '')
* category="cars, boats" Will pull products matching these categories (Default: '')
* brand="brand1, brand2" Will pull products matching these brands (Default: '')
* featured="true" Will pull featured products (Default: '')
* outofstock="true" Will show out of stock products (Default: '')
* onsale="true" Will show only onsale products (Default: '')
*
*/


// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
defined('ABSPATH') || exit;


// Product Slider Shortcode
add_shortcode('bs-swiper-card-product', 'bootscore_product_slider');
function bootscore_product_slider($atts) {
function bootscore_product_slider($atts)
{

ob_start();
$atts = shortcode_atts(array(
'type' => 'product',
'order' => 'DESC',
'orderby' => 'date',
'limit' => 12,
'id' => '',
'posts' => -1,
'id' => '',
'category' => '',
'brand' => '',
'featured' => '',
'outofstock' => '',
'onsale' => '',
), $atts);

$options = array(
'order' => sanitize_text_field($atts['order']),
'orderby' => sanitize_text_field($atts['orderby']),
'posts_per_page' => is_numeric($atts['limit']) ? (int) $atts['limit'] : 12,
'posts_per_page' => is_numeric($atts['posts']) ? (int) $atts['posts'] : -1,
'product_cat' => sanitize_text_field($atts['category']),
'post_type' => sanitize_text_field($atts['type']),
);
Expand All @@ -57,6 +62,14 @@ function bootscore_product_slider($atts) {
$options['post__in'] = array_map('trim', explode(',', sanitize_text_field($atts['id'])));
}

if ($atts['brand']) {
$options['tax_query'][] = array(
'taxonomy' => 'brand',
'field' => 'slug',
'terms' => array_map('trim', explode(',', sanitize_text_field($atts['brand']))),
);
}

if ($atts['featured'] == 'true') {
$options['tax_query'][] = array(
'taxonomy' => 'product_visibility',
Expand All @@ -66,7 +79,7 @@ function bootscore_product_slider($atts) {
);
}

if ($atts['outofstock'] == 'false') {
if ($atts['outofstock'] != 'true') {
$options['meta_query'] = array(
array(
'key' => '_stock_status',
Expand All @@ -76,6 +89,24 @@ function bootscore_product_slider($atts) {
);
}

if ($atts['onsale'] == 'true') {
$options['meta_query'][] = array(
'relation' => 'OR',
array(
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array(
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
);
}

$query = new WP_Query($options);
if ($query->have_posts()) { ?>

Expand Down
30 changes: 15 additions & 15 deletions templates/sc-swiper-card.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
function bootscore_swiper($atts) {

ob_start();
extract(shortcode_atts(array(
$atts = shortcode_atts(array(
'type' => 'post',
'order' => 'date',
'orderby' => 'date',
Expand All @@ -55,19 +55,19 @@ function bootscore_swiper($atts) {
'excerpt' => 'true',
'tags' => 'true',
'categories' => 'true',
), $atts));
), $atts);

$options = array(
'post_type' => $type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts,
'category_name' => $category,
'post_parent' => $post_parent,
'post_type' => sanitize_text_field($atts['type']),
'order' => sanitize_text_field($atts['order']),
'orderby' => sanitize_text_field($atts['orderby']),
'posts_per_page' => is_numeric($atts['posts']) ? (int) $atts['posts'] : -1,
'category_name' => sanitize_text_field($atts['category']),
'post_parent' => is_numeric($atts['post_parent']) ? (int) $atts['post_parent'] : '',
);

$tax = trim($tax);
$terms = trim($terms);
$tax = trim(sanitize_text_field($atts['tax']));
$terms = trim(sanitize_text_field($atts['terms']));
if ($tax != '' && $terms != '') {
$terms = explode(',', $terms);
$terms = array_map('trim', $terms);
Expand All @@ -81,8 +81,8 @@ function bootscore_swiper($atts) {
));
}

if ($id != '') {
$ids = explode(',', $id);
if ($atts['id'] != '') {
$ids = explode(',', sanitize_text_field($atts['id']));
$ids = array_map('intval', $ids);
$ids = array_filter($ids);
$ids = array_unique($ids);
Expand Down Expand Up @@ -112,7 +112,7 @@ function bootscore_swiper($atts) {

<div class="card-body d-flex flex-column">

<?php if ($categories == 'true') : ?>
<?php if ($atts['categories'] == 'true') : ?>
<?php bootscore_category_badge(); ?>
<?php endif; ?>

Expand All @@ -131,7 +131,7 @@ function bootscore_swiper($atts) {
</p>
<?php endif; ?>

<?php if ($excerpt == 'true') : ?>
<?php if ($atts['excerpt'] == 'true') : ?>
<p class="card-text">
<a class="text-body text-decoration-none" href="<?php the_permalink(); ?>">
<?= strip_tags(get_the_excerpt()); ?>
Expand All @@ -145,7 +145,7 @@ function bootscore_swiper($atts) {
</a>
</p>

<?php if ($tags == 'true') : ?>
<?php if ($atts['tags'] == 'true') : ?>
<?php bootscore_tags(); ?>
<?php endif; ?>

Expand Down
30 changes: 15 additions & 15 deletions templates/sc-swiper-hero-fade.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
function bootscore_swiper_hero_fade($atts) {

ob_start();
extract(shortcode_atts(array(
$atts = shortcode_atts(array(
'type' => 'post',
'order' => 'date',
'orderby' => 'date',
Expand All @@ -55,19 +55,19 @@ function bootscore_swiper_hero_fade($atts) {
'excerpt' => 'true',
'tags' => 'true',
'categories' => 'true',
), $atts));
), $atts);

$options = array(
'post_type' => $type,
'order' => $order,
'orderby' => $orderby,
'posts_per_page' => $posts,
'category_name' => $category,
'post_parent' => $post_parent,
'post_type' => sanitize_text_field($atts['type']),
'order' => sanitize_text_field($atts['order']),
'orderby' => sanitize_text_field($atts['orderby']),
'posts_per_page' => is_numeric($atts['posts']) ? (int) $atts['posts'] : -1,
'category_name' => sanitize_text_field($atts['category']),
'post_parent' => is_numeric($atts['post_parent']) ? (int) $atts['post_parent'] : '',
);

$tax = trim($tax);
$terms = trim($terms);
$tax = trim(sanitize_text_field($atts['tax']));
$terms = trim(sanitize_text_field($atts['terms']));
if ($tax != '' && $terms != '') {
$terms = explode(',', $terms);
$terms = array_map('trim', $terms);
Expand All @@ -81,8 +81,8 @@ function bootscore_swiper_hero_fade($atts) {
));
}

if ($id != '') {
$ids = explode(',', $id);
if ($atts['id'] != '') {
$ids = explode(',', sanitize_text_field($atts['id']));
$ids = array_map('intval', $ids);
$ids = array_filter($ids);
$ids = array_unique($ids);
Expand Down Expand Up @@ -111,7 +111,7 @@ function bootscore_swiper_hero_fade($atts) {

<div class="mt-auto text-white mb-5 text-center">

<?php if ($categories == 'true') : ?>
<?php if ($atts['categories'] == 'true') : ?>
<?php bootscore_category_badge(); ?>
<?php endif; ?>

Expand All @@ -123,7 +123,7 @@ function bootscore_swiper_hero_fade($atts) {
</h2>

<!-- Excerpt & Read more -->
<?php if ($excerpt == 'true') : ?>
<?php if ($atts['excerpt'] == 'true') : ?>
<p class="card-text">
<a class="text-white text-decoration-none" href="<?php the_permalink(); ?>">
<?= strip_tags(get_the_excerpt()); ?>
Expand All @@ -136,7 +136,7 @@ function bootscore_swiper_hero_fade($atts) {
</p>

<!-- Tags -->
<?php if ($tags == 'true') : ?>
<?php if ($atts['tags'] == 'true') : ?>
<?php bootscore_tags(); ?>
<?php endif; ?>

Expand Down
Loading

0 comments on commit 27fd9af

Please sign in to comment.