douhui1630 2019-05-19 16:16
浏览 92
已采纳

在WooCommerce中为特定产品和特定客户折扣

I am trying to change the price for one logged in customer for one product. When using this code, the price does change, but only on the product page. I need the price to change everywhere (archive, product page, cart etc.)

Here's the code that I am trying to make work.

add_filter( 'woocommerce_product_get_price', 'special_discount_for_product_cat_for_logged_in_customers', 10, 2 );
function special_discount_for_product_cat_for_logged_in_customers( $price, $product ) {

    $current_user_id = get_current_user_id();
    if( $current_user_id == 433 && is_user_logged_in() ) {
        if (is_product( '11323', $product->get_id())) {
            $price = 9.95;
        }
    }
    return $price;
}

Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • dpw43061 2019-05-19 18:17
    关注

    Try the following instead, that will change everywhere the product price for a specific product and a specific user Id:

    add_filter( 'woocommerce_product_get_price', 'special_discount_for_product_cat_for_logged_in_customers', 10, 2 );
    function special_discount_for_product_cat_for_logged_in_customers( $price, $product ) {
        // YOUR SETTINGS
        $targeted_user_id    = 433;
        $targeted_product_id = 11323;
    
        if( get_current_user_id() == $targeted_user_id && $product->get_id() == $targeted_product_id ) {
            $price = 9.95;
        }
        return $price;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?