在WooCommerce中通过URL获取并设置自定义产品价格

时间:2020-06-17 10:49:49

标签: php wordpress woocommerce get hook-woocommerce

我正在尝试使用URL将参数传递给Woocommerce网站-例如:

site/?custom_p=77将是URL。

这是我的代码

add_filter('woocommerce_product_get_price', 'custom_price', 99, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_price', 99, 2 );
function custom_price( $cprice, $product ) {
    // Delete product cached price  (if needed)
    wc_delete_product_transients($product->get_id());
    // Get the data from the GET request
$custom_p=$_GET['custom_p'];
    return ($custom_p) ;
}

URL添加到购物车site/?add-to-cart=455&custom_p=77

当我直接声明数字77时,该问题起作用了$custom_p=77,但是$_GET['custom_p']声明在购物车中给出了零。

3 个答案:

答案 0 :(得分:0)

URL更改后,您的GET变量将丢失,因此所有产品价格都为空,因为您也未定位到已添加到购物车中的产品。

在这种情况下,当检测到custom_p GET变量时,您需要启用并使用WooCommerce Session变量来存储产品ID和自定义价格。然后,您可以使用该WooCommerce会话变量来更改产品价格。

首先,我们检测必要的数据并将其存储在WC_Session变量中:

// get and set the custom product price in WC_Session
add_action( 'init', 'get_custom_product_price_set_to_session' );
function get_custom_product_price_set_to_session() {
    // Check that there is a 'custom_p' GET variable
    if( isset($_GET['add-to-cart']) && isset($_GET['custom_p']) 
    && $_GET['custom_p'] > 0 && $_GET['add-to-cart'] > 0 ) {
        // Enable customer WC_Session (needed on first add to cart)
        if ( ! WC()->session->has_session() ) {
            WC()->session->set_customer_session_cookie( true );
        }
        // Set the product_id and the custom price in WC_Session variable
        WC()->session->set('custom_p', [
            'id'    => (int) wc_clean($_GET['add-to-cart']),
            'price' => (float) wc_clean($_GET['custom_p']),
        ]);
    }
}

然后有两种方法可以更改添加到购物车的产品价格(仅选择一种)

选项1-直接更改产品价格:

// Change product price from WC_Session data
add_filter('woocommerce_product_get_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_variation_get_regular_price', 'custom_product_price', 900, 2 );
function custom_product_price( $price, $product ) {
    if ( ( $data = WC()->session->get('custom_p') ) && $product->get_id() == $data['id'] ) {
        $price = $data['price'];
    }
    return $price;
}

选项2-更改购物车商品价格:

// Change cart item price from WC_Session data
add_action( 'woocommerce_before_calculate_totals', 'custom_cart_item_price', 20, 1 );
function custom_cart_item_price( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Must be required since Woocommerce version 3.2 for cart items properties changes
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    // Looo through our specific cart item keys
    foreach ( $cart->get_cart() as $cart_item ) {
        // Get custom product price for the current item
        if ( ( $data = WC()->session->get('custom_p') ) && $cart_item['data']->get_id() == $data['id'] ) {
            // Set the new product price
            $cart_item['data']->set_price( $data['price'] );
        }
    }
}

代码进入活动子主题(或活动主题)的functions.php文件中。经过测试,可以正常工作。

使用URL变量示例:www.example.com/?add-to-cart=37&custom_p=75

答案 1 :(得分:0)

选项 1 对 WC 管理员给出了致命错误。为了解决这个问题,我必须将 !is_admin() 添加到 if 子句中,使其看起来像这样:

    add_filter('woocommerce_product_get_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_get_regular_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_variation_get_price', 'custom_product_price', 900, 2 );
add_filter('woocommerce_product_variation_get_regular_price', 'custom_product_price', 900, 2 );
function custom_product_price( $price, $product ) {
    if ( ( !is_admin() && $data = WC()->session->get('custom_p') ) && $product->get_id() == $data['id'] ) {
        $price = $data['price'];
    }
    return $price;
}

答案 2 :(得分:0)

很好,即使当 Dreamweaver 说在设置 WC_Session 变量的第一个 Part 中存在语法错误时,它也能工作:

<块引用>
    // Set the product_id and the custom price in WC_Session variable
    WC()->session->set('custom_p', [
        'id'    => (int) wc_clean($_GET['add-to-cart']),
        'price' => (float) wc_clean($_GET['custom_p']),
    ]);
相关问题