亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

WooCommerce:刪除存檔上但不在購物車中的“添加到購物車”按鈕

WooCommerce:刪除存檔上但不在購物車中的“添加到購物車”按鈕

PHP
翻閱古今 2023-08-06 10:37:07
我想刪除除購物車本身之外的每個頁面上的“添加到購物車”按鈕。我找到了一個用鉤子移除購物車按鈕的解決方案:remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );工作正常,但到處都刪除了按鈕。我嘗試使用此代碼來刪除除購物車之外的所有位置的按鈕:if (!is_cart()) :    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );endif;但這似乎不起作用。有沒有其他方法而不觸及模板文件?
查看完整描述

2 回答

?
慕婉清6462132

TA貢獻1804條經驗 獲得超2個贊

下面的代碼可以解決這個問題(基于woocommerce_template_loop_add_to_cart()原始函數代碼):

add_action('init', 'remove_add_to_cart_function_callback' );

function remove_add_to_cart_function_callback() {

? ? remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );

? ? add_action( 'woocommerce_after_shop_loop_item', 'custom_template_loop_add_to_cart', 10 );

}


if ( ! function_exists( 'woocommerce_template_loop_add_to_cart' ) ) {


? ? function custom_template_loop_add_to_cart( $args = array() ) {

? ? ? ? global $product;


? ? ? ? if ( $product && is_cart() ) {

? ? ? ? ? ? $defaults = array(

? ? ? ? ? ? ? ? 'quantity'? ?=> 1,

? ? ? ? ? ? ? ? 'class'? ? ? => implode(

? ? ? ? ? ? ? ? ? ? ' ',

? ? ? ? ? ? ? ? ? ? array_filter(

? ? ? ? ? ? ? ? ? ? ? ? array(

? ? ? ? ? ? ? ? ? ? ? ? ? ? 'button',

? ? ? ? ? ? ? ? ? ? ? ? ? ? 'product_type_' . $product->get_type(),

? ? ? ? ? ? ? ? ? ? ? ? ? ? $product->is_purchasable() && $product->is_in_stock() ? 'add_to_cart_button' : '',

? ? ? ? ? ? ? ? ? ? ? ? ? ? $product->supports( 'ajax_add_to_cart' ) && $product->is_purchasable() && $product->is_in_stock() ? 'ajax_add_to_cart' : '',

? ? ? ? ? ? ? ? ? ? ? ? )

? ? ? ? ? ? ? ? ? ? )

? ? ? ? ? ? ? ? ),

? ? ? ? ? ? ? ? 'attributes' => array(

? ? ? ? ? ? ? ? ? ? 'data-product_id'? => $product->get_id(),

? ? ? ? ? ? ? ? ? ? 'data-product_sku' => $product->get_sku(),

? ? ? ? ? ? ? ? ? ? 'aria-label'? ? ? ?=> $product->add_to_cart_description(),

? ? ? ? ? ? ? ? ? ? 'rel'? ? ? ? ? ? ? => 'nofollow',

? ? ? ? ? ? ? ? ),

? ? ? ? ? ? );


? ? ? ? ? ? $args = apply_filters( 'woocommerce_loop_add_to_cart_args', wp_parse_args( $args, $defaults ), $product );


? ? ? ? ? ? if ( isset( $args['attributes']['aria-label'] ) ) {

? ? ? ? ? ? ? ? $args['attributes']['aria-label'] = wp_strip_all_tags( $args['attributes']['aria-label'] );

? ? ? ? ? ? }


? ? ? ? ? ? wc_get_template( 'loop/add-to-cart.php', $args );

? ? ? ? }

? ? }

}

代碼位于活動子主題(或活動主題)的functions.php 文件中。經過測試并有效。



查看完整回答
反對 回復 2023-08-06
?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

如果不是購物車,您嘗試過嗎?


if (!is_cart()) :

    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );

endif;


查看完整回答
反對 回復 2023-08-06
  • 2 回答
  • 0 關注
  • 146 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號