【问题标题】:dynamic pricing and request a quote woocommerce动态定价并请求报价 woocommerce
【发布时间】:2017-05-30 14:27:39
【问题描述】:

世界你好!

我目前正在做一个基于woocommerce的电子商务网站,我最近买了

YITH 制作的动态定价和请求报价插件

这些插件一切正常,但我正在做的电子商务网站既可以报价也可以购物车,这就是我使用请求报价插件的原因,但我所做的不同动态价格在我的报价页面中不可见,但是它们在我的购物车页面中。

在google上搜索了很多,这两个插件似乎不兼容。

所以这是我的问题,有没有办法在我的报价页面中挂钩动态价格插件获得的最终价格?

我知道一些 PHP 的基础知识,我查看了这两个插件的不同函数和类以尝试解决问题,但我没有找到它.. :(

这是我正在做的网站的测试版,http://beta.jardivrac.com你可以在这个页面上尝试动态价格,然后转到卡片页面和报价页面

这是我在动态价格插件的前端类中找到的公共函数 ->

public function replace_cart_item_price( $price, $cart_item, $cart_item_key ) {

        if ( ! isset( $cart_item['ywdpd_discounts'] ) ) {
            return $price;
        }
        $old_price = $price;

        foreach ( $cart_item['ywdpd_discounts'] as $discount ) {
            if ( isset( $discount['status'] ) && $discount['status'] == 'applied' ) {

                if ( wc_price( $cart_item['ywdpd_discounts']['default_price'] ) != WC()->cart->get_product_price( $cart_item['data'] ) ) {
                    $price = '<del>' . wc_price( $cart_item['ywdpd_discounts']['default_price'] ) . '</del> ' . WC()->cart->get_product_price( $cart_item['data'] );
                } else {
                    return $price;
                }
            }
        }

        $price = apply_filters( 'ywdpd_replace_cart_item_price', $price, $old_price, $cart_item, $cart_item_key );

        return $price;
    }

如果有人有解决方案,那将是一个好消息!

提前感谢您的帮助!

安东尼

编辑:

这是 Woocommerce cart.php 中的循环

foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        $_product   = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
        $product_id = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

        if ( $_product && $_product->exists() && $cart_item['quantity'] > 0 && apply_filters( 'woocommerce_cart_item_visible', true, $cart_item, $cart_item_key ) ) {
            $product_permalink = apply_filters( 'woocommerce_cart_item_permalink', $_product->is_visible() ? $_product->get_permalink( $cart_item ) : '', $cart_item, $cart_item_key );
            ?>
            <tr class="<?php echo esc_attr( apply_filters( 'woocommerce_cart_item_class', 'cart_item', $cart_item, $cart_item_key ) ); ?>">

                <td class="product-remove">
                    <?php
                        echo apply_filters( 'woocommerce_cart_item_remove_link', sprintf(
                            '<a href="%s" class="remove" title="%s" data-product_id="%s" data-product_sku="%s">&times;</a>',
                            esc_url( WC()->cart->get_remove_url( $cart_item_key ) ),
                            __( 'Remove this item', 'woocommerce' ),
                            esc_attr( $product_id ),
                            esc_attr( $_product->get_sku() )
                        ), $cart_item_key );
                    ?>
                </td>

                <td class="product-thumbnail">
                    <?php
                        $thumbnail = apply_filters( 'woocommerce_cart_item_thumbnail', $_product->get_image(), $cart_item, $cart_item_key );

                        if ( ! $product_permalink ) {
                            echo $thumbnail;
                        } else {
                            printf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $thumbnail );
                        }
                    ?>
                </td>

                <td class="product-name" data-title="<?php _e( 'Product', 'woocommerce' ); ?>">
                    <?php
                        if ( ! $product_permalink ) {
                            echo apply_filters( 'woocommerce_cart_item_name', $_product->get_title(), $cart_item, $cart_item_key ) . '&nbsp;';
                        } else {
                            echo apply_filters( 'woocommerce_cart_item_name', sprintf( '<a href="%s">%s</a>', esc_url( $product_permalink ), $_product->get_title() ), $cart_item, $cart_item_key );
                        }

                        // Meta data
                        echo WC()->cart->get_item_data( $cart_item );

                        // Backorder notification
                        if ( $_product->backorders_require_notification() && $_product->is_on_backorder( $cart_item['quantity'] ) ) {
                            echo '<p class="backorder_notification">' . esc_html__( 'Available on backorder', 'woocommerce' ) . '</p>';
                        }
                    ?>
                </td>

                <td class="product-price" data-title="<?php _e( 'Price', 'woocommerce' ); ?>">
                    <?php
                        echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
                    ?>
                </td>

                <td class="product-quantity" data-title="<?php _e( 'Quantity', 'woocommerce' ); ?>">
                    <?php
                        if ( $_product->is_sold_individually() ) {
                            $product_quantity = sprintf( '1 <input type="hidden" name="cart[%s][qty]" value="1" />', $cart_item_key );
                        } else {
                            $product_quantity = woocommerce_quantity_input( array(
                                'input_name'  => "cart[{$cart_item_key}][qty]",
                                'input_value' => $cart_item['quantity'],
                                'max_value'   => $_product->backorders_allowed() ? '' : $_product->get_stock_quantity(),
                                'min_value'   => '0'
                            ), $_product, false );
                        }

                        echo apply_filters( 'woocommerce_cart_item_quantity', $product_quantity, $cart_item_key, $cart_item );
                    ?>
                </td>

                <td class="product-subtotal" data-title="<?php _e( 'Total', 'woocommerce' ); ?>">
                    <?php
                        echo apply_filters( 'woocommerce_cart_item_subtotal', WC()->cart->get_product_subtotal( $_product, $cart_item['quantity'] ), $cart_item, $cart_item_key );
                    ?>
                </td>
            </tr>
            <?php
        }
    }

