javascript - 更改 WooCommerce 中某些产品的 "Sold out"

标签 javascript php jquery wordpress woocommerce

我想要做的是更改标准 WooCommerce Sold Out 消息,并在其后添加一些内容,仅适用于两种类型的产品。

我决定尝试使用 JavaScript 来实现这一点,方法是更改​​ p 元素中出现的文本。我首先执行一个窗口 url 检查,看看我们是否在正确的页面上,如果是,则确保文本不仅仅显示“UITVERKOCHT”,而是显示“UITVERKOCHT(neem contact op via contactformulier, levertijd 2 werkdagen) ”。 enter image description here

UITVERKOCHT 显示产品何时售完。但仅仅对于这两个产品,我想在 UITVERKOCHT 后面添加一条额外的消息。这就是我到目前为止所尝试的。

enter image description here

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/

相关文章:

javascript - 使用对象的方法设置对象的属性值

javascript - 如何获取由 chrome.windows.create() 创建的弹出窗口的窗口 ID

php - 从表中删除项目 - Laravel

javascript - 使用 AJAX 进行表单验证

php - memcached 键可以包含空格吗?

javascript - 为什么这个 animate() jquery 函数脚本不起作用?

javascript - 使用 Leaflet.Path.Transform 在 Mapbox 中旋转多边形

javascript - 谷歌地球引擎: How can I perform image calculations with random values pixel by pixel?

jquery - 如何使用 jQuery 绑定(bind)附加的 html?

javascript - 使用纯 jQuery 自定义文件 uploader