重命名 WooCommerce 3 中缺货产品的“添加到购物车”按钮
Rename Add to Cart buttons for Out Of Stock Products in WooCommerce 3
我想在产品 售出后将 'Add to Cart'
重命名为 'Sold Out'
在 WooCommerce 中输出。
这是我的代码:
add_filter( 'add_to_cart_text', 'woo_custom_single_add_to_cart_text' );
add_filter( 'woocommerce_product_single_add_to_cart_text',
'woo_custom_single_add_to_cart_text' ); // 2.1 +
function woo_custom_single_add_to_cart_text() {
if( $product->get_stock_quantity() == 0 )
return __( 'Sold Out', 'woocommerce' );
} else {
return __( 'Add to cart ', 'woocommerce' );
}
但是没用
请问我做错了什么?
您似乎忘记将 $product
声明为 global
。
您的过滤函数应以 global $product;
开头
更新(现在使用可变产品和变体)
您的代码中存在一些错误:
1.You 需要在你的函数中设置 钩子参数 作为 $button_text
和 $product
。
2.The add_to_cart_text
已弃用并替换为 woocommerce_product_add_to_cart_text
.
在此更新的代码中,我添加了一个 jQuery 脚本,该脚本仅捕获可变产品的选定变体(在单个产品页面上)并更改按钮文本。现在这适用于所有情况。
add_filter( 'woocommerce_product_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
function customizing_add_to_cart_button_text( $button_text, $product ) {
$sold_out = __( "Sold Out", "woocommerce" );
$availability = $product->get_availability();
$stock_status = $availability['class'];
// Only for variable products on single product pages
if ( $product->is_type('variable') && is_product() )
{
?>
<script>
jQuery(document).ready(function($) {
$('select').blur( function(){
if( '' != $('input.variation_id').val() && $('p.stock').hasClass('out-of-stock') )
$('button.single_add_to_cart_button').html('<?php echo $sold_out; ?>');
else
$('button.single_add_to_cart_button').html('<?php echo $button_text; ?>');
console.log($('input.variation_id').val());
});
});
</script>
<?php
}
// For all other cases (not a variable product on single product pages)
elseif ( ! $product->is_type('variable') && ! is_product() )
{
if($stock_status == 'out-of-stock')
$button_text = $sold_out.' ('.$stock_status.')';
else
$button_text.=' ('.$stock_status.')';
}
return $button_text;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效
我想在产品 售出后将 'Add to Cart'
重命名为 'Sold Out'
在 WooCommerce 中输出。
这是我的代码:
add_filter( 'add_to_cart_text', 'woo_custom_single_add_to_cart_text' );
add_filter( 'woocommerce_product_single_add_to_cart_text',
'woo_custom_single_add_to_cart_text' ); // 2.1 +
function woo_custom_single_add_to_cart_text() {
if( $product->get_stock_quantity() == 0 )
return __( 'Sold Out', 'woocommerce' );
} else {
return __( 'Add to cart ', 'woocommerce' );
}
但是没用
请问我做错了什么?
您似乎忘记将 $product
声明为 global
。
您的过滤函数应以 global $product;
更新(现在使用可变产品和变体)
您的代码中存在一些错误:
1.You 需要在你的函数中设置 钩子参数 作为 $button_text
和 $product
。
2.The add_to_cart_text
已弃用并替换为 woocommerce_product_add_to_cart_text
.
在此更新的代码中,我添加了一个 jQuery 脚本,该脚本仅捕获可变产品的选定变体(在单个产品页面上)并更改按钮文本。现在这适用于所有情况。
add_filter( 'woocommerce_product_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
function customizing_add_to_cart_button_text( $button_text, $product ) {
$sold_out = __( "Sold Out", "woocommerce" );
$availability = $product->get_availability();
$stock_status = $availability['class'];
// Only for variable products on single product pages
if ( $product->is_type('variable') && is_product() )
{
?>
<script>
jQuery(document).ready(function($) {
$('select').blur( function(){
if( '' != $('input.variation_id').val() && $('p.stock').hasClass('out-of-stock') )
$('button.single_add_to_cart_button').html('<?php echo $sold_out; ?>');
else
$('button.single_add_to_cart_button').html('<?php echo $button_text; ?>');
console.log($('input.variation_id').val());
});
});
</script>
<?php
}
// For all other cases (not a variable product on single product pages)
elseif ( ! $product->is_type('variable') && ! is_product() )
{
if($stock_status == 'out-of-stock')
$button_text = $sold_out.' ('.$stock_status.')';
else
$button_text.=' ('.$stock_status.')';
}
return $button_text;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
此代码已经过测试并且有效