【问题标题】:GET and set a custom product price on add to cart via URL in WooCommerce通过 WooCommerce 中的 URL 获取并设置自定义产品价格添加到购物车
【发布时间】:2020-06-17 10:49:49
【问题描述】:

我正在尝试使用 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) ;
}

网址加入购物车site/?add-to-cart=455&custom_p=77

当我直接声明数字 77 时出现问题 $custom_p=77$_GET['custom_p'] 声明导致购物车中的结果为零。

【问题讨论】:

  • @RST 自 Woocommerce 版本 3 起,woocommerce_get_price 已被弃用。

标签: php wordpress woocommerce get hook-woocommerce


【解决方案1】:

一旦 URL 更改,您的 GET 变量就会丢失,因此所有产品价格都为空,因为您也没有针对已添加到购物车的产品。

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

首先我们检测必要的数据并将其存储在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 文件中。经过测试并且可以工作。

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

【讨论】:

  • 选项 1 与选项 2 相比效果很好,它在购物车中显示正确的总数,但价格不正确。非常感谢,已经解决了
  • @NaceurElhani 所以你选择第一个......现在这个答案有效并回答了你的问题,你可以请accept回答,谢谢。
【解决方案2】:

选项 1 在 WC admin 上出现致命错误。为了解决这个问题,我必须在 if 子句中添加!is_admin(),所以它应该是这样的:

    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;
}

【讨论】:

    【解决方案3】:

    很好,即使 Dreamweaver 说在设置 WC_Session 变量的第一部分存在语法错误,它也能正常工作:

        // 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']),
        ]);
    

    【讨论】:

      猜你喜欢
      • 2019-12-21
      • 2021-01-07
      • 2018-10-31
      • 1970-01-01
      • 2021-07-11
      • 2015-11-28
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多