/**
* ===== Additional custom shortcodes and product utilities (kept intact) =====
*/
// Display product stock quantity
function product_stock_quantity_shortcode($atts) {
if (!class_exists('WooCommerce')) {
return 'WooCommerce not active';
}
$atts = shortcode_atts( array('id' => null), $atts, 'product_stock_quantity' );
if (is_null($atts['id']) && is_product()) {
global $product;
if (!$product) {
return 'No product found';
}
$product_id = $product->get_id();
} else {
$product_id = intval($atts['id']);
}
$product = wc_get_product($product_id);
if (!$product) {
return 'Product not found';
}
$stock_quantity = $product->get_stock_quantity();
if ($stock_quantity > 10) {
$message = '<span class="stock-label" style="font-size: 15px;">庫存</span><span class="greater-than-symbol" style="font-size: 15px;">></span><span style="color: #FD010C; font-weight: 800; font-size: 15px;">10</span>';
} elseif ($stock_quantity > 0) {
$message = '<span class="stock-label" style="font-size: 15px;">庫存僅剩 </span><span style="color: #fd010c; font-weight: 600; font-size: 15px;">' . esc_html($stock_quantity) . '</span>';
} else {
$message = '<span class="out-of-stock" style="font-size: 15px;">商品缺貨中</span>';
}
return '<div class="stock-quantity" style="font-size: 16px;">' . $message . '</div>';
}
add_shortcode('product_stock_quantity', 'product_stock_quantity_shortcode');
// Auto-update prices based on retail price
function update_prices_based_on_retail_price($post_id) {
if (get_post_type($post_id) !== 'product') {
return;
}
$retail_price_ntd = get_field('retail_price_ntd', $post_id);
if (!empty($retail_price_ntd)) {
$regular_price_rate = 0.18;
$market_price_rate = 0.2;
$regular_price = $retail_price_ntd * $regular_price_rate;
$market_price_myr = $retail_price_ntd * $market_price_rate;
update_post_meta($post_id, '_regular_price', $regular_price);
update_post_meta($post_id, '_price', $regular_price);
$market_price_myr_with_rm = 'RM' . number_format($market_price_myr, 2);
update_field('market_price_myr', $market_price_myr_with_rm, $post_id);
}
}
add_action('save_post', 'update_prices_based_on_retail_price');
add_filter('acf/update_value/name=outofprint_market_price_myr', 'complete_market_price_myr', 10, 3);
function complete_market_price_myr($value, $post_id, $field) {
$conversion_rate = 0.2;
if (is_numeric($value)) {
$myr_value = $value * $conversion_rate;
$value = $value . "台幣 / 大約RM" . number_format($myr_value, 2);
}
return $value;
}
add_filter('acf/update_value/name=retail_price_ntd', 'append_yuan_symbol', 10, 3);
function append_yuan_symbol($value, $post_id, $field) {
if (is_numeric($value)) {
$value = $value . '元';
}
return $value;
}
function display_ebook_status_shortcode($atts) {
$ebook_status = get_field('ebook_status');
if ($ebook_status === '有电子书') {
return '<span style="color: #da3800;">(2)</span>';
} elseif ($ebook_status === '沒有電子書') {
return '<span style="color: #da3800;">(1)</span>';
}
return '';
}
add_shortcode('ebook_status', 'display_ebook_status_shortcode');
add_action('save_post', function ($post_id) {
if (get_post_type($post_id) !== 'product') {
return;
}
$current_episode = get_field('current_episode', $post_id);
if (!$current_episode) {
return;
}
$series_terms = get_the_terms($post_id, 'series');
if (!empty($series_terms) && !is_wp_error($series_terms)) {
foreach ($series_terms as $term) {
$existing_episode = (int) get_field('current_episode', 'series_' . $term->term_id);
if ($current_episode > $existing_episode) {
update_field('current_episode', $current_episode, 'series_' . $term->term_id);
}
}
}
});
// Series with episode and stock count
function display_series_with_current_episode_and_stock_shortcode() {
$post_id = get_the_ID();
$series_terms = get_the_terms($post_id, 'series');
if (!empty($series_terms) && !is_wp_error($series_terms)) {
$output = '';
foreach ($series_terms as $term) {
$current_episode = get_field('current_episode', 'series_' . $term->term_id);
$query_args = [
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => [[
'taxonomy' => 'series',
'field' => 'term_id',
'terms' => $term->term_id,
]],
'meta_query' => [[
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
]],
];
$products_query = new WP_Query($query_args);
$in_stock_count = $products_query->found_posts;
wp_reset_postdata();
$series_link = get_term_link($term);
if (!is_wp_error($series_link)) {
$output .= '<p style="color: #F28900; font-size: 14px; margin: 0;">';
$output .= '<a href="' . esc_url($series_link) . '" style="color: #F28900; text-decoration: none;">';
$output .= esc_html($term->name) . ' - 本系列共' . esc_html($current_episode) . '集,可购买商品种类为' . esc_html($in_stock_count) . '样';
$output .= '</a>。</p>';
}
}
return $output;
}
return '<p style="color: #F28900; font-size: 13px; margin: 0;">未找到本商品的系列信息.</p>';
}
add_shortcode('series_with_current_episode_and_stock', 'display_series_with_current_episode_and_stock_shortcode');
// Simpler series stock count
function zipbag_series_display_shortcode() {
$post_id = get_the_ID();
$series_terms = get_the_terms($post_id, 'series');
if (!empty($series_terms) && !is_wp_error($series_terms)) {
$output = '';
foreach ($series_terms as $term) {
$query_args = [
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => -1,
'tax_query' => [[
'taxonomy' => 'series',
'field' => 'term_id',
'terms' => $term->term_id,
]],
'meta_query' => [[
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
]],
];
$products_query = new WP_Query($query_args);
$in_stock_count = $products_query->found_posts;
wp_reset_postdata();
$series_link = get_term_link($term);
if (!is_wp_error($series_link)) {
$output .= '<p style="color: #F28900; font-size: 14px; margin: 0;">';
$output .= '<a href="' . esc_url($series_link) . '" style="color: #F28900; text-decoration: none;">';
$output .= esc_html($term->name) . ' - 可购买商品种类为 ' . esc_html($in_stock_count) . ' 样';
$output .= '</a>。</p>';
}
}
return $output;
}
return '<p style="color: #F28900; font-size: 13px; margin: 0;">未找到本商品的系列信息。</p>';
}
add_shortcode('zipbag_series_display', 'zipbag_series_display_shortcode');
// Availability text customization
function custom_woocommerce_get_availability_text($availability, $product) {
$stock_quantity = $product->get_stock_quantity();
if ($stock_quantity > 10) {
return '庫存>10';
} elseif ($stock_quantity >= 1 && $stock_quantity <= 9) {
return '庫存僅剩 ' . $stock_quantity;
} else {
return '商品缺貨中';
}
}
add_filter('woocommerce_get_availability_text', 'custom_woocommerce_get_availability_text', 10, 2);
// Book specifications shortcode
function display_book_specifications() {
$binding_type = get_field('binding_type');
$number_of_pages = get_field('number_of_pages');
$printing_size = get_field('printing_size');
$content_rate = get_field('content_rate');
$printing_colour = get_field('printing_colour');
$edition = get_field('edition');
$specifications = array();
if (!empty($binding_type)) $specifications[] = $binding_type;
if (!empty($number_of_pages)) $specifications[] = $number_of_pages;
if (!empty($printing_size)) $specifications[] = $printing_size;
if (!empty($content_rate)) $specifications[] = $content_rate;
if (!empty($printing_colour)) $specifications[] = $printing_colour;
if (!empty($edition)) $specifications[] = $edition;
$output = "規格:" . implode(" / ", $specifications);
return $output;
}
add_shortcode('book_specifications', 'display_book_specifications');
// Force qty 1 on product page
add_filter('woocommerce_quantity_input_min', 'hide_woocommerce_quantity_input', 10, 2);
add_filter('woocommerce_quantity_input_max', 'hide_woocommerce_quantity_input', 10, 2);
function hide_woocommerce_quantity_input($quantity, $product) {
if (!is_product()) {
return $quantity;
}
return 1;
}
currently, for my Regular price, it is set through key in number in 定價, then 定價 * 0.2 = 市場價格
, and regular price is set by multiplying 市場價格 with 0.9. Howeverm currently the outcome will be in 3 decimal place. I want to limit it to only 2 decimal places and round up the 3rd decimal places. Is it possible to set it? because now even tho the displayed price is 2 digit, but it charges customer the price of 3 digit. The difference is obvious when theres high in quantity.
最新发布