doubei8541 2015-08-08 13:27
浏览 61
已采纳

PHP代码 - woocommerce忽略产品

Sorry I am somewhat of a novice at php but am wondering if someone might be able to point me in the right direction. I have a function created that works perfect. It is used in woocommerce and for a certain category only, removes the 'add to cart' button and replaces with a link to another page. There is one product in this category that I need to ignore the function for. The working code is:

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

The product ID is 1268 that I want to ignore (i.e. keep the add to cart button). Is it possible to include an 'and' condition in the if statement? I have tried if ( has_term( 'fishing', 'product_cat' ) && product_id != '1268' ){ But have not had any success

  • 写回答

1条回答 默认 最新

  • dousuitang5239 2015-08-09 22:33
    关注

    product means nothing as you have it. $product is an object and the product ID is stored as the class variable $product->id. Therefore your code should be:

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

    Also what hook is fishing_buttons attached to? You can probably just use the global $product but don't need to retrieve the product again.

    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' );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何解决蓝牙通话音频突发失真问题
  • ¥15 安装opengauss数据库报错
  • ¥15 【急】在线问答CNC雕刻机的电子电路与编程
  • ¥60 在mc68335芯片上移植ucos ii 的成功工程文件
  • ¥15 笔记本外接显示器正常,但是笔记本屏幕黑屏
  • ¥15 Python pandas
  • ¥15 蓝牙硬件,可以用哪几种方法控制手机点击和滑动
  • ¥15 生物医学数据分析。基础课程就v经常唱课程舅成牛逼
  • ¥15 云环境云开发云函数对接微信商户中的分账功能
  • ¥15 空间转录组CRAD遇到问题