gpt4 book ai didi

php - 获取 WooCommerce 中的最小变动价格

转载 作者:行者123 更新时间:2023-12-01 17:59:12 25 4
gpt4 key购买 nike

我是一个初学者,试图学习 Wordpress 并拥有价格可变的产品:

  • 鳕鱼 250 卢比
  • 在线 200 卢比

  • 我想在我的主题中使用最低价格 functions.php .

    我将如何获取或获得最小值?

    我想在下面的代码中使用:
    function filter_gateways($gateways){

    $payment_NAME = 'cod'; //
    $min_variation_price = ''; <--------------- minimum variation price

    global $woocommerce;

    foreach ($woocommerce->cart->cart_contents as $key => $values ) {
    // Get the terms, i.e. category list using the ID of the product
    $terms = get_the_terms( $values['product_id'], 'product_cat' );
    // Because a product can have multiple categories, we need to iterate through the list of the products category for a match
    foreach ($terms as $term) {
    // 20 is the ID of the category for which we want to remove the payment gateway
    if($term->term_id == $min_variation_price){
    unset($gateways[$payment_NAME]);
    // If you want to remove another payment gateway, add it here i.e. unset($gateways['cod']);
    break;
    }
    break;
    }
    }
    return $gateways;
    }

    最佳答案

    中获取 WooCommerce 中的最小变化事件价格WC_Product_Variable 目的:

    $variation_min_price = $product->get_variation_price();

    It seems that you are trying here to unset 'cod' payment gateway when a cart item min variation price is matching with his product category ID.


    使用 WordPress 条件函数 has_term() 将真正简化您的代码(它接受任何分类法的术语 Id、术语 slug 或术语名称(如这里的产品类别的“product_cat”)。
    我想你的函数是在 中 Hook 的woocommerce_available_payment_gateways 过滤钩…
    我对您的代码进行了一些更改,因为它有点过时并且有一些错误:
    add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
    function conditional_payment_gateways( $available_gateways ) {
    // Not in backend (admin)
    if( is_admin() )
    return $available_gateways;

    foreach (WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
    // Get the WC_Product object
    $product = wc_get_product($cart_item['product_id']);
    // Only for variable products
    if($product->is_type('variable')){
    // Get the Variation "min" price
    $variation_min_price = $product->get_variation_price();
    // If the product category match with the variation 'min" price
    if( has_term( $variation_min_price, 'product_cat', $cart_item['product_id'] ) ){
    // Cash on delivery (cod) payment gateway
    unset($available_gateways['cod']); // unset 'cod'
    break; // stop the loop
    }
    }
    }
    return $available_gateways;
    }
    代码位于您的事件子主题(或主题)的 functions.php 文件中或任何插件文件中。
    这应该有效(但我无法真正测试它,因为它对产品类别 ID 和最小变化价格非常具体)......

    可变产品类型的所有相关价格方法:
    // All prices: multidimensional array with all variation prices (including active prices, regular prices and sale prices). 
    $prices = $product->get_variation_prices();
    $prices_for_display = $product->get_variation_prices( true ); // For display

    // The min or max active price.
    $min_price = $product->get_variation_price(); // Min active price
    $min_price_for_display = $product->get_variation_price('min', true); // Min active price for display
    $max_price = $product->get_variation_price('max'); // Max active price
    $max_price_for_display = $product->get_variation_price('max', true); // Max active price for display

    // The min or max regular price.
    $min_price = $product->get_variation_regular_price(); // Min regular price
    $min_price_for_display = $product->get_variation_regular_price('min', true); // Min regular price for display
    $max_price = $product->get_variation_regular_price('max'); // Max regular price
    $max_price_for_display = $product->get_variation_regular_price('max', true); // Max regular price for display

    // The min or max sale price.
    $min_price = $product->get_variation_sale_price(); // Min sale price
    $min_price_for_display = $product->get_variation_sale_price('min', true); // Min sale price for display
    $max_price = $product->get_variation_sale_price('max'); // Max sale price
    $max_price_for_display = $product->get_variation_sale_price('max', true); // Max sale price for display

    相关回答:
    Disable WooCommerce Payment methods if cart item quantity limit is reached

    关于php - 获取 WooCommerce 中的最小变动价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45518837/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com