在Woocommerce Force Sells旁边添加价格 [英] Add price beside Woocommerce Force Sells

查看:96
本文介绍了在Woocommerce Force Sells旁边添加价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,Woocommerce Force Sells仅列出产品标题.我还需要它们在每个标题旁边的方括号中显示产品价格.您可以在>在此处此处 看到它在此屏幕截图中:

Woocommerce Force Sells by default only lists the product titles. I need them to also show the product price in brackets beside each title. You can see it here like in this screenshot:

但是应该是:

  • 出生池租用保证金($ 50.00)
  • 分娩池衬里(33.00美元)

是否可以使用过滤器,以便强制销售"将价格放在项目旁边?除非如此,我如何才能更改强制销售"代码,使其也能输出价格?

Is there a filter I can use so that Force Sells puts the prices beside items? Barring that, how can I alter the Force Sells code so that it also outputs price as well?

这是强制销售"代码的一部分,该代码在单个产品页面的添加到购物车"框中输出物品:

This is the part of the Force Sells code that outputs the items under the add to cart box on single product pages:

    /**
     * Displays information of what linked products that will get added when current
     * product is added to cart.
     */
    public function show_force_sell_products() {
        global $post;

        $product_ids = $this->get_force_sell_ids( $post->ID, array( 'normal', 'synced' ) );
        $titles      = array();

        // Make sure the products still exist and don't display duplicates.
        foreach( array_values( array_unique( $product_ids ) ) as $key => $product_id ) {
            $product = wc_get_product( $product_id );

            if ( $product && $product->exists() && 'trash' !== $product->get_status() ) {
                $titles[] = version_compare( WC_VERSION, '3.0', '>=' ) ? $product->get_title() : get_the_title( $product_id );
            }
        }

        if ( ! empty( $titles ) ) {
            echo '<div class="clear"></div>';
            echo '<div class="wc-force-sells">';
            echo '<p>' . __( 'This will also add the following products to your cart:', 'woocommerce-force-sells' ) . '</p>';
            echo '<ul>';

            foreach ( $titles as $title ) {
                echo '<li>' . $title . '</li>';
            }

            echo '</ul></div>';
        }
    }

推荐答案

您可以尝试以下操作:

add_filter( 'woocommerce_product_title', 'custom_product_title', 20, 2 );
function custom_product_title( $name, $product ) {
    // Only in single product pages
    if( ! is_product() ) return $name;

    // The product name + the product formatted price
    return $name . ' (' . wc_price( wc_get_price_to_display( $product ) ) . ')';
}

代码进入您的活动子主题(或活动主题)的function.php文件中.应该可以.

Code goes in function.php file of your active child theme (or active theme). It should work.

这篇关于在Woocommerce Force Sells旁边添加价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