2015-08-08 48 views
0

對不起,我有點新手在PHP,但我想知道如果有人能夠指出我在正確的方向。我創建了一個完美的功能。它僅用於woocommerce並僅用於某個類別,刪除「添加到購物車」按鈕並替換爲指向另一個頁面的鏈接。這個類別中有一種產品需要忽略該功能。工作代碼是:php代碼 - woocommerce忽略產品

function fishing_buttons(){ 

// die early if we aren't on a product 
if (! is_product()) return; 

$product = get_product(); 

if (has_term('fishing', 'product_cat')){ 

    // removing the purchase buttons 
    remove_action('woocommerce_after_shop_loop_item', 'woocommerce_template_loop_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); 

    // adding our own custom text 
    add_action('woocommerce_after_shop_loop_item', 'fishing_priceguide'); 
    add_action('woocommerce_single_product_summary', 'fishing_priceguide', 30); 
    add_action('woocommerce_simple_add_to_cart', 'fishing_priceguide', 30); 
    add_action('woocommerce_grouped_add_to_cart', 'fishing_priceguide', 30); 
    add_action('woocommerce_variable_add_to_cart', 'fishing_priceguide', 30); 
    add_action('woocommerce_external_add_to_cart', 'fishing_priceguide', 30);} // fishing_buttons 
    add_action('wp', 'fishing_buttons'); 

    /** 
    * Our custom button 
    */ 
    function fishing_priceguide(){ 
     echo do_shortcode('[pl_button type="fish" link="http://localhost:8888/fish/fishing-price-guide/"]View our price guide[/pl_button]'); 
    } // fishing_priceguide 

產品ID是1268,我想忽略(即保持添加到購物車按鈕)。 if語句中是否可以包含'和'條件?我試過 if (has_term('fishing', 'product_cat') && product_id != '1268'){ 但是還沒有成功

回答

0

product沒有任何意義,因爲你擁有它。 $product是一個對象,產品ID存儲爲類變量$product->id。因此你的代碼應該是:

if (has_term('fishing', 'product_cat')&& $product->id != '1268') 

又是什麼鉤子fishing_buttons連接到?您可能只需使用global $product,但不需要再次檢索產品。

function fishing_buttons(){ 

global $product; 
if (has_term('fishing', 'product_cat')&& $product->id != '1268'){ 
// your stuff 
} 
} 
add_action('woocommerce_before_shop_loop_item', 'fishing_buttons'); 
+0

完美。這對我來說是訣竅。謝謝 –