WooCommerceのカートでのカウントに問題があります。各商品に手数料フィールドを追加し、カート内の手数料の合計を劇的にカウントしたいと思います。私の調査によると、私は自分の製品にフィールドを作成しました。デモ-1
次のステップは、カート内のこのフィールドを数えることです。私もGoogleでこの問題を検索しましたが、劇的な機能ではなく固定料金をカウントするためのいくつかの解決策(Wordpress:カートに追加料金を追加)しか見つかりませんでした。
デモ-2
// 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 ) );
}
add_action( 'woocommerce_cart_calculate_fees','endo_handling_fee' );
function endo_handling_fee() {
global $woocommerce;
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;
$fee = 5.00;
$woocommerce->cart->add_fee( 'Handling', $fee, true, 'standard' );
}
小計列の各製品の料金(作成したカスタムフィールドから提供される値)をカウントするように関数を変更するにはどうすればよいですか?
今、私は次のコードを試しています。製品の価値をどうやってつかみ、変数にするかがカギだと思います。
add_action('woocommerce_cart_calculate_fees' , 'add_custom_fees');
function add_custom_fees( WC_Cart $cart ){
$fees = 0;
foreach( $cart->get_cart() as $item ){
$fees += $item[ 'quantity' ] * 0.08;
}
if( $fees != 0 ){
$cart->add_fee( 'Handling fee', $fees);
}
}
必要な関数は、カスタムフィールドの値を取得するためのget_post_metaです。
$prod_fee = get_post_meta($item['product_id'] , '_number_field', true);
次に、これを累積して、統合料金として表示できます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加