Customize quot;Add to cartquot; button for a specific product category in WooCommerce(自定义“加入购物车WooCommerce 中特定产品类别的按钮)
问题描述
这是我的代码:
add_filter('woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text');//2.1 +功能 woo_custom_cart_button_text( $text ) {if( has_term( 'liners', 'product_cat' ) ){$text = __( ' ', '你的插件' );echo do_shortcode('<a href="#" class="popmake-923">Request a Quote</a>');}返回 $text;}
我需要制作功能来替换一个特定产品类别的添加到购物车"按钮网址和文本.
此按钮将触发带有联系表单的灯箱,此按钮的文本将是:请求报价.
我怎样才能让它按预期工作?
以下是
<小时>This is my code:
add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_custom_cart_button_text' ); // 2.1 +
function woo_custom_cart_button_text( $text ) {
if( has_term( 'liners', 'product_cat' ) ){
$text = __( ' ', 'your-plugin' );
echo do_shortcode('<a href="#" class="popmake-923">Request а Quote</a>');
}
return $text;
}
I need to make function to replace the "Add to cart" button Url and Text for just one specific product category.
This button will trigger a Lightbox with a contact form and the text for this button will be: Request a Quote.
How can I make it work as expected?
Here is how it works actually on this link.
Updated: for 2 different product categories (2 different buttons)
A global and complete solution for your products from 'liners' product category:
- If one of your products (in 'liners' product category) is not a variable product, you need first to replace the add-to-cart button in shop and archives pages by a simple button linked to the product.
- In single product pages you need to remove add-to-cart button and quantities fields, to replace it by your custom button.
Here is that code:
// Replacing the button add to cart by a link to the product in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_replacing_add_to_cart_button', 10, 2 );
function conditionally_replacing_add_to_cart_button( $button, $product ) {
$categories = array('liners','custom-classics');
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// For 'liners' product category
if( has_term( $categories, 'product_cat', $product_id ) ){
$button_text = __("View product", "woocommerce");
$button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
}
return $button;
}
// replacing add to cart button and quantities by your custom button in Single product pages
add_action( 'woocommerce_single_product_summary', 'conditionally_replacing_template_single_add_to_cart', 1, 0 );
function conditionally_replacing_template_single_add_to_cart() {
global $product;
$categories = array('liners','custom-classics');
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
function custom_button_replacement(){
global $product;
$categories = array('liners','custom-classics');
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
if( has_term( $categories[0], 'product_cat', $product_id ) )
$class_id = "923"; // liners
elseif( has_term( $categories[1], 'product_cat', $product_id ) )
$class_id = "925"; // custom-classics
else $class_id = ""; // none
// set below your custom text
$button_text = __('Request а Quote', 'woocommerce');
// Output your custom text
echo '<a href="#" class="popmake-'.$class_id.' button">'.$button_text.'</a>';
}
// Only for 'liners' and 'custom-classics' product categories
if( has_term( $categories, 'product_cat', $product_id ) ):
// For variable product types
if( $product->is_type( 'variable' ) ){
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
// The button replacement
add_action( 'woocommerce_single_variation', 'custom_button_replacement', 20 );
}
else // For all other product types
{
// Removing add to cart button and quantities
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
// The button replacement
add_action( 'woocommerce_single_product_summary', 'custom_button_replacement', 30 );
}
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
This code is tested and works for all product types (simple, variable…). You will get (example):
这篇关于自定义“加入购物车"WooCommerce 中特定产品类别的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:自定义“加入购物车"WooCommerce 中特定产品类
基础教程推荐
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01