Skip to content

Commit

Permalink
14.4.1
Browse files Browse the repository at this point in the history
- [WC] Refactor the WooCommerce template
- [WC] Changed mobile Add-to-cart button to always show instead of being hidden by toggling.
  • Loading branch information
hrsetyono committed Nov 28, 2022
1 parent 9615be8 commit 4e6b2c0
Show file tree
Hide file tree
Showing 26 changed files with 313 additions and 323 deletions.
2 changes: 1 addition & 1 deletion _dist/app.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-polyfill'), 'version' => '6d476549372716f5015e7a506797b6e8');
<?php return array('dependencies' => array('wp-polyfill'), 'version' => 'b3a8743c9b023dff76b9420841c5eee1');
2 changes: 1 addition & 1 deletion _dist/app.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion _dist/shop-admin.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-polyfill'), 'version' => '41aca7e59a974c5118a9e0e1152d235d');
<?php return array('dependencies' => array('wp-polyfill'), 'version' => '429f50163cc30dac7196ed46d3e00679');
2 changes: 1 addition & 1 deletion _dist/shop-admin.css
Original file line number Diff line number Diff line change
@@ -1 +1 @@
#adminmenu .toplevel_page_getwooplugins{display:none}
#adminmenu .toplevel_page_getwooplugins{display:none}.woo_variation_swatches_tab{display:none !important}
2 changes: 1 addition & 1 deletion _dist/shop.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-polyfill'), 'version' => '6c7fe173069f34ed1a8156c07e51f20a');
<?php return array('dependencies' => array('wp-polyfill'), 'version' => 'bd46e5b1cda8a6ce66ab214fa8094dab');
2 changes: 1 addition & 1 deletion _dist/shop.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion _dist/shop.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions assets/css/components/_widgets.sass
Original file line number Diff line number Diff line change
Expand Up @@ -359,12 +359,16 @@ input.search-submit
svg
width: 1rem
height: 1rem
margin-right: 0.25rem

span
margin-right: 0.25rem
line-height: 1
text-transform: uppercase
font-size: var(--sFontSize)

@media ($below-xs)
display: none

path,
circle
fill: var(--toggleColor)
Expand All @@ -374,10 +378,6 @@ input.search-submit
.widget-column:last-child:not(:first-child) &
flex-direction: row-reverse

svg
margin-right: 0
margin-left: 0.25rem

// WIDGET: GALLERY
.widget_media_gallery
Expand Down
6 changes: 2 additions & 4 deletions functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
require_once $inc . '/frontend.php';
}


if (class_exists('WooCommerce')) {
$wc_dir = __DIR__ . '/woocommerce';
require_once $wc_dir . '/shop-setup.php';
require_once $wc_dir . '/shop-frontend.php';
require_once $wc_dir . '/shop-admin.php';
require_once __DIR__ . '/woocommerce/_functions.php';
}


Expand Down
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Theme Name: Edje Boilerplate Theme
Author: Pixel Studio
Author URI: https://pixelstudio.id
Description: Boilerplate WordPress theme to be used alongside Edje WP Library
Version: 14.4.0
Version: 14.4.1
Tags: private theme, edje, acf, woocommerce, custom-menu, editor-style
*/

Expand Down
120 changes: 5 additions & 115 deletions woocommerce.php
Original file line number Diff line number Diff line change
@@ -1,123 +1,13 @@
<?php
global $woocommerce;
$wc_dir = __DIR__ . '/woocommerce';

$template = '';
$args = [
'woo' => $woocommerce,
'catalog_columns' => get_option('woocommerce_catalog_columns'),
];

