WooCommerce Sample plugin is adding full price when using custom option

No comments
Is your WooCommerce Sample e is adding full price of your product rathan than custome sample price than this is the solution for it


just copy and paste this code into the respective file

/wp-content/plugins/woocommerce-sample/woocommerce-sample.php

1)  add this code in init() function
//custome RI code start
add_action( 'woocommerce_before_calculate_totals','cp_add_custom_price', 10, 2);
function cp_add_custom_price( $cart_obj ) {
// This is necessary for WC 3.0+
if ( is_admin() && ! defined( 'DOING_AJAX' ) )
return;

foreach ( $cart_obj->get_cart() as $key => $value ) {
if($value['sample']){
$product_id = $value['product_id'];
$sample_price_mode = get_post_meta($product_id, 'sample_price_mode', true) ? get_post_meta($product_id, 'sample_price_mode', true) : 'default';
$sample_price = get_post_meta($product_id, 'sample_price', true) ? get_post_meta($product_id, 'sample_price', true) : 0;
if ($sample_price_mode === 'custom'){
$price = $sample_price;
}else if ($sample_price_mode === 'free'){
$price = 0;
}else{
$price = $value['data']->price;
}
$value['data']->set_price( $price );


}

}
}
//custome RI code start

2) Add this code inside the foreach loop of mini-cart file
    note: $product_id variable is before this code ,
     $_product   variable is after this code



/wp-content/plugins/woocommerce/templates/cart/mini-cart.php

//custome RI code start
$product_id   = apply_filters( 'woocommerce_cart_item_product_id', $cart_item['product_id'], $cart_item, $cart_item_key );

if($cart_item['sample']){
$sample_price_mode = get_post_meta($product_id, 'sample_price_mode', true) ? get_post_meta($product_id, 'sample_price_mode', true) : 'default';
$sample_price = get_post_meta($product_id, 'sample_price', true) ? get_post_meta($product_id, 'sample_price', true) : 0;
if ($sample_price_mode === 'custom'){
$price = $sample_price;
}else if ($sample_price_mode === 'free'){
$price = 0;
}
$cart_item['data']->set_price( $price );
//$product_price=apply_filters( 'woocommerce_cart_item_price',$price, $cart_item, $cart_item_key );
}

$_product     = apply_filters( 'woocommerce_cart_item_product', $cart_item['data'], $cart_item, $cart_item_key );
//custome RI code start

No comments :

Post a Comment