这是引用的循环

$total = 0;
            foreach ( $raq_content as $key => $raq ):

            $_product = wc_get_product( ( isset( $raq['variation_id'] ) && $raq['variation_id'] != '' ) ? $raq['variation_id'] : $raq['product_id'] );

            if( ! $_product ){
                continue;
            }

            $show_price = true;


            do_action( 'ywraq_before_request_quote_view_item', $raq_content, $key );
            ?>
            <tr class="<?php echo esc_attr( apply_filters( 'yith_ywraq_item_class', 'cart_item', $raq_content, $key ) ); ?>" <?php echo esc_attr( apply_filters( 'yith_ywraq_item_attributes', '', $raq_content, $key ) ); ?>>

                <td class="product-remove">
                    <?php
                    echo apply_filters( 'yith_ywraq_item_remove_link', sprintf( '<a href="#"  data-remove-item="%s" data-wp_nonce="%s"  data-product_id="%d" class="yith-ywraq-item-remove remove" title="%s">&times;</a>', $key, wp_create_nonce( 'remove-request-quote-' . $_product->id ), $_product->id, __( 'Retirer cet article', 'yith-woocommerce-request-a-quote' ) ), $key );
                    ?>

                </td>

                <td class="product-thumbnail">
                    <?php $thumbnail = $_product->get_image();

                    if ( ! $_product->is_visible() ) {
                        echo $thumbnail;
                    } else {
                        printf( '<a href="%s">%s</a>', $_product->get_permalink(), $thumbnail );
                    }
                    ?>
                </td>

                <td class="product-name">
                    <?php
                    $title = $_product->get_title();

                    if ( $_product->get_sku() != '' && get_option( 'ywraq_show_sku' ) == 'yes' ) {
                        $title .= apply_filters( 'ywraq_sku_label', __( ' SKU:', 'yith-woocommerce-request-a-quote' ) ) . $_product->get_sku();
                    }
                    ?>
                    <a href="<?php echo $_product->get_permalink() ?>"><?php echo $title ?></a>
                    <?php
                    // Meta data

                    $item_data = array();

                    // Variation data
                    if ( ! empty( $raq['variation_id'] ) && is_array( $raq['variations'] ) ) {

                        foreach ( $raq['variations'] as $name => $value ) {
                            $label = '';

                            if ( '' === $value ) {
                                continue;
                            }

                            $taxonomy = wc_attribute_taxonomy_name( str_replace( 'attribute_pa_', '', urldecode( $name ) ) );

                            // If this is a term slug, get the term's nice name
                            if ( taxonomy_exists( $taxonomy ) ) {
                                $term = get_term_by( 'slug', $value, $taxonomy );
                                if ( ! is_wp_error( $term ) && $term && $term->name ) {
                                    $value = $term->name;
                                }
                                $label = wc_attribute_label( $taxonomy );

                            } else {

                                if ( strpos( $name, 'attribute_' ) !== false ) {
                                    $custom_att = str_replace( 'attribute_', '', $name );

                                    if ( $custom_att != '' ) {
                                        $label = wc_attribute_label( $custom_att );
                                    } else {
                                        $label = $name;
                                    }
                                }

                            }

                            $item_data[] = array(
                                'key'   => $label,
                                'value' => $value
                            );
                        }
                    }

                    $item_data = apply_filters( 'ywraq_request_quote_view_item_data', $item_data, $raq, $_product, $show_price );


                    // Output flat or in list format
                    if ( sizeof( $item_data ) > 0 ) {
                        foreach ( $item_data as $data ) {
                            echo esc_html( $data['key'] ) . ': ' . wp_kses_post( $data['value'] ) . "\n";
                        }
                    }


                    ?>
                </td>
                
                <td class="product-price" data-title="<?php _e( 'Price', 'woocommerce' ); ?>">
                    <?php
                        echo apply_filters( 'woocommerce_cart_item_price', WC()->cart->get_product_price( $_product ), $cart_item, $cart_item_key );
                    ?>
                </td>


                <td class="product-quantity">
                    <?php
                    $product_quantity = woocommerce_quantity_input( array(
                        'input_name'  => "raq[{$key}][qty]",
                        'input_value' => apply_filters( 'ywraq_quantity_input_value', $raq['quantity'] ),
                        'max_value'   => apply_filters( 'ywraq_quantity_max_value', $_product->backorders_allowed() ? '' : $_product->get_stock_quantity(), $_product ),
                        'min_value'   => apply_filters( 'ywraq_quantity_min_value', 0, $_product )
                    ), $_product, false );

                    echo $product_quantity;
                    ?>
                </td>

                <?php if ( get_option( 'ywraq_hide_total_column', 'yes' ) == 'no' ): ?>
                    <td class="product-subtotal">
                        <?php
                            $total += apply_filters( 'yith_ywraq_product_price', $_product->get_display_price( '', $raq[ 'quantity' ] ), $_product, $raq );
                            $price = apply_filters( 'yith_ywraq_product_price_html', WC()->cart->get_product_subtotal( $_product, $raq[ 'quantity' ] ), $_product, $raq );
                            echo apply_filters( 'yith_ywraq_hide_price_template', $price, $_product->id, $raq);
                        ?>
                    </td>
                <?php endif ?>
            </tr>
            <?php do_action( 'ywraq_after_request_quote_view_item', $raq_content, $key ); ?>
        <?php endforeach ?>
        <!--
            <?php if ( get_option( 'ywraq_hide_total_column', 'yes' ) == 'no' ): ?>
            <tr>
                <td  colspan="3">
                </td>
                <th>
                    <?php _e( 'Total:', 'yith-woocommerce-request-a-quote') ?>
                </th>
                <td class="raq-totals">
                    <?php echo wc_price( $total ); ?>
                </td>
            </tr>
            <?php endif ?>
            -->

我认为这会有所帮助,我已经尝试用卡片的循环替换请求页面的循环,但它只创建了 2 个购物车循环,这不是我所期望的......也许有人比我更擅长 PHP有什么想法吗?

【问题讨论】:

  • 注意:您的问题很复杂,因为您的问题是真正的开发事务。问题是当使用一些插件作为 YITH 插件时(在这种情况下),一方面帮助您更改/添加一些功能/行为,另一方面也将迫使您留在预定义的更改/行为中。大多数情况下,它们没有提供正确的扩展方式,您需要在插件文件中添加一些自定义更改,以使它们与其他文件(或自定义代码)一起工作。 您可能必须编写自己的插件才能按预期工作

标签: php wordpress class woocommerce


【解决方案1】:

经过多次尝试和短暂的夜晚,我找到了一个不太好的“解决方案”,但一切正常!

所以我修改了那些页面:

request-quote-view.php(报价页面的表格)

我在表中不同产品的 foreach 之前添加了这个小循环。

$arr_discount = array();
foreach(YITH_WC_Dynamic_Pricing() as $key => $raq ){
    foreach($raq as $arr => $data){
        array_push($arr_discount, $data);
    }
}

通过这个循环,我将有关动态定价的不同信息放入一个新数组中

那么,在 foreach 循环的开始,我有另一个循环,它将我们之前得到的内容放入引用的数组中

foreach($arr_discount as $test){
    foreach($test['apply_to_products_list'] as $id){
        if($id == $raq['product_id']){
            foreach($test['rules'] as $rule){
                $raq['arr_dsct'][] = $rule;
            }
        }
    }
}

最后在 td class=subtotal 我将内容替换为

if(isset($raq['arr_dsct']) == ''){
    $total += apply_filters( 'yith_ywraq_product_price', $_product->get_display_price( '', $raq[ 'quantity' ] ), $_product, $raq );
    $price = apply_filters( 'yith_ywraq_product_price_html', WC()->cart->get_product_subtotal( $_product, $raq[ 'quantity' ] ), $_product, $raq );
    echo apply_filters( 'yith_ywraq_hide_price_template', $price, $_product->id, $raq); 
} else {
    $count = count($raq['arr_dsct']);
    for($i = 0; $i < $count; $i++) {
        if( $raq['quantity'] >= $raq['arr_dsct'][$i][min_quantity] AND $raq['quantity'] <= $raq['arr_dsct'][$i][max_quantity] ){
            $price = $_product->price - $_product->price * $raq['arr_dsct'][$i][discount_amount];
            $total = round($raq['quantity'] * $price, 2);
            $totale = '<span class="woocommerce-Price-amount amount">'. $total .'<span class="woocommerce-Price-currencySymbol">€</span></span>';
        } elseif ( $raq['quantity'] < $raq['arr_dsct'][0][min_quantity] ) {
            $total += apply_filters( 'yith_ywraq_product_price', $_product->get_display_price( '', $raq[ 'quantity' ] ), $_product, $raq );
            $price = apply_filters( 'yith_ywraq_product_price_html', WC()->cart->get_product_subtotal( $_product, $raq[ 'quantity' ] ), $_product, $raq );
            $totale = apply_filters( 'yith_ywraq_hide_price_template', $price, $_product->id, $raq); 
        }
    }
    echo $totale;
}

request-quote-table.php(邮件表)

对于邮件页面,它是完全相同的答案,只有一个区别,产品的数组对象命名为 $item 而不是 $raq 所以它需要一些修改

如果您想了解更多关于不同代码的位置的信息,有两张图片可以显示它!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多