// SINGLE PRODUCT
if (is_singular('product')) {
global $post;
$template = 'single-product';
$related_ids = wc_get_related_products($post->ID, $args['catalog_columns']);

$args = array_merge($args, [
'post' => $post,
'thumbnail_url' => get_the_post_thumbnail_url($post, 'thumbnail'),
'product' => wc_get_product($post->ID),
'products' => get_posts([
'post_type' => 'product',
'post__in' => $related_ids,
'orderby' => 'post__in',
]),
'extra_classes' => 'is-style-my-slider',
]);
require_once "{$wc_dir}/single-product.php";
}
// If SHOP page
elseif (is_shop()) {
$template = 'shop';
$post = get_post(get_option('woocommerce_shop_page_id'));

$args = array_merge($args, [
'post' => $post,
'products' => $wp_query->get_posts(),
]);
}
// if CATEGORY page
elseif(is_product_category() || is_product_tag()) {
global $wp_query;
$template = 'archive-product';

$display_mode = woocommerce_get_loop_display_mode();
$term = get_queried_object();

$args['title'] = $term->name;
$args['content'] = wpautop($term->description);

// if display products
if ($display_mode === 'both' || $display_mode === 'products') {
$args['products'] = $wp_query->get_posts();
}

// if display categories
if ($display_mode === 'both' || $display_mode === 'subcategories') {
$args['categories'] = _get_categories($term->term_id);
}

// disable pagination
if ($display_mode === 'subcategories') {
wc_set_loop_prop('total', 0);
}
}

get_header();
get_template_part("woocommerce/views/$template", '', $args);
get_footer();


//


/**
* Get WC_Product data from posts and embed it
*
* @param $posts
* @return - Posts with embedded Product data
*/
function _get_products(array $posts) : array {
$post_ids = array_reduce($posts, function($result, $p) {
$result[] = $p->id;
return $result;
}, []);

$products = wc_get_products([
'include' => $post_ids,
'orderby' => 'post__in',
'posts_per_page' => wc_get_loop_prop('total')
]);

$posts = array_map(function($p, $index) use ($products) {
$p->product = $products[$index];
return $p;
}, $posts, array_keys($posts));

return $posts;
require_once "{$wc_dir}/shop.php";
}


