我有一个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);
}
});
functions.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 */]);
}
答案 0 :(得分:1)
要排除产品类别,请尝试使用以下之一:
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 checks for parent product categories
function has_product_categories( $product_id, $categories ) {
// Initializing
$parent_term_ids = $categories_ids = array();
$taxonomy = 'product_cat';
// Convert categories term names and slugs to categories term ids
foreach ( $categories as $category ){
if( is_numeric( $category ) ) {
$categories_ids[] = (int) $category;
} elseif ( term_exists( sanitize_title( $category ), $taxonomy ) ) {
$categories_ids[] = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
}
}
// 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文件中。经过测试,可以正常工作。