![](/img/trans.png)
[英]Disable add to cart button on Woocommerce product category archive pages
[英]Change product button based on product category in Woocommerce archive pages
我有一个 jQuery 脚本,它成功地从本地存储中检索数据,然后返回到一个 PHP 函数(在子主题内)。 在此功能中,我有一个 if/else 块,我想使用它来更改打开时所有商店页面和链接产品页面上的所有按钮。 取决于从 jQuery 脚本获得的结果。
有 3 个选项:默认“检查覆盖范围”、标准“添加到购物车”和最后“注册您的兴趣”
我有 3 个类别,想排除“硬件类别”
如何将过滤器应用于与标签匹配的所有产品页面? 下面的代码将此应用于商店中的所有商店页面项目,但不适用于当前客户的链接产品页面,而不会覆盖其他客户的视图。
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
$button_text = __("Check Coverage", "woocommerce");
$button = '<a class="button" href="******/coverage">' . $button_text . '</a>';
return $button;
}
任何指导,批评或想法表示赞赏。
编辑- 我使用的 Ajax 代码:
从子主题中的functions.php 排队的jQuery 脚本:
jQuery.ajax({
type: "POST",
url: "/wp-admin/admin-ajax.php",
data: {
action: 'dynamic_product_links',
// add your parameters here
cat1: localStorage.getItem("cat1"),
cat2: localStorage.getItem("cat2"),
},
success: function (output) {
console.log(output);
}
});
函数.php
add_action('wp_enqueue_scripts', 'woocommerce_ajax_check_coverage_js', 99);
function woocommerce_ajax_check_coverage_js(){
//echo "<script type='text/javascript'>alert('called woocommerce_ajax_check_coverage_js ');</script>";
wp_register_script( 'woocommerce-ajax-check-coverage-js', get_stylesheet_directory_uri() . "/assets/ajax-check-coverage.js", array('jquery'), null, true );
wp_enqueue_script('woocommerce-ajax-check-coverage-js');
}
// register the ajax action for authenticated users
add_action('wp_ajax_dynamic_product_links', 'dynamic_product_links');
// register the ajax action for unauthenticated users
add_action('wp_ajax_nopriv_dynamic_product_links', 'dynamic_product_links');
function dynamic_product_links() {
// ajax data received from jQuery script which retrieves data from local storage
$cat1_speed = $_REQUEST['cat1']; // can be either 'Check Coverage','No Coverage' or a number linked to a custom atttribute
$cat2_speed = $_REQUEST['cat2'];
if ($cat1_speed == 'check ooverage' || $cat2_speed == 'check coverage') {
// set all matching categories except hardware to check coverage
}
if ($cat1_speed == 'No Coverage' ) {
// remove this catogory item set from the store (hide?) -> change any product/* pages add to cart button to "register your interest"
}
if ($cat1_speed == '20' || $cat2_speed == 'check coverage') {
// remove this catogory item set from the store (hide?) if the products atttribute is greater than the number
}
// in the end, returns success json data
wp_send_json_success([/* some data here */$wireless_speed ,$fusion_fibre_speed]);
// or, on error, return error json data
wp_send_json_error([/* some data here */]);
}
要排除产品类别,请尝试使用以下方法之一:
1)。 直接产品类别处理(无父条款):
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if( ! has_term( array('hardware'), 'product_cat', $product->get_id() ) ) {
$button_text = __("Check Coverage", "woocommerce");
$button = '<a class="button" href="******/coverage">' . $button_text . '</a>';
}
return $button;
}
2)。 产品类别处理(以及父条款):
add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );
function replacing_add_to_cart_button( $button, $product ) {
if( ! has_product_categories( $product->get_id(), array('hardware') ) ) {
$button_text = __("Check Coverage", "woocommerce");
$button = '<a class="button" href="******/coverage">' . $button_text . '</a>';
}
return $button;
}
// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
$parent_term_ids = $categories_ids = array(); // Initializing
$taxonomy = 'product_cat';
$product_id = $product_id == 0 ? get_the_id() : $product_id;
if( is_string( $categories ) ) {
$categories = (array) $categories; // Convert string to array
}
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
$result = (array) term_exists( $category, $taxonomy );
if ( ! empty( $result ) ) {
$categories_ids[] = reset($result);
}
}
// Loop through the current product category terms to get only parent main category term
foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
if( $term->parent > 0 ){
$parent_term_ids[] = $term->parent; // Set the parent product category
$parent_term_ids[] = $term->term_id; // (and the child)
} else {
$parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
}
}
return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}
代码位于活动子主题(或活动主题)的 function.php 文件中。 测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.