![](/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.