[英]Woocommerce: How to get variations stock-status
I have this function to personalize my product page a little.我有这个 function 来个性化我的产品页面。 Inside my child-themes function.php I added this:
在我的儿童主题 function.php 中,我添加了这个:
function change_product_summary_germanized(){
global $product;
if ($product->is_type('variable')) {
$shopmarks = wc_gzd_get_single_product_shopmarks();
foreach ($shopmarks as $shopmark) {
$type = $shopmark->get_type();
switch ($type) {
case 'unit_price':
$shopmark->remove();
add_action('woocommerce_before_single_variation', 'woocommerce_gzd_template_single_price_unit', 20);
add_action('woocommerce_before_single_variation', 'custom_unit_price_title', 19);
break;
case 'legal':
$shopmark->remove();
add_action('woocommerce_before_single_variation', 'woocommerce_gzd_template_single_legal_info', 40);
break;
case 'delivery_time':
$shopmark->remove();
// if ( $product->is_in_stock() ) {
add_action('woocommerce_before_single_variation', 'woocommerce_gzd_template_single_delivery_time_info', 50);
// }
break;
}
}
}
}
add_filter('woocommerce_single_product_summary', 'change_product_summary_germanized');
Works fine but as you can see I commented out an if-clause that I am having trouble with.工作正常,但如您所见,我注释掉了一个我遇到问题的 if 子句。 I tried all different kinds of ways to not add the delivery-time, when the selected variation is out of stock.
当所选变体缺货时,我尝试了各种不同的方法来不添加交货时间。
Examples of what I have tried so far:到目前为止我尝试过的示例:
if( $product->is_in_stock() ) {...}
if( $product->get_stock_quantity() > 0 ) {...}
if( sizeof($product->get_available_variations()) > 0) {...}
...
I am pretty new to this.我对此很陌生。 Anybody can point out to me, what's the way to go here?
任何人都可以向我指出,这里的 go 的方法是什么?
Many may agree that this is terrible work, but I achieved what I wanted on a complicated way.许多人可能同意这是一项糟糕的工作,但我以复杂的方式实现了我想要的。 A few months ago I built a function to get product-prices on my own way.
几个月前,我构建了一个 function 以自行获取产品价格。 It's leaned on something I found online, but I can't find the reference anymore.
它依赖于我在网上找到的东西,但我再也找不到参考了。 Anyway, I added some JQuery to hide/show elements depending on if other elements are visible.
无论如何,我添加了一些 JQuery 来隐藏/显示元素,具体取决于其他元素是否可见。
function custom_wc_template_single_price(){
global $product;
if ($product->is_type('variable')):
$prices = array(
$product->get_variation_price('min', true),
$product->get_variation_price('max', true)
);
$price = $prices[0] !== $prices[1] ? sprintf(__('ab %1$s', 'woocommerce'), wc_price($prices[0])) : wc_price($prices[0]);
$prices = array(
$product->get_variation_regular_price('min', true),
$product->get_variation_regular_price('max', true)
);
sort($prices);
$saleprice = $prices[0] !== $prices[1] ? sprintf(__('ab %1$s', 'woocommerce'), wc_price($prices[0])) : wc_price($prices[0]);
if ($price !== $saleprice && $product->is_on_sale()) {
$price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>';
}
?>
<style>
div.woocommerce-variation-price,
div.woocommerce-variation-availability,
div.hidden-variable-price {
height: 0px !important;
overflow: hidden;
position: relative;
line-height: 0px !important;
font-size: 0% !important;
visibility: hidden !important;
}
p.price {
margin-bottom: 12px;
}
p.availability {
margin-bottom: 4px;
}
</style>
<script>
jQuery(document).ready(function ($) {
setTimeout(function () {
if (0 < $('input.variation_id').val() && null != $('input.variation_id').val()) {
if ($('p.availability'))
$('p.availability').remove();
$('p.price:not(.price-unit)').html($('div.woocommerce-variation-price > span.price').html());
if ($('div.legal-price-info > p.stock').length === 0) {
$('div.legal-price-info').append('<p class="availability">' + $('div.woocommerce-variation-availability').html() + '</p>');
}
}
}, 300);
$('input').blur(function () {
if (0 < $('input.variation_id').val() && null != $('input.variation_id').val()) {
if ($('.price p.availability') || $('.price p.stock'))
$('p.price:not(.price-unit) p').each(function () {
$(this).remove();
});
if ($('div.legal-price-info > p.availability'))
$('div.legal-price-info > p:not(.wc-gzd-additional-info)').remove();
$('p.price:not(.price-unit)').html($('div.woocommerce-variation-price > span.price').html());
$('div.legal-price-info').append('<p class="availability">' + $('div.woocommerce-variation-availability').html() + '</p>');
if ($('div.legal-price-info > p.stock').length !== 0) {
$('p.delivery-time-info').hide();
$('div.woocommerce-variation-add-to-cart').hide();
} else {
$('p.delivery-time-info').show();
$('div.woocommerce-variation-add-to-cart').show();
}
} else {
$('p.price:not(.price-unit)').html($('div.hidden-variable-price').html());
if ($('p.availability'))
$('p.availability').remove();
}
})
});
</script>
<?php
echo '<p class="price">' . $price . '</p>
<div class="hidden-variable-price" >' . $price . '</div>';
endif;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.