【问题标题】:Woocommerce - custom functions runs only on update cartWoocommerce - 自定义功能仅在更新购物车上运行
【发布时间】:2016-12-14 11:52:27
【问题描述】:

如果产品不在购物车中或购物车中该产品的数量少于 6,我编写了一个非常小的函数来禁用安装作为一种方法(来自表格运费插件)。

这有效,但只有当我点击“更新购物车”按钮时才有效,例如,当我点击购物车时。

这是函数,直接来自我的自定义主题中的 function.php 文件:

function disable_installation_for_less_than( $rates ) {
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    $installation =  $rates['table_rate_shipping_installation'];

    foreach ( $items as $item => $values ) {
        $productID       = $values['product_id'];
        $productQuantity = $values['quantity'];
        unset( $rates['table_rate_shipping_installation'] );

        if ( $productID == 2412 ) {
            if ( $productQuantity < 6 ) {
                unset( $rates['table_rate_shipping_installation'] );
            } else {
                array_push($rates, $installation);
            }
        }
    }

    return $rates;
}

add_filter( 'woocommerce_package_rates', 'disable_installation_for_less_than', 10, 3 );

知道为什么吗?我用错了钩子吗? 感谢您的帮助

另外,与其取消设置安装并仅在需要时重新设置,有没有更好的方式来表示“如果此产品不在购物车中”然后将其删除?

谢谢

【问题讨论】:

  • 当您说您“点击购物车”(当数据未更新时)时,您的问题是什么意思?
  • 我点击菜单项进入购物车(或者我手动更改网址进入购物车页面)
  • 所以只是在购物车页面上处理 ajax 操作……对……我必须使用 Table Rate Shipping 插件测试您的代码。这需要一些时间。
  • 好的,谢谢。慢慢来,不要着急

标签: wordpress woocommerce hook-woocommerce


【解决方案1】:

好的,我设法解决了这个问题:

function disable_installation_for_less_than( $rates ) {
    global $woocommerce;
    $items = $woocommerce->cart->get_cart();
    $showInstallation= false;
    foreach ( $items as $item => $values ) {
        $productID       = $values['product_id'];
        $productQuantity = $values['quantity'];
        if ( $productID == 2412 ) {
            $showInstallation= true;
            if ( $productQuantity < 6 ) {
                unset( $rates['table_rate_shipping_installation'] );
            }
        }
    }

    if ($showInstallation== false){
        unset( $rates['table_rate_shipping_installation'] );
    }

    return $rates;
}

add_filter( 'woocommerce_package_rates', 'disable_installation_for_less_than', 10, 2 );

现在可以了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-20
    • 2018-05-18
    • 2021-03-12
    • 2019-08-07
    • 1970-01-01
    • 2021-12-10
    • 2015-12-10
    • 2018-01-09
    相关资源
    最近更新 更多