Woocommerce实时计算:数量x get_meta('_ value')=最终值

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

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

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

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

((1)

我将其放在Snippet中。

此后,我开始阅读并尝试理解和遵循代码背后的逻辑。

((2)

Add some fonctions.刚刚重复了几行..一些复制/粘贴..

((3)

尝试放置相同的功能在产品页面中(正在进行中,未找到解决方案)

更新27/11/2016

修订后的代码“我在结帐页面中发现一个[[内部服务器错误]

”]

我隔离了导致错误的代码

有人可以解释这部分有什么问题吗?

// 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') );
    }
}

工作部分

// 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'),
        'description' => __(' Enter prefix unit price here.', 'woocommerce'),
        'desc_tip'    => 'true',
        'value'       => str_replace('.', ',', $product_object->get_meta('_min_unit_prefix') ),
        'data_type'   => 'texte'
    ));


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

    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'] ) ) ) );
    }

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

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

}

// 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 subtotal', '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;
}

现在,我需要帮助来了解操作方式>

Estimated quantity: <? Php echo get_post_meta (get_the_ID (), '_estimated_quantity', true); ?>

可以Update in real time with the quantity of add_to_cart.

公式必须在jQuery或javascript中非常简单?

_ estimated_quantity *数量。

我现在所需要的。向客户显示以1公斤或xx公斤可以得到多少蛋糕(或其他东西)]

所以我在后端放入了15或xx(近似值)。

我希望这对您有意义。

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

向Snuwerd更新27/11/2019

感谢您尝试帮助我。我为您提供了有关如何集成代码的所有必要信息。 (初学者)

我专门使用elementor专业版和摘要。这样,我就拥有了我需要集成的所有内容(php,html,脚本,css等)。elementor是主题编辑器,因此不需要打开任何functions.php或其他文件。当然,主要原因是我必须通过导入/导出在其他项目上重用它们。对于主题,我从不使用任何主题。我只使用了hello elementor主题,它完全没有任何函数,脚本或CSS。

How I integrate codes php and jquery / javascript

Estimated quantity elements code

Qty add to cart elements code

更新28/11/2019

Here the screenshot

我将css容器命名为css,并将ID命名为“ snuwerd”。在chrome中,snuwerd隐藏在一堆疯狂的废话代码中。但我们可以看到一个ID“ snuwerd”。可以肯定的是,我使用yellowpencil来查看它是否有效并存在,并且可以选择目标。

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

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

欢迎使用堆栈溢出!

回答您的问题:

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