首页 > 解决方案 > woocommerce 购物车具有来自具有多个价格的相同产品的多个小计

问题描述

我正在尝试建立一个产品有多种价格的商店,每个供应商都有一个价格。当我选择产品时,在购物车中显示每个供应商的价格和小计。例如,我进入商店,然后在不知道价格的情况下选择糖和牛奶,继续购物车,在购物车页面中我可以看到:Sugar- Store 1 $10- Store 2 $20 / Milk - Store 1 $20 - Store 2 $30- --小计商店 1=30 美元商店 2=50 美元。就是这样,我不需要进行结帐,只是比较同等产品的商店价格。我尝试了可变产品,我可以在购物车上显示价格,但我不能通过可变 id 制作多个小计数组。有任何想法吗?

标签: phpwordpresswoocommerceproductcart

解决方案


well, maybe this answer will help someone else in the future, i figure it out, i leave the function here just in case

   

 // Muestra los custom fields en el carrito
add_filter( 'woocommerce_get_item_data', 'super_custom_field', 10, 2 );
function super_custom_field( $cart_data, $cart_item ) 
{
    $items_super = array();

    if( !empty( $cart_data ) )
        $custom_items = $cart_data;

    // Los busca desde el id
    $product_id = $cart_item['product_id'];
    
    $supermercado_1 = get_field('super_1',7);
    $supermercado_2 = get_field('super_2',7);   
    $supermercado_3 = get_field('super_3',7);       

    if( $precio_1 = get_post_meta( $product_id, 'precio_1', true ))
    if( $precio_2 = get_post_meta( $product_id, 'precio_2', true ))
    if( $precio_3 = get_post_meta( $product_id, 'precio_3', true ))  
        
        $items_super[] = array(
            'name'      => __( 'Precios en supermercados', 'woocommerce'),
            'display'   => '<div>
                            <p class="super1 supermercados"><strong>' . ($supermercado_1). '</strong> <span>$' . ($precio_1). '</span></p>
                            <p class="super2 supermercados"><strong>' . ($supermercado_2). '</strong> <span>$' . ($precio_2). '</span></p>
                            <p class="super3 supermercados"><strong>' . ($supermercado_3). '</strong> <span>$' . ($precio_3). '</span></p>
                            </div>',

        );

    return $items_super;
}

//Suma los custom field de precio 1
//Elige el lugar donde lo muestra
add_filter( 'woocommerce_after_cart_contents', 'suma_precio_1', 10, 2 );
//este es despues del carro antes del checkout
add_action( 'woocommerce_cart_totals_before_shipping', 'suma_precio_1', 20 );
//este es en el checkout
add_action( 'woocommerce_review_order_before_shipping', 'suma_precio_1', 20 );


function suma_precio_1() {
    $total_1 = 0;

    $supermercado_1 = get_field('super_1',7);

    // Busca en el loop del carro el cutom field en cada producto y los suma y multiplica por la cantidad
    foreach( WC()->cart->get_cart() as $cart_item ){
        $precio_super_1 = (float) get_post_meta( $cart_item['product_id'], 'precio_1', true );
        $total_1  += $precio_super_1 * $cart_item['quantity'];
    }
    if( $total_1 > 0 ){

        // The Output
        echo ' <tr class="super1">
            <td colspan="5"><p><strong>' .$supermercado_1.  '</strong></td> <td colspan="" style="text-align:right;"><span>$' . number_format($total_1, 2) . '</span></p></td>
        </tr>';
    }
}


//Suma los custom field de precio 1
//Elige el lugar donde lo muestra
add_filter( 'woocommerce_after_cart_contents', 'suma_precio_2', 10, 2 );
//este es despues del carro antes del checkout
add_action( 'woocommerce_cart_totals_before_shipping', 'suma_precio_2', 20 );
//este es en el checkout
add_action( 'woocommerce_review_order_before_shipping', 'suma_precio_2', 20 );


function suma_precio_2() {
    $total_2 = 0;

    $supermercado_2 = get_field('super_2', 7);

    // Busca en el loop del carro el cutom field en cada producto y los suma y multiplica por la cantidad
    foreach( WC()->cart->get_cart() as $cart_item ){
        $precio_super_2 = (float) get_post_meta( $cart_item['product_id'], 'precio_2', true );
        $total_2  += $precio_super_2 * $cart_item['quantity'];
    }
    if( $total_2 > 0 ){

        // The Output
        echo ' <tr class="super2">
            <td colspan="5"><p><strong>' .$supermercado_2.  '</strong></td> <td colspan="" style="text-align:right;"><span>$' . number_format($total_2, 2) . '</span></p></td>
        </tr>';
    }
}
//Suma los custom field de precio 1
//Elige el lugar donde lo muestra
add_filter( 'woocommerce_after_cart_contents', 'suma_precio_3', 10, 2 );
//este es despues del carro antes del checkout
add_action( 'woocommerce_cart_totals_before_shipping', 'suma_precio_3', 20 );
//este es en el checkout
add_action( 'woocommerce_review_order_before_shipping', 'suma_precio_3', 20 );


function suma_precio_3() {
    $total_3 = 0;

    $supermercado_3 = get_field('super_3', 7);

    // Busca en el loop del carro el custom field en cada producto y los suma y multiplica por la cantidad
    foreach( WC()->cart->get_cart() as $cart_item ){
        $precio_super_3 = (float) get_post_meta( $cart_item['product_id'], 'precio_3', true );
        $total_3  += $precio_super_3 * $cart_item['quantity'];
    }
    if( $total_3 > 0 ){

        // The Output
        echo ' <tr class="super3">
            <td colspan="5"><p><strong>' .$supermercado_3.  '</strong></td> <td colspan="" style="text-align:right;"><span>$' . number_format($total_3, 2) . '</span></p></td>
        </tr>';
    }
}

推荐阅读