【问题标题】:Change 'add to cart' text when product is sold out产品售罄时更改“添加到购物车”文本
【发布时间】:2020-03-01 16:55:49
【问题描述】:

大家我想在产品售罄时将“in de winkelmand”按钮更改为售罄。

我已尝试禁用如下所示的产品单品,以摆脱那个奇怪的盒子。

但现在需要更改文字

.product .single_variation {
    display: none !important;
}

nm-variable-add-to-cart-button single_add_to_cart_button button alt disabled wc-variation-is-unavailable

很遗憾没有结果,谁能指出我正确的方向?

https://www.peachandhoney.nl/product/bella-rose-top/

是指向该站点的链接。此产品 Xs 已售罄,其余有货。

谢谢。

【问题讨论】:

  • 避免“我想要,有人可以帮我做吗?”问题。包括代码并展示你到目前为止所做的尝试。考虑阅读本指南,了解如何提出好问题:stackoverflow.com/help/how-to-ask。省力的问题将被投反对票,并最终由主持人关闭。
  • 只是确认一下,当相应产品缺货时,您希望每个产品都售罄。对吗?

标签: php jquery wordpress woocommerce


【解决方案1】:

如果我理解您的问题,您想更改文字吗? add to cart 文本是可过滤的。使用过滤器woocommerce_product_single_add_to_cart_text。只要您允许延期交货,就会显示该按钮。

add_filter( 'woocommerce_product_single_add_to_cart_text', 'dd_custom_single_add_to_cart_text' ); 
function dd_custom_single_add_to_cart_text(){
    global $product;
    if ($product->get_stock_status() !== 'instock'){
        return __( 'Sold Out', 'woocommerce' );
    } else {
        return __( 'Add to cart', 'woocommerce' );
    }
}

【讨论】:

    【解决方案2】:

    想用另一个文本更改“已售罄”文本,那么这将解决您使用 WooCommerce Way 的问题。请将此代码添加到您的主题functions.php。但请不要忘记使用子主题

    add_filter( 'woocommerce_get_availability_text', 'amc_change_out_of_stock_text', 10, 2 );
    
     function amc_change_out_of_stock_text( $availability, $product ){
    
        if ( ! $product->is_in_stock() ) {
    
            $availability = __( 'in de winkelmand', 'woocommerce' );
    
        }
    
        return $availability;
    
     }
    

    【讨论】:

    • 当添加这个没有任何反应?知道为什么吗?
    【解决方案3】:

    这应该可以正常工作。经过测试和确认。

    function change_loop_add_to_cart_button( $button, $product, $args = array() ) {
        if( !$product->is_in_stock() ){
            $button = '<a class="button disabled" style="cursor:not-allowed;color:#777;background-color:#aaa;">'.__('Sold Out', 'woocommerce').'</a>';
        }
       return $button;
    }
    add_filter( 'woocommerce_loop_add_to_cart_link', 'change_loop_add_to_cart_button', 20, 3 );
    

    【讨论】:

    • 我在实施时收到此错误 由于文件 wp-content/themes/savoy/functions.php 的第 1059 行出现错误,您的 PHP 代码更改已回滚。请修复并再次尝试保存。无法重新声明 change_loop_add_to_cart_button()(之前在 wp-content/themes/savoy/functions.php:972 中声明)
    • @QuintyvanDijk 我的答案change_loop_add_to_cart_button 中的函数名称已在您的functions.php 中使用。因此,将上面代码中名称 change_loop_add_to_cart_button 的每次出现都更改为其他名称,例如 change_loop_add_to_cart_button_new
    • 谢谢你这样做了,现在我可以输入代码了,但是什么也没发生,知道为什么吗?
    • @QuintyvanDijk 您添加代码的主题可能未激活。或者函数定义和钩子回调中的函数名称可能不同。或者产品可能没有缺货。不检查就无法找出确切的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多