【问题标题】:Woocommerce get variation product priceWoocommerce 获得变化的产品价格
【发布时间】:2012-09-04 23:43:16
【问题描述】:

我试图在变体下拉列表中显示产品变体价格。 当您在下拉列表中选择变体时,我试图更改价格显示在 div 内的默认行为。

问题是我无法找到该 div 获取变化价格的位置。我搜索了所有 javascript 但找不到它

如果我使用:

add_filter('woocommerce_variation_option_name' ,'add_price_to_dropdown');

function add_price_to_dropdown($name){

    global $product;
    return $name.' '.$product->get_price_html();
}

我只是得到所有选项的最小变化价格。我想得到每个变化的价格。有什么线索吗?

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    这是您要查找的代码

    add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
    
    function display_price_in_variation_option_name( $term ) {
        global $wpdb, $product;
    
        if ( empty( $term ) ) return $term;
        if ( empty( $product->id ) ) return $term;
    
        $id = $product->get_id();
    
        $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
    
        $term_slug = ( !empty( $result ) ) ? $result[0] : $term;
    
        $query = "SELECT postmeta.post_id AS product_id
                    FROM {$wpdb->prefix}postmeta AS postmeta
                        LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
                    WHERE postmeta.meta_key LIKE 'attribute_%'
                        AND postmeta.meta_value = '$term_slug'
                        AND products.post_parent = $id";
    
        $variation_id = $wpdb->get_col( $query );
    
        $parent = wp_get_post_parent_id( $variation_id[0] );
    
        if ( $parent > 0 ) {
             $_product = new WC_Product_Variation( $variation_id[0] );
             return $term . ' (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
        }
        return $term;
    
    }
    

    希望这会有用。

    【讨论】:

    • @chifliiiii 或任何我会在哪里放置此代码的人,这正是我需要的,但我不确定将其放置在哪里。谢谢!
    • 在你的functions.php中
    • 此代码显示的折扣价格不正确(销售)。我认为同样的问题也可能出现在含关税的价格上。
    • 非常感谢。简单、快速且有效的解决方案。
    • 警告:代码需要使用 esc_sql() 函数转义 $term 变量!
    【解决方案2】:

    这可能对你们有帮助;在搜索如何使用新版本的 WC 时:

    global $woocommerce;
    $product_variation = new WC_Product_Variation($_POST['variation_id']);
    $regular_price = $product_variation->regular_price;
    

    我正在使用 ajax 并使用 post 方法传递变体的 ID。

    【讨论】:

    • 我加 1,因为这比接受的答案要干净得多(它告诉我我需要在上面的答案中的代码中添加 global $woocommerce;),但不是这样通过引用函数(例如get_price())而不是字段(在您的情况下为regular_price)从WC_Product_Variation对象获取价格更具前瞻性,因为后者更有可能在未来版本中发生变化WooCommerce??
    【解决方案3】:

    我的问题与 OP 完全相同,但我的情况有点不同。这是我的解决方案,它可能会帮助其他也登陆此页面的人。

    function get_product_variation_price($variation_id) {
        $product = new WC_Product_Variation($variation_id);
        return $product->product_custom_fields['_price'][0];
    }
    

    WooCommerce 2.2.10 更新: 上述代码不适用于当前版本的 WooCommerce。下面的这段代码可以运行并且值得考虑,因为它使用了 WooCommerce API,它避免了手动 SQL 查询并且非常简单......

    /*
     * You can find the $variation_id in the Product page (go to Product Data > Variations, and it is shown with a preceeding "#" character)
     */
    function get_product_variation_price($variation_id) {
    
        global $woocommerce; // Don't forget this!
        $product = new WC_Product_Variation($variation_id);
        //return $product->product_custom_fields['_price'][0]; // No longer works in new version of WooCommerce
        //return $product->get_price_html(); // Works. Use this if you want the formatted price
        return $product->get_price(); // Works. Use this if you want unformatted price
    
    }
    

    【讨论】:

    • 我刚刚发现,此功能不再适用于最新版本的 WooCommerce。问题解决后,我将更新此代码。
    • 使用工作代码更新答案,使用 WooCommerce 2.2.10 测试。
    • 那么这与原始钩子/过滤器有何关系?请问 $variation_id 来自哪里?想要在可变选项旁边添加价格。
    • 我不确定它是否与原始钩子/过滤器有关,因为它确实有不同的用例(但可用于解决原始问题)。 $variation_id 来自 wp-admin 中的产品变体设置。当您查看产品的产品变体数据时,您应该会看到它。
    • 非常感谢过去 3 天的搜索。现在我可以找到解决方案了。
    【解决方案4】:

    我一直在 Woocommerce 上使用此代码来显示我的变体价格,当我更新到 Woocommerce 2.0 时,我注意到每当我编辑产品时 wp-admin/error-log 中都会出现数据库错误。我不确定在更新到 2.0 之前是否也发生了错误,因为我在更新之前很久没有使用 Woocommerce 并且认为我直到今天才检查错误日志。

    变化价格按预期显示,但每次我打开产品进行编辑时,错误日志都会填满以下错误。与我正在编辑的产品相关的每个变体都有一个错误。

    WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8 for query SELECT postmeta.post_id AS product_id
                FROM wp_postmeta AS postmeta
                    LEFT JOIN wp_posts AS products ON ( 
    
    products.ID = postmeta.post_id )
                WHERE postmeta.meta_key LIKE 'attribute_%'
                    AND postmeta.meta_value = '12'
                    AND products.post_parent =  made by 
    
    include('wp-admin/edit-form-advanced.php'), do_meta_boxes, call_user_func, woocommerce_product_data_box, do_action('woocommerce_product_write_panels'), call_user_func_array, variable_product_type_options, include('/plugins/woocommerce/admin/post-types/writepanels/variation-admin-html.php'), apply_filters('woocommerce_variation_option_name'), call_user_func_array, display_price_in_variation_option_name
    

    我在您的代码中更改了以下行: AND products.post_parent = $product->id"; 对此 AND products.post_parent = '$product->id' ";

    现在,没有更多错误了。错误日志保持良好且为空。

    只是想分享一下,以防其他人遇到问题。

    【讨论】:

      【解决方案5】:

      我对这个问题的看法,在最新的 Woocommerce 中工作:3.2.1(2017 年 10 月) 价格将显示在下拉列表中的变体旁边。

      add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
      
      function display_price_in_variation_option_name( $term ) {
          global $wpdb, $product;
      
          $result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
      
          $term_slug = ( !empty( $result ) ) ? $result[0] : $term;
      
      
          $query = "SELECT postmeta.post_id AS product_id
                      FROM {$wpdb->prefix}postmeta AS postmeta
                          LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
                      WHERE postmeta.meta_key LIKE 'attribute_%'
                          AND postmeta.meta_value = '$term_slug'
                          AND products.post_parent = $product->id";
      
          $variation_id = $wpdb->get_col( $query );
      
          $parent = wp_get_post_parent_id( $variation_id[0] );
      
          if ( $parent > 0 ) {
              $_product = new WC_Product_Variation( $variation_id[0] );
              $_currency = get_woocommerce_currency_symbol();
              return $term . ' ('.$_currency.' '. $_product->get_price()  . ')';
          }
          return $term;
      
      }

      希望这个对某人有所帮助。 将此添加到您的子主题functions.php中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-22
        • 2015-09-24
        • 2019-02-12
        • 2017-11-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多