我想要做的是更改标准 WooCommerce Sold Out 消息,并在其后添加一些内容,仅适用于两种类型的产品。
我决定尝试使用 JavaScript 来实现这一点,方法是更改 p 元素中出现的文本。我首先执行一个窗口 url 检查,看看我们是否在正确的页面上,如果是,则确保文本不仅仅显示“UITVERKOCHT”,而是显示“UITVERKOCHT(neem contact op via contactformulier, levertijd 2 werkdagen) ”。
UITVERKOCHT 显示产品何时售完。但仅仅对于这两个产品,我想在 UITVERKOCHT 后面添加一条额外的消息。这就是我到目前为止所尝试的。
if(window.location.href.indexOf("prikkabel-50-meter") > -1 || window.location.href.indexOf("prikkabel-100-meter") > -1){
jQuery( "#product-5666 > div.single-product-wrapper > div.summary.entry-summary > form > div > div.woocommerce-variation.single_variation > div.woocommerce-variation-availability > p" ).change(function() {
console.log('a change occured');
});
}
知道我还能尝试什么吗?
最佳答案
您可以尝试woocommerce_get_availability
过滤器:
在您的主题functions.php
中:
add_filter( 'woocommerce_get_availability', 'custom_availability', 5, 2 );
function custom_availability( $availability, $_product ) {
// If the product is out of stock and the product id = 5666
if ( ! $_product->is_in_stock() && $_product->id === 5666 ) {
$availability['availability'] = __( '[your custom text here]', 'woocommerce' );
}
return $availability;
}
如果产品缺货并且产品 ID 为5666,这将应用上述过滤器。
关于javascript - 更改 WooCommerce 中某些产品的 "Sold out",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44181734/