【问题标题】:Adding cart fees based on product’s category and quantity using php (WooCommerce 4.5.1 )使用 php (WooCommerce 4.5.1) 根据产品的类别和数量添加购物车费用
【发布时间】:2020-09-10 19:11:02
【问题描述】:

我已尝试寻找此问题的答案,但大部分代码已过时或不完全符合我的要求。以下是场景——当人们从我们的商店订购数字图像版权时,他们必须支付使用费和复制费。使用费根据几个因素而变化,并且不征税 - 这将是添加到购物车中的产品。复印费是每张图像的固定、应税费用。我希望在添加“图片”类别的产品时自动将其添加到我的购物车中。

我在 WPFactory 上找到了根据运行良好的类别添加费用的代码。

add_action( 'woocommerce_cart_calculate_fees', 'wpf_wc_add_cart_fees_by_product_category' );
if ( ! function_exists( 'wpf_wc_add_cart_fees_by_product_category' ) ) {
    /**
     * wpf_wc_add_cart_fees_by_product_category.
     */
    function wpf_wc_add_cart_fees_by_product_category( $cart ) {
        // You need to enter your fees here, in `category slug` => `fee amount` format
        $fees = array(
            'images'    => 25,
        );
        foreach ( $cart->get_cart() as $cart_item_key => $values ) {
            $product_categories = get_the_terms( $values['product_id'], 'product_cat' );
            if ( $product_categories && ! is_wp_error( $product_categories ) ) {
                $product_categories = wp_list_pluck( $product_categories, 'slug' );
                foreach ( $product_categories as $product_category ) {
                    if ( ! empty( $fees[ $product_category ] ) ) {
                        $name      = 'Digital Image Copy Fee'
                        $amount    = $fees;
                        $taxable   = true;
                        $tax_class = '';
                        $cart->add_fee( $name, $amount, $taxable, $tax_class );
                    }
                }
            }
        }
    }
}

问题是,它只添加一次费用,我需要它根据购物车中类别的商品总数乘以费用。我试图计算 $qty 并将 $fee 乘以 $qty 但我的 php 非常生锈而且我没有成功。任何帮助将不胜感激。

【问题讨论】:

    标签: php wordpress woocommerce


    【解决方案1】:

    解决办法如下:

    add_action( 'woocommerce_cart_calculate_fees', 'wpf_wc_add_cart_fees_by_product_category', 10, 1 );
    function wpf_wc_add_cart_fees_by_product_category( $cart ) {
    
        $product_cat_id = 119; // set "image" cat id
        $total_qty = 0; // initialize qty products
        $fee = 25; // set fee amount
    
        foreach ( $cart->get_cart() as $cart_item ) {
            $product_id = $cart_item['product_id'];
            // get all category ids by product id
            $categories = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) );
            // if product has "image" category
            if ( in_array( $product_cat_id, $categories ) )  {
                // get quantity
                $total_qty += $cart_item['quantity'];
            }
        }
        
        if ( $total_qty > 0 ) {
            $tax = true; // set "false" for no tax
            $cart->add_fee( __( "Digital Image Copy Fee", "woocommerce" ), $fee * $total_qty, $tax ); // add custom fee
        }
    
    }
    

    代码进入你的主题的functions.php文件,它可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      相关资源
      最近更新 更多