/**
* Get categories / subcategories with attached Thumbnail image and Permalink
*
* @param $parent_id
* @return - The formatted categories
*/
function _get_categories(int $term_id = 0) : array {
$raw_cats = woocommerce_get_product_subcategories($term_id);

// get extra data for category
$parsed_cats = array_map(function($c) {
// get thumbnail image
$thumb_id = get_term_meta($c->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_image_src($thumb_id, 'medium');
$c->image = $image ? $image[0] : wc_placeholder_img_src();

// get permalink
$c->link = get_term_link($c->term_id, 'product_cat');

return $c;
}, $raw_cats);

return $parsed_cats;
elseif (is_product_category() || is_product_tag()) {
require_once "{$wc_dir}/archive-product.php";
}
87 changes: 56 additions & 31 deletions woocommerce/shop-setup.php → woocommerce/_functions.php
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
<?php

add_action('init', 'my_shop_init');
add_action('after_setup_theme', 'my_shop_support');
add_action('after_setup_theme', 'my_shop_theme_supports');
add_action('wp_enqueue_scripts', 'my_frontend_shop_assets', 101);
add_action('admin_enqueue_scripts', 'my_admin_shop_assets', 100);
add_action('enqueue_block_editor_assets', 'my_editor_shop_assets', 100);
my_shop_block_styles();

// disable built-in WooCommerce CSS
add_filter('woocommerce_enqueue_styles', '__return_empty_array');

my_shop_block_styles();

if (is_admin()) {
add_action('add_meta_boxes' , 'my_modify_shop_excerpt_metabox', 40);
}

/**
* Set global $product object
*/
function my_setup_productdata($post) {
global $product;
$product = isset($post->product)
? $post->product
: wc_get_product($post->ID);
if (is_cart()) {
// Move the Cross-sell out of the CART TOTAL div
remove_action('woocommerce_cart_collaterals', 'woocommerce_cross_sell_display');
add_action('woocommerce_after_cart', 'my_custom_cross_sell_display');
}

/////

/**
* Remove image sizes
* @action init
*/
function my_shop_init() {
// remove all woocommerce image sizes
Expand All @@ -41,6 +40,24 @@ function my_shop_init() {
}
}

/**
* Setup theme_support for WooCommerce
*
* @action after_setup_theme
*/
function my_shop_theme_supports() {
add_theme_support('woocommerce', [
'product_grid' => [
'default_columns' => 4
],
'single_image_width' => 480,
]);

add_theme_support('wc-product-gallery-zoom');
add_theme_support('wc-product-gallery-lightbox');
add_theme_support('wc-product-gallery-slider');
}


/**
* Register Woocommerce assets here
Expand Down Expand Up @@ -90,24 +107,6 @@ function my_editor_shop_assets() {
wp_deregister_style('wc-blocks-editor');
}


/**
* Run after theme is loaded
* @action after_setup_theme
*/
function my_shop_support() {
add_theme_support('woocommerce', [
'product_grid' => [
'default_columns' => 4
],
'single_image_width' => 480,
]);

add_theme_support('wc-product-gallery-zoom');
add_theme_support('wc-product-gallery-lightbox');
add_theme_support('wc-product-gallery-slider');
}

/**
* Custom Styles for WooCommerce's blocks
*/
Expand All @@ -127,5 +126,31 @@ function my_shop_block_styles() {
foreach ($slider_blocks as $b) {
register_block_style($b, ['name' => 'my-slider', 'label' => 'Slider']);
}
}

/**
* Override the WooCommerce excerpt box with plain textarea
*
* @action add_meta_boxes 40
*/
function my_modify_shop_excerpt_metabox() {
remove_meta_box('postexcerpt', 'product', 'normal');
add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', 'product', 'side');
}


/**
* Change the markup of Cart's Cross-Sell
* @action woocommerce_cart_collaterals
*/
function my_custom_cross_sell_display() {
$args = [
'products' => get_posts([
'post_type' => 'product',
'post__in' => WC()->cart->get_cross_sells(),
'posts_per_page' => 4,
]),
];

get_template_part('woocommerce/views/_cart-cross-sells', '', $args);
}
57 changes: 57 additions & 0 deletions woocommerce/archive-product.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
global $wp_query;

$term = get_queried_object();
$display_mode = woocommerce_get_loop_display_mode();

$args = [
'catalog_columns' => get_option('woocommerce_catalog_columns'),
'title' => $term->name,
'content' => wpautop($term->description),
'pagination' => H::get_pagination(),
];

// if display products
if ($display_mode === 'both' || $display_mode === 'products') {
$args['products'] = $wp_query->get_posts();
}

// if display categories
if ($display_mode === 'both' || $display_mode === 'subcategories') {
$args['categories'] = _get_categories($term->term_id);
}

// disable pagination
if ($display_mode === 'subcategories') {
wc_set_loop_prop('total', 0);
}

get_header();
get_template_part('woocommerce/views/archive-product', '', $args);
get_footer();


/**
* Get categories / subcategories with attached Thumbnail image and Permalink
*
* @param $parent_id
* @return - The formatted categories
*/
function _get_categories(int $term_id = 0) : array {
$raw_cats = woocommerce_get_product_subcategories($term_id);

// get extra data for category
$parsed_cats = array_map(function($c) {
// get thumbnail image
$thumb_id = get_term_meta($c->term_id, 'thumbnail_id', true);
$image = wp_get_attachment_image_src($thumb_id, 'medium');
$c->image = $image ? $image[0] : wc_placeholder_img_src();

// get permalink
$c->link = get_term_link($c->term_id, 'product_cat');

return $c;
}, $raw_cats);

return $parsed_cats;
}
9 changes: 2 additions & 7 deletions woocommerce/css/_shop-gutenberg-custom.sass
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,8 @@
margin-top: calc(-1.5 * var(--blockGap))

.wc-block-grid__products
row-gap: 0
column-gap: 0

// gap is bugged, has to use padding
li
padding-right: 0.5rem
padding-left: 0.5rem
row-gap: 1rem
column-gap: 1rem

.swiper-pagination
display: flex
Expand Down
Loading

0 comments on commit 4e6b2c0

Please sign in to comment.