自定义“添加到购物车”按钮,在WooCommerce
特定的产品类别这是我的代码:自定义“添加到购物车”按钮,在WooCommerce
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;
}
我需要的功能,以取代只是一个特定的“添加到购物车”按钮,URL和文本产品分类。
此按钮将触发带有联系表单的灯箱,此按钮的文本将为:请求报价。
如何使其按预期工作?
这是它的实际工作原理this link。
更新时间: 2不同的产品类别(2个不同的按钮)
为您的产品的全球和完整的解决方案从“套”产品类别:
- 如果你的产品之一( '衬衫'产品类别)不是可变产品,您首先需要通过链接到产品的简单按钮替换商店和存档页面中的“加载到购物车”按钮。
- 在单件产品页面中,您需要删除按钮和数量字段,以便将其替换为您的自定义按钮。
这里是代码:
// 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;
}
代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。
此代码已通过测试,适用于所有产品类型(简单,可变...)。您将获得(例如):
谢谢你SOO多!如果我需要为2个不同类别做什么?我的意思是我需要为一个类别调用一个弹出窗口,并且为第二个类别弹出不同的弹出窗口?我只需要克隆下面的代码并将'liners'更改为'custom-samplers'? –
谢谢!但它会弹出相同的弹出窗口。如何制作 “echo”';“对于“cat2”有不同的价值? –
array('liners','custom-classics') –
谢谢你洙多! –