Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WooCommerce-修改后的购物车重量不会影响运输成本_Php_Wordpress_Woocommerce_Cart_Hook Woocommerce - Fatal编程技术网

Php WooCommerce-修改后的购物车重量不会影响运输成本

Php WooCommerce-修改后的购物车重量不会影响运输成本,php,wordpress,woocommerce,cart,hook-woocommerce,Php,Wordpress,Woocommerce,Cart,Hook Woocommerce,晚上好。 我写了一些代码来改变我购物车中某些特定物品的总重量(由于快递员的特殊要求)。在你问之前。。。不,我不能修改单件重量,因为我必须计算包装的特殊重量。是的,说来话长 我的代码如下所示(简化版): 当我回显调用的值时 echo WC()->cart->cart\u contents\u weight似乎一切正常,但由于某些原因,运输成本没有改变。 我正在使用“WooCommerce基于重量的配送”插件来管理我的配送成本 为什么插件忽略了新的权重 感谢由于WooCommerce基于重量的配送是

晚上好。 我写了一些代码来改变我购物车中某些特定物品的总重量(由于快递员的特殊要求)。在你问之前。。。不,我不能修改单件重量,因为我必须计算包装的特殊重量。是的,说来话长

我的代码如下所示(简化版):

当我回显调用的值时
echo WC()->cart->cart\u contents\u weight似乎一切正常,但由于某些原因,运输成本没有改变。
我正在使用“WooCommerce基于重量的配送”插件来管理我的配送成本

为什么插件忽略了新的权重


感谢

由于WooCommerce基于重量的配送是一个第三方插件,因此有不同的方法根据购物车重量计算成本变化

下面是改变购物车重量的两种方法

1)更改购物车内容总重量:

add_filter( 'woocommerce_cart_contents_weight', 'filter_wc_cart_contents_weight', 10000 );
function filter_wc_cart_contents_weight( $weight ) {
    $condition    = true; // Only for testing
    $extra_weight = 1.5;

    // Loop through cart items
    foreach(WC()->cart->get_cart() as $cart_item ) {
        $quantity = $cart_item['quantity']; // If needed

        if( $condition ) {
            $weight += $extra_weight;
        }
    }
    return $weight;
}

// For testing: display the total weight after order total in cart page (Ajax updated)
add_action( 'woocommerce_cart_totals_after_order_total', 'display_wc_cart_total_weight_after_order_total' );
function display_wc_cart_total_weight_after_order_total() {
    ?>
    <tr class="order-total">
        <th><?php esc_html_e( 'Total weight', 'woocommerce' ); ?></th>
        <td data-title="<?php esc_attr_e( 'Total weight', 'woocommerce' ); ?>"><?php echo wc_format_weight( WC()->cart->get_cart_contents_weight() ); ?></td>
    </tr>
    <?php
}
add_action( 'woocommerce_before_calculate_totals', 'conditionally_change_tax_class', 10000 );
function conditionally_change_tax_class( $cart ){
    if ( is_admin() && ! defined( 'DOING_AJAX' ) )
        return;

    // Required since Woocommerce version 3.2 for cart items properties changes
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 )
        return;

    $condition    = true; // Only for testing
    $extra_weight = 1.5;

    // Looo through our specific cart item keys
    foreach ( $cart->get_cart() as $cart_item ) {
        // get product weight
        $weight = (float) $cart_item['data']->get_weight();

        // Set the new product weight
        $cart_item['data']->set_weight( $weight + ( $extra_weight / $cart_item['quantity'] ) );
    }
}

// For testing display the total weight after order total
add_action( 'woocommerce_cart_totals_after_order_total', 'display_wc_cart_total_weight_after_order_total' );
function display_wc_cart_total_weight_after_order_total() {
    ?>
    <tr class="order-total">
        <th><?php esc_html_e( 'Total weight', 'woocommerce' ); ?></th>
        <td data-title="<?php esc_attr_e( 'Total weight', 'woocommerce' ); ?>"><?php echo wc_format_weight( WC()->cart->get_cart_contents_weight() ); ?></td>
    </tr>
    <?php
}
添加过滤器('woocommerce\u cart\u contents\u weight','filter\u wc\u cart\u contents\u weight',10000);
功能过滤器\厕所\购物车\内容\重量($weight){
$condition=true;//仅用于测试
$额外重量=1.5;
//循环浏览购物车项目
foreach(WC()->cart->get_cart()作为$cart_项目){
$quantity=$cart_item['quantity'];//如果需要
如果($条件){
$weight+=$extra_weight;
}
}
返回$weight;
}
//用于测试:在购物车页面中显示订单合计后的总重量(Ajax更新)
添加操作('WOOMerce\u cart\u totals\u after\u order\u total'、'display\u wc\u cart\u total\u weight\u after\u order\u total');
功能显示\u wc\u cart\u总重量\u订单后\u总重量(){
?>
如果您正在使用此插件

@如果您提前更改购物车数据,LoicTheAztec答案可能会起作用
WC()->cart->calculate\u shipping()
使用钩子
woocommerce\u checkout\u update\u order\u review

add_action( 'woocommerce_checkout_update_order_review', 'conditionally_change_tax_class', 10000 );

来源:

谢谢。你的代码是……我的。我发现问题出在我用来根据重量计算运费的插件中。我快速查看了代码,它似乎没有查找购物车的重量;它将所有单个产品的重量相加,这对我来说是一个很大的问题。我需要在指定的基础上添加重量c数量(箱),因此我无法将额外的重量分摊到单个产品上。我联系了插件的开发人员。@SimoneConti我已更改了我的答案…请检查。现在,正如您所说,他们对产品重量进行了计算,无法进行您希望的更改。谢谢您的回复。我正在尝试修改插件的代码,以反映我的需要。当然,我以后将无法更新它。。。
add_action( 'woocommerce_checkout_update_order_review', 'conditionally_change_tax_class', 10000 );