【问题标题】:php code - woocommerce ignore productphp 代码 - woocommerce 忽略产品
【发布时间】:2015-08-08 13:27:36
【问题描述】:

对不起,我在 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' ){ 但是一直没有成功

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    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' );
    

    【讨论】:

      猜你喜欢
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2014-12-05
      • 1970-01-01
      • 2014-09-20
      相关资源
      最近更新 更多