【问题标题】:Product price based on cookie data - WooCommerce基于 cookie 数据的产品价格 - WooCommerce
【发布时间】:2019-12-25 08:09:31
【问题描述】:

我在网站加载时有一个自定义弹出窗口并将所选选项存储在 cookie 中,我想根据所选选项更改产品价格。

我已经根据产品中的那些选项添加了价格元框,用于简单和可变的产品。

并在前端检索该价格,请检查以下代码:

add_filter( 'woocommerce_product_get_price',  'return_price_by_option', 10, 2 );

function return_price_by_option( $price, $product ) {

    // options can be digits like 1, 2, 3, ...
    $option = isset( $_COOKIE['option'] ) ? $_COOKIE['option'] : '';

    if( $option ) {
        $regMKey    = '_sale_price_option_' . $option;
        $optPrice   = get_post_meta( $product->get_id(), $regMKey, true );

        if( empty($optPrice) ) {
            $regMKey    = '_regular_price_option_' . $option;
            $optPrice   = get_post_meta( $product->get_id(), $regMKey, true );
        }
        return $optPrice;
    }
    return $price;
}

// Also used two more filter for regular and sale price,
// Function have same code, but just return the price for same meta key 
add_filter( 'woocommerce_product_get_regular_price', 'return_regular_price_by_option', 10, 2 );
add_filter( 'woocommerce_product_get_sale_price',  'return_sale_price_by_option'), 10, 2 );

它适用于简单的产品, 但是,不适用于可变产品,任何人都可以帮助我处理可变产品吗?请提出任何简单的方法。

【问题讨论】:

    标签: wordpress dynamic woocommerce price


    【解决方案1】:

    添加以下代码 sn-p 以更改可变和变体产品的价格 -

    function return_price_by_option( $price, $product ) {
        // delete product cache
        wc_delete_product_transients( $product->get_id() );
    
        // options can be digits like 1, 2, 3, ...
        $option = isset( $_COOKIE['option'] ) ? $_COOKIE['option'] : '';
    
        if( $option ) {
            $regMKey    = '_sale_price_option_' . $option;
            $optPrice   = get_post_meta( $product->get_id(), $regMKey, true );
    
            if( empty($optPrice) ) {
                $regMKey    = '_regular_price_option_' . $option;
                $optPrice   = get_post_meta( $product->get_id(), $regMKey, true );
            }
            return $optPrice;
        }
        return $price;
    }
    // Variable
    add_filter('woocommerce_product_variation_get_regular_price', 'return_price_by_option', 99, 2 );
    add_filter('woocommerce_product_variation_get_price', 'return_price_by_option' , 99, 2 );
    
    function return_price_by_option_variation( $price, $variation, $product ) {
        // delete product cache
        wc_delete_product_transients($variation->get_id());
    
        // options can be digits like 1, 2, 3, ...
        $option = isset( $_COOKIE['option'] ) ? $_COOKIE['option'] : '';
    
        if( $option ) {
            $regMKey    = '_sale_price_option_' . $option;
            $optPrice   = get_post_meta( $product->get_id(), $regMKey, true );
    
            if( empty($optPrice) ) {
                $regMKey    = '_regular_price_option_' . $option;
                $optPrice   = get_post_meta( $product->get_id(), $regMKey, true );
            }
            return $optPrice;
        }
        return $price;
    }
    
    // Variations
    add_filter('woocommerce_variation_prices_price', 'return_price_by_option_variation', 99, 3 );
    add_filter('woocommerce_variation_prices_regular_price', 'return_price_by_option_variation', 99, 3 );
    

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 2021-08-01
      • 2018-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多