我想做的是
现在这是代码的外观 -
add_action( 'init', array($this, 'woocommerce_custom_add_to_cart_remove_add_to_cart')); // Goes in class constructor.
function woocommerce_custom_add_to_cart_remove_add_to_cart() {
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
add_action( 'woocommerce_single_product_summary', array($this, 'woocommerce_custom_add_to_cart_custom_button'), 30 );
add_action( 'woocommerce_simple_add_to_cart', array($this, 'woocommerce_custom_add_to_cart_custom_button'), 30 );
add_action( 'woocommerce_grouped_add_to_cart', array($this, 'woocommerce_custom_add_to_cart_custom_button'), 30 );
add_action( 'woocommerce_variable_add_to_cart', array($this, 'woocommerce_custom_add_to_cart_custom_button'), 30 );
add_action( 'woocommerce_external_add_to_cart', array($this, 'woocommerce_custom_add_to_cart_custom_button'), 30 );
}
function woocommerce_custom_add_to_cart_custom_button() {
global $post,$product;
if($this->woocommerce_custom_add_to_cart_get_cartURL() !='')
echo '<form name="custom_cart_button" method="post" action="'.$this->woocommerce_custom_add_to_cart_get_cartURL().'"><button type="submit" class="single_add_to_cart_button button alt">'.$this->woocommerce_custom_add_to_cart_get_carttext().'</button></form><br />';
else {
// What logic should go here to handle the fall back, and show the default add to cart button. adding the actions wont work.
}
}
答案 0 :(得分:0)
尝试将remove_action和add_action代码移动到另一个钩子,在那里您将获得产品ID,例如“woocommerce_before_single_product_summary”并检查其中的条件,如果匹配则删除默认的woocommerce钩子并添加您的钩子。
并且不要删除钩子“woocommerce_single_product_summary - &gt; woocommerce_template_single_add_to_cart”并将其添加回来,因为它不需要。
add_action("woocommerce_before_single_product_summary", "test_func");
function test_func(){
global $post,$product;
if( TEST-CONDITION ){
remove_action( 'woocommerce_simple_add_to_cart', 'woocommerce_simple_add_to_cart', 30 );
remove_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_grouped_add_to_cart', 30 );
remove_action( 'woocommerce_variable_add_to_cart', 'woocommerce_variable_add_to_cart', 30 );
remove_action( 'woocommerce_external_add_to_cart', 'woocommerce_external_add_to_cart', 30 );
add_action( 'woocommerce_simple_add_to_cart', 'woocommerce_custom_add_to_cart_custom_button', 30 );
add_action( 'woocommerce_grouped_add_to_cart', 'woocommerce_custom_add_to_cart_custom_button', 30 );
add_action( 'woocommerce_variable_add_to_cart', 'woocommerce_custom_add_to_cart_custom_button', 30 );
add_action( 'woocommerce_external_add_to_cart', 'woocommerce_custom_add_to_cart_custom_button', 30 );
}
}
function woocommerce_custom_add_to_cart_custom_button() {
global $post,$product;
echo '<form name="custom_cart_button" method="post" action="REQUIRED_URL"><button type="submit" class="single_add_to_cart_button button alt">BUTTON TEXT</button></form><br />';
}