动态定价并请求报价woocommerce

时间:2017-01-15 18:29:17

标签: php wordpress class woocommerce

Hello World!

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

  

动态定价&请求由YITH

制作的报价插件

这些插件一切都还可以,但我正在做的电子商务网站都可以引用和推车,这就是我使用Request a Quote插件的原因,但我所做的不同的动态价格是&#39 ;在我的报价页面中没有visibe,但它们在我的购物车页面中。

在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中比我更好可以有个主意吗?

1 个答案:

答案 0 :(得分:1)

经过多次尝试&amp;一个短暂的夜晚,我找到了一个解决方案&#34;不是很好,但一切都很完美!

所以我修改了这些页面:

  

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 所以需要进行一些修改

如果您想了解有关应该是不同代码的地方的更多信息,则有两个图像显示它!

enter image description here

enter image description here