I have two tables: products (which has price_before and discount_id) and discounts (which has discount_value and discount_id) and I want to show the products with the new price.
This is my query in product controller:
public function newPrice(Request $request)
{
$newPrice= Product::join('discounts','discounts.discount_id','=','products.discount_id')
->where('products.product_id',$request->id)
->select(DB::raw('products.*','(products.prict_before * discounts.discount_value/100) as priceAfter'))
->get();
return response()->json($newPrice);
}
and this is my route:
Route::get('/newPrice/{id}','App\Http\Controllers\ProductController#newPrice');
My problem is: when I test the query in postman, it shows me the information about the product without new price (priceAfter) and I want to return it.
Can you help me please?
Related
I want to update WooCommerce products by the WP All import. I have 2 independent xml files (from the first one I'm getting prices, from the second one descriptions). For some reasons product's _sku is {code} from the first one xml not {ean}. So I put {ean} into product attribute pa_ean to match the products from the first one and the second one xml.
Now I don't know how to write the php function to return product_id so WP All import could update right products with right descriptions.
Do you have any unique identifier that are same for the product in both files? Like product_key or something. If you have it you can store it as custom meta for that product and get product by that meta key, something like below ↓
function check_if_product_exist($rawDataFromFile) {
global $products;
// Get WC_Product Objects
$products = wc_get_products([
'meta_key' => 'your_meta_key',
'meta_value' => $rawDataFromFile->your_key
]);
if (!empty($products)) {
return $products[0];
}
return null;
}
I put {ean} to Custom field, not to Attribute and after that I can easily use it in WP All import.
I would like to ask Is there any simple method to get data with conditions in second relation.
In my controller I get every category entity.
Every category have many products so there is relation "OneToMany".
Next every product have an author and there is relation "ManyToOne".
Now I would like to get every category which include products whose are active and whose author is active.
category OneToMany product
product ManyToOne author
product have field "active"
author have field "active"
My method to get products in Category entity.
public function getProducts()
{
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('isVisible', true));
$criteria->andWhere(Criteria::expr()->eq('isActive', true));
return $this->products->matching($criteria);
}
When you call getProducts(), Doctrine gets all entries from the database and selects products matching your criteria. While this works for now, it's better to use your database (MySQL?) to select the right entries.
So use a QueryBuilder instead. And since you're Symfony, create those methods in a Repository.
We're not here to write your code, but to give you an idea, it can be something like this:
CategoryRepository.php
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Query\Expr\Join;
class CategoryRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, Category::class);
}
public function findActiveCategories()
{
$qb = $this->createQueryBuilder('Category')
->innerJoin(
'AppBundle:Product',
'Product',
Join::WITH,
'Category.id = Product.category'
)
->where('Product.isActive = 1')
;
return $qb->getQuery()->getResult();
}
}
Basically, on the order edit page, I want to calculate the total of an individual product as price * product_height * quantity_of_products.
reference image
I have used the following hook but this hook is not getting executed.
add_filter( 'woocommerce_data_get_total', 'change_product_total_on_order_edit_page', 10, 2);
function change_product_total_on_order_edit_page ($value, $abc ) {
var_dump($value);
die();
return 15;
}
Use 'woocommerce_order_item_get_total' filter instead of 'woocommerce_data_get_total' because this filter is created dynamically by Woocommerce. The data in the hook is object type. When order items are fetched on orders edit page the object type is order_item.
Guys !
I develop eCommerce platform but i don't know how to implement the scenario below.
I want to have friendly URLs (for SEO stuff..).
I have the correct slug in the database for categories and products for example:
www.domain.com/category-tools/
wwww.domain.com/category-laptops/ - which are categories
but i and products which are:
www.domain.com/iphone6s-32gb
www.domain.com/macbook-pro-ssd
How to implement this logic in to the handler below ? How to know when it is product or category ? Do i need to make two separate queries (one for products and one for categories) to the database ?
#RequestMapping(value = "/{categoryOrProduct}", method = RequestMethod.GET)
public String getCategory(Model model) {
model.addAttribute("category", .......);
if( is category ) {
return "categoryTemplate"
}
return "productTemplate;
}
I'm building a site with Wordpress and WooCommerce for someone for the purposes of a business, but they have certain items that cannot be sold together - essentially the opposite of a force sell or chained products. If a customer puts product A in their cart, I don't want them to be able to put product C in with it, but B and D are fine. Alternatively, when it goes to checkout, separating the products into two separate orders would work as well. Is there any way to do this? This is my first time using Wordpress, so I'm a bit at a loss.
You can use this code below :
add_action('woocommerce_add_to_cart_handler','mycustomfuncion',11,2);
function mycustomfuncion($p,$q)
{
global $woocommerce;
$cartItem = $woocommerce->cart->cart_contents;
$currentProductId = $q->id;
foreach($cartItem as $item)
{
$productItemId = $item['product_id'];
///your condition will be here
}
return $q;
}
You will get all product ids which are in cart by the $productItemId. And you will get current product id (which user wants to add to cart ) by $currentProductId. After your conditions if you want to allow to add to cart the product then return $q from the function otherwise don't return any value.