1
WooCommerceでは、私はthis code from Remicorsonを使用していますが、動作していないため、どこに問題があるのかわかりません。ここでWooCommerceのカスタムフィールドの値をカートに追加する合計
は私のコードです:
// Display Fields
add_action('woocommerce_product_options_general_product_data', 'woo_add_custom_general_fields');
// Save Fields
add_action('woocommerce_process_product_meta', 'woo_add_custom_general_fields_save');
function woo_add_custom_general_fields()
{
global $woocommerce, $post;
echo '<div class="options_group">';
// Custom fields will be created here...
woocommerce_wp_text_input(array(
'id' => '_number_field',
'label' => __('Environmental fee', 'woocommerce') ,
'placeholder' => '',
'description' => __('Enter the custom value here.', 'woocommerce') ,
'type' => 'number',
'custom_attributes' => array(
'step' => 'any',
'min' => '0'
)
));
echo '</div>';
}
function woo_add_custom_general_fields_save($post_id)
{
// Number Field
$woocommerce_number_field = $_POST['_number_field'];
if (!empty($woocommerce_number_field)) update_post_meta($post_id, '_number_field', esc_attr($woocommerce_number_field));
}
function woo_add_cart_fee()
{
global $woocommerce;
$prod_fee = get_post_meta($item['product_id'], '_number_field', true);
// After that you need to add condition or do calculation in.
function add_custom_fees(WC_Cart $cart)
{
$fees = 0;
$prod_fee = get_post_meta($item['product_id'], '_number_field', true);
foreach($cart->get_cart() as $item) {
$fees+= $item['quantity'] * $prod_fee;
}
if ($fees != 0) {
$cart->add_fee('Handling fee', $fees);
}
}
$woocommerce->cart->add_fee('Handling', $fee, true, 'standard');
}
add_action('woocommerce_cart_calculate_fees', 'woo_add_cart_fee');
をその$料Sなし... おかげで@ LoicTheAztecその作業は完全に.. –