Woocommerce计算:数量按钮* get_meta('_ value')=有效值

问题描述 投票:0回答:1

这里我们正在谈论以散装单位/单位/或千克销售产品(特别是糕点或食品)。>>

所有度量值都是已知的重量和数量,因此我们必须能够估计以千克为单位的哪种产品将具有多少个单位。

因此,我创建了一个产品示例,仅用于woocommerce中的测试,我设法实现了我的想法,即使用有用的代码found here来估算每公斤的最小数量(或任何单位)。

// Backend: Add and display a custom field for simple and variable products

add_action( 'woocommerce_product_options_general_product_data', 'add_custom_price_field_to_general_product_data' );
function add_custom_price_field_to_general_product_data() {
    global $product_object;

    echo '<div class="options_group hide_if_external">';

    woocommerce_wp_text_input(array(
        'id'          => '_min_unit_price',
        'label'       => __('Min Unit price', 'woocommerce') . ' (' . get_woocommerce_currency_symbol() . ')',
        'description' => __('Enter the minimum unit price here.', 'woocommerce'),
        'desc_tip'    => 'true',
        'value'       => str_replace('.', ',', $product_object->get_meta('_min_unit_price') ),
        'data_type'   => 'price'
    ));


     // My custom field "Min price unit prefix"
    woocommerce_wp_text_input(array(
        'id'          => '_min_unit_prefix',
        'label'       => __('Min Unit prefix', 'woocommerce') . ' (' . get_woocommerce_currency_symbol() . ')',
        'description' => __(' Enter prefix unit price here.', 'woocommerce'),
        'desc_tip'    => 'true',
        'value'       => str_replace('.', ',', $product_object->get_meta('_min_unit_prefix') ),
        'data_type'   => 'text'
    ));


    // My custom field "Estimated quantity"
    woocommerce_wp_text_input(array(
        'id'          => '_estimated_quantity',
        'label'       => __('Estimated Quantity', 'woocommerce') . ' (' . get_woocommerce_currency_symbol() . ')',
        'description' => __('Enter the quantity per (kg) here.', 'woocommerce'),
        'desc_tip'    => 'true',
        'value'       => str_replace('.', ',', $product_object->get_meta('_estimated_quantity') ),
        'data_type'   => 'pcs'
    ));


    echo '</div>';
}

// Backend: Save the custom field value for simple and variable products
add_action( 'woocommerce_admin_process_product_object', 'save_product_custom_price_field', 10, 1 );
function save_product_custom_price_field( $product ) {

    if ( isset($_POST['_min_unit_price']) ) {
        $product->update_meta_data( '_min_unit_price', wc_clean( wp_unslash( str_replace( ',', '.', $_POST['_min_unit_price'] ) ) ) );
    }

     // my field value "Estimated quantity"
     if ( isset($_POST['_estimated_quantity']) ) {
        $product->update_meta_data( '_estimated_quantity', wc_clean( wp_unslash( str_replace( ',', '.', $_POST['_estimated_quantity'] ) ) ) );
    }

     // my field value  "Min price unit prefix"
     if ( isset($_POST['_min_unit_prefix']) ) {
        $product->update_meta_data( '_min_unit_prefix', wc_clean( wp_unslash( str_replace( ',', '.', $_POST['_min_unit_prefix'] ) ) ) );
    }


}

// Frontend variable products: Display the min price with "From" prefix label
add_filter( 'woocommerce_variable_price_html', 'custom_min_unit_variable_price_html', 10, 2 );
function custom_min_unit_variable_price_html( $price, $product ) {

    if( $min_unit_price = $product->get_meta('_min_unit_price') ){
        $price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $min_unit_price ) ) );
        $price .= $product->get_meta('_min_unit_prefix');
    }
    return $price;
}

// Frontend simple products: Display the min price with "From" prefix label
add_filter( 'woocommerce_get_price_html', 'custom_min_unit_product_price_html', 10, 2 );
function custom_min_unit_product_price_html( $price, $product ) {
    if( $product->is_type('simple') && $min_unit_price = $product->get_meta('_min_unit_price') ){
        $price  = wc_price( wc_get_price_to_display( $product, array( 'price' => $min_unit_price ) ) );
        $price .= $product->get_meta('_min_unit_prefix');
    }
    return $price;
}



// Display the cart item weight in cart and checkout pages


add_filter( 'woocommerce_get_item_data', 'display_custom_item_data', 10, 2 );
function display_custom_item_data( $cart_item_data, $cart_item ) {

    if ( $cart_item['data']->get_weight() > 0 ){
        $cart_item_data[] = array(
            'name' => __( 'Weight ', 'woocommerce' ),
            'value' =>  ( $cart_item['quantity'] * $cart_item['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit')
        );
    }

        // Display the cart item "estimated quantity" in cart and checkout pages
        if ( $cart_item['data']->get_meta('_estimated_quantity') ){
        $cart_item_data[] = array(
            'name' => __( 'Estimated quantity ', 'woocommerce' ),
            'value' =>  ( $cart_item['quantity'] * $cart_item['data']->get_meta('_estimated_quantity') )
        );
    }


    return $cart_item_data;
}

// Save and Display the order item weight (everywhere)
add_action( 'woocommerce_checkout_create_order_line_item', 'display_order_item_data', 20, 4 );
function display_order_item_data( $item, $cart_item_key, $values, $order ) {
    if ( $values['data']->get_weight() > 0 ){
        $item->update_meta_data( __( 'weight ', 'woocommerce' ), ( $cart_item['quantity'] * $cart_item['data']->get_weight() )  . ' ' . get_option('woocommerce_weight_unit') );
    }
}

我把它放在snippet中。之后,我开始阅读并尝试理解和遵循代码背后的逻辑。然后我只复制了几行..一些复制/粘贴.. (add estimated qty and min prefix field)

我检查了所有事情(对我而言)是否合乎逻辑,然后我保存并激活了代码段。

[我告诉你,到目前为止,我从未接触过任何php代码,我什至不了解基本知识。我期望到处都有数以百万计的致命错误。

刷新后..奇迹般地everything works well as expected

这是一个很好的开始,所以现在我想我们有一个很好的基础可以具体讨论,现在人们可以加入其中并为我提供一些帮助吗?

验证该代码有效,并且在以后的woocommerce更新中不会造成问题。

现在,我试图将这两行信息放入the product page中。通过简码?还是再次使用PHP?

还改进代码并在估计数量上添加前缀。

欢迎任何帮助或建议和改进。

我希望这对您有意义。

有关信息,我使用全新安装的wordpress + elementor + elementor hello主题+ woocommerce。

更新26/11/2019

我今天尝试了一些简码。我有所有可能的php错误。我想我知道该怎么做,但是我没有正确的技术。总结一下:在购物车页面上,一切都很棒。客户更改数量,一切都会自动更新,非常完美。

我想要的也是最理想的是在产品页面中显示相同的信息实时

客户单击数量后,所有内容都会在实时中更新

该值是已知的:get_meta ('_ estimated_quantity'),它的位置在这里

'value' => ($ cart_item ['quantity'] * $ cart_item ['data'] -> get_meta ('_ estimated_quantity'))

我只需要知道如何定位“数量众多的布顿”

通过jquery或脚本或简码。

我做了small photoshop以确保我们了解我们在这里所说的内容。

我希望这对你有意义。

[这里是谈论以散装单位/单位/或千克为单位的产品(尤其是糕点或食品)的销售。所有测量值都是已知的重量和数量,因此我们必须能够估计出哪种产品...

php wordpress woocommerce code-snippets
1个回答
0
投票

欢迎使用堆栈溢出!

回答您的问题:

© www.soinside.com 2019 - 2024. All rights reserved.