显示Woocommerce

时间:2018-03-12 06:57:55

标签: php wordpress woocommerce volume shipping

我为订购家具容器的批发客户使用woocommerce - 通常是40英尺集装箱,体积为68立方米。

有没有办法可以在网站上的某个地方展示 - 也许在标题区域有一个显示产品总数m3的方框?我需要在客户达到68m3时向他们展示,以便他们知道他们已经装满了容器。

如果客户试图提交低于68m3的订单,并且向他们表明他们的容器中仍有剩余空间,是否有办法点亮消息?

感谢任何帮助。

2 个答案:

答案 0 :(得分:4)

这是一个自动获取Woocommerce中设置的尺寸单位并将进行购物车总量计算的功能:

function get_cart_volume(){
    // Initializing variables
    $volume = $rate = 0;

    // Get the dimetion unit set in Woocommerce
    $dimension_unit = get_option( 'woocommerce_dimension_unit' );

    // Calculate the rate to be applied for volume in m3
    if ( $dimension_unit == 'mm' ) {
        $rate = pow(10, 9);
    } elseif ( $dimension_unit == 'cm' ) {
        $rate = pow(10, 6);
    } elseif ( $dimension_unit == 'm' ) {
        $rate = 1;
    }

    if( $rate == 0 ) return false; // Exit

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item) { 
        // Get an instance of the WC_Product object and cart quantity
        $product = $cart_item['data'];
        $qty     = $cart_item['quantity'];

        // Get product dimensions  
        $length = $product->get_length();
        $width  = $product->get_width();
        $height = $product->get_height();

        // Calculations a item level
        $volume += $length * $width * $height * $qty;
    } 
    return $volume / $rate;
}

代码进入活动子主题(或活动主题)的function.php文件。经过测试并正常工作。

示例用法输出:

echo __('Cart volume') . ': ' . get_cart_volume() . ' m3';

答案 1 :(得分:1)

您可以尝试这样的事情:

<?php
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    $cart_prods_m3 = array();

        //LOOP ALL THE PRODUCTS IN THE CART
        foreach($items as $item => $values) { 
            $_product =  wc_get_product( $values['data']->get_id());
            //GET GET PRODUCT M3 
            $prod_m3 = $_product->get_length() * 
                       $_product->get_width() * 
                       $_product->get_height();
            //MULTIPLY BY THE CART ITEM QUANTITY
            //DIVIDE BY 1000000 (ONE MILLION) IF ENTERING THE SIZE IN CENTIMETERS
            $prod_m3 = ($prod_m3 * $values['quantity']) / 1000000;
            //PUSH RESULT TO ARRAY
            array_push($cart_prods_m3, $prod_m3);
        } 

    echo "Total of M3 in the cart: " . array_sum($cart_prods_m3);
?>

请参阅WC()文档:https://docs.woocommerce.com/document/class-reference/