更新Woocommerce中数量变化的购物车项目自定义数据

时间:2022-09-01 07:24:46

I have added a custom field to the woocommerce product named 'Donation', which stores donation of the individual product. Then, I added line item meta named 'line_donation'. Now I need to update 'line_donation' on product quantity change and after clicking update cart Button like product total changes.

我在名为“捐赠”的woocommerce产品中添加了一个自定义字段,用于存储个别产品的捐赠。然后,我添加了名为'line_donation'的订单项元。现在,我需要更新产品数量更改的'line_donation',然后点击更新购物车按钮,就像产品总数更改一样。

function cfwc_add_custom_field_item_data( $cart_item_data, $product_id, $variation_id, $quantity ) {
    // Add the item data
    $cart_item_data['line_donation'] = get_post_meta($product_id,'donation', true);
    return $cart_item_data;
}
add_filter( 'woocommerce_add_cart_item_data', 'cfwc_add_custom_field_item_data', 10, 4 );

/**
 * Display the custom field value in the cart
 * @since 1.0.0
 */
function cfwc_cart_item_name( $name, $cart_item, $cart_item_key ) {
    if( isset( $cart_item['line_donation'] ) ) {
        $name .= sprintf(
            '<p>%s</p>',
            esc_html( $cart_item['line_donation'] )
        );
    }
    return $name;
}
add_filter( 'woocommerce_cart_item_name', 'cfwc_cart_item_name', 10, 3 );

function add_line_donation_to_order( $item, $cart_item_key, $values, $order ) {
    foreach( $item as $cart_item_key=>$values ) {
        if( isset( $values['line_donation'] ) ) {
            $item->add_meta_data( __( 'line_donation', 'woocommerce' ), $values['line_donation'], true );
        }
    }
}
add_action( 'woocommerce_checkout_create_order_line_item', 'add_line_donation_to_order', 10, 4 );

Any help is welcome.

欢迎任何帮助。

1 个解决方案

#1


1  

You just simply need to make a little change in your 2 last functions as follows:

您只需要对最后两个函数进行一些更改,如下所示:

add_filter( 'woocommerce_cart_item_name', 'cfwc_cart_item_name', 10, 3 );
function cfwc_cart_item_name( $name, $cart_item, $cart_item_key ) {
    if( isset( $cart_item['line_donation'] ) )
        $name .= '<p>' . $cart_item['line_donation'] * $cart_item['quantity'] . '</p>';

    return $name;
}

add_action( 'woocommerce_checkout_create_order_line_item', 'add_line_donation_to_order', 10, 4 );
function add_line_donation_to_order( $item, $cart_item_key, $values, $order ) {
    foreach( $item as $cart_item_key=>$values ) {
        if( isset( $values['line_donation'] ) ) {
            $item->add_meta_data( __( 'line_donation', 'woocommerce' ), $values['line_donation'] * $values['quantity'], true );
        }
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

#1


1  

You just simply need to make a little change in your 2 last functions as follows:

您只需要对最后两个函数进行一些更改,如下所示:

add_filter( 'woocommerce_cart_item_name', 'cfwc_cart_item_name', 10, 3 );
function cfwc_cart_item_name( $name, $cart_item, $cart_item_key ) {
    if( isset( $cart_item['line_donation'] ) )
        $name .= '<p>' . $cart_item['line_donation'] * $cart_item['quantity'] . '</p>';

    return $name;
}

add_action( 'woocommerce_checkout_create_order_line_item', 'add_line_donation_to_order', 10, 4 );
function add_line_donation_to_order( $item, $cart_item_key, $values, $order ) {
    foreach( $item as $cart_item_key=>$values ) {
        if( isset( $values['line_donation'] ) ) {
            $item->add_meta_data( __( 'line_donation', 'woocommerce' ), $values['line_donation'] * $values['quantity'], true );
        }
    }
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。