【问题标题】:Change cart item price if a specific item is in the cart on Woocommerce如果特定商品在 Woocommerce 的购物车中,则更改购物车商品价格
【发布时间】:2017-11-26 17:16:25
【问题描述】:

我正在使用 WooCommerce 会员资格,如果首次购买特定产品的会员,我想将免费会员资格提供给他们。我可以让其中一些单独工作,但无法将它们全部组合在一起。

他们必须购买的商品也有销售价格,因此我也在检查日期以查看该商品是否在销售窗口中。那部分正在工作。

似乎当我添加参数以检查商品是否在购物车中时,整个事情都中断了。如果我分别运行这两个函数,它们都可以工作。

function mps_registration_price($cart_object) {
    global $post, $product;

    // Bail if memberships is not active
    if ( ! function_exists( 'wc_memberships' ) ) {
        return;
    }

    $user_id = get_current_user_id();
    date_default_timezone_set("America/Chicago");
    $today = time();

    // Check if user was never a member 
    if ( !wc_memberships_get_user_memberships( $user_id )) {
        if( mps_check_for_product() ) {

        foreach ( $cart_object->get_cart() as $cart_item ) {

            // get the product id (or the variation id)
            $id = $cart_item['data']->get_id();

            if($id == 17) {
                    $salePrice = get_post_meta($id, '_sale_price', true);
                    $saleFrom = get_post_meta($id, '_sale_price_dates_from', true);
                    $saleTo = get_post_meta($id, '_sale_price_dates_to', true); 

                    if( !empty($salePrice) && $today >= $saleFrom && $today <= $saleTo ) {
                        $new_price = $salePrice;
                    } else {
                        $new_price = get_post_meta($id, '_regular_price', true);
                    }
                    // Updated cart item price
                    $cart_item['data']->set_price( $new_price ); 
                }
            }
        } else {

            foreach ( $cart_object->get_cart() as $cart_item ) {

                // get the product id (or the variation id)
                $id = $cart_item['data']->get_id();

                if($id == 17) {
                    $salePrice = get_post_meta($id, '_sale_price', true);
                    $saleFrom = get_post_meta($id, '_sale_price_dates_from', true);
                    $saleTo = get_post_meta($id, '_sale_price_dates_to', true); 

                    if( !empty($salePrice) && $today >= $saleFrom && $today <= $saleTo ) {
                        $new_price = $salePrice + 50;
                    } else {
                        $new_price = get_post_meta($id, '_regular_price', true);
                    }
                    // Updated cart item price
                    $cart_item['data']->set_price( $new_price ); 
                }
            }
        }
    }

}

add_filter( 'woocommerce_before_calculate_totals', 'mps_registration_price', 10, 1 );

function mps_check_for_product() {

    $product_id = 11;
    $product_cart_id = WC()->cart->generate_cart_id( $product_id );
    $in_cart = WC()->cart->find_product_in_cart( $product_cart_id );

    if ( $in_cart ) {
        return true;
    } else {
        return false;
    }
}

add_action('woocommerce_before_cart', 'mps_check_for_product');

【问题讨论】:

    标签: php wordpress woocommerce cart membership


    【解决方案1】:

    更新 (2018 年 10 月)

    您在此处将所有内容都复杂化了,因为您只想在客户还不是会员的情况下向特定产品 (ID 17) 添加额外费用。

    您不需要 2 个单独的函数,并且您的第二个函数以错误的方式执行此操作。如果您在第一个挂钩函数中调用它,则不需要挂钩。

    我已经完全重新访问了您的代码,使用 WC_Product 方法而不是 get_post_meta()...

    还有WC_Product 方法is_on_sale() 管理一切,例如促销产品价格的起止日期。所以你不需要在你的函数中得到它。

    在您的代码中,当产品 11 在购物车中或不在您的 2 个购物车循环中的购物车中时,没有差异......所以您可能需要进行一些更改 那里。

    所以你的代码现在非常紧凑:

    add_action( 'woocommerce_before_calculate_totals', 'mps_registration_price', 20, 1 );
    function mps_registration_price( $cart ) {
    
        if ( is_admin() && ! defined( 'DOING_AJAX' ) )
            return;
    
        if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
            return;
    
        // Bail if memberships is not active
        if ( ! function_exists( 'wc_memberships' ) ) return; // Exit
    
        // Only for non members
        $user_id = get_current_user_id();
        if ( wc_memberships_get_user_memberships( $user_id ) ) return; // Exit
    
        // First loop to check if product 11 is in cart
        foreach ( $cart->get_cart() as $cart_item ){
            $is_in_cart = $cart_item['product_id'] == 11 ? true : false;
        }
    
        // Second loop change prices
        foreach ( $cart->get_cart() as $cart_item ) {
    
            // Get an instance of the WC_Product object (or the variation product object)
            $product = $cart_item['data'];
    
            // Method is_on_sale() manage everything (dates…)
            // Here we target product ID 17
            if( $product->is_on_sale() && $product->get_id() == 17 ) {
    
                // When product 11 is not cart
                if( ! $is_in_cart ){
                    $product->set_price( $product->get_sale_price() + 50);
                }
            }
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件或任何插件文件中。

    经过测试并且有效。

    【讨论】:

    • 非常好!谢谢!
    • 我似乎无法让它反过来工作,就像产品 14 在产品 17 之前在购物车中一样。我认为逻辑仍然是相同的,但价格没有改变。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2017-12-10
    • 2021-06-28
    • 2019-12-21
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    相关资源
    最近更新 更多