for my woocommerce e-shop i use WpAllImport plugin for importing my products with their variations. Typically one product with the variations is shown below
The parent product has no price/sale price values and the price/sale price values are all the same for all variations.
Now i want to export an xml file with only my parents products with WpAllExport plugin. As there is no price/sale price values the tag <price> </price> and <sale price> </sale price>is return empty with no values.
I realise that i need php function for the export to pass any variation price/sale price to parent product price fields.
Does someone help me with the php function i have to write?
Thank you in advance
OK, finally I add the following code and it works fine.
<?php
function get_regular_price_for_xml($parent, $price)
{
if ($parent == "0") {
global $product;
$product_variations = $product->get_available_variations();
if (!empty($product_variations) && is_array($product_variations)) {
$variation_product_id = $product_variations[0]['variation_id'];
$variation_product = new WC_Product_Variation($variation_product_id);
return $variation_product->regular_price;
} else {
return $price;
}
} else {
return $price;
}
}
Related
Hi so I would like to be able to change currency for different products added to the cart/checkout.
I found solution that is filtering woocommerce_currency hook and using cart session to change currency.
Two questions:
Is there a better way of doing it?
Do you see any potential problems with the following solution?
public function modifyWoocommerceCurrency( $currency ) {
$cart_data = WC()->session->get('cart');
foreach ($cart_data as $item) {
if (isset($item["_campaign_options"]["user_paying_currency"])) {
$currency = $item['_campaign_options']['user_paying_currency'];
}
}
return $currency;
}
add_filter( 'woocommerce_currency', [$this,'modifyWoocommerceCurrency'], 10, 1 );
On single product page I'm trying to get stock quantity or atleast stock status from woocommerce product but it seems not working properly, even if it's simple product (manage stock status is checked) or variable product the button is not showing. And strange thing happens - I've got Yith Product Bundle plugin and even if every item is in stock it shows that button but whatever, that's not main problem.
So here is code I'm using
add_action('woocommerce_before_add_to_cart_button', 'show_wishlist');
function show_wishlist() {
global $product;
$stockQ=$product->get_stock_quantity();
if ( $stockQ < 1 ) {
echo "<button>Hello</button>";
}
}
I've also tried but completely nothing happens.
add_action('woocommerce_before_add_to_cart_button', 'show_wishlist');
function show_wishlist() {
global $product;
if ( ! $product->is_in_stock() ) {
echo "<button>Hello</button>";
}
}
First check if the hook is firing properly.
add_action( 'woocommerce_before_add_to_cart_button', 'show_wishlist' );
function show_wishlist(){
echo '<p>Some custom text before</p>';
}
If you are able to confirm that the hook is working then use the below code.
function show_wishlist() {
global $product;
// Change In Stock Text
if ( $product->managing_stock() && $product->is_in_stock() ) {
//add your code here for Products in Stock
}
else{
//add your code here for Products out of Stock
}
}
I have tried to do it using a filter hook called "woocommerce_countries". I tried getting the cart total in this hook but it's not working. Is anyone have any idea on this OR suggest any hook?
Please try with this code. WooCommerce 3.6.2, they have removed the ability to check the cart data. But cart session is accessible right now so we use this function WC()->session->cart
add_filter( 'woocommerce_countries', 'products_disable_country', 10, 1 );
function products_disable_country( $countries ) {
$cartdata = WC()->session->cart;
if (is_array($cartdata) && !empty($cartdata)) {
foreach ($cartdata as $itemdata) {
$sum += $itemdata['line_total'];
}
if ($sum >= 100) {
unset($countries["CA"]);
}
}
return $countries;
}
I already created a function that will filter products using URL parameters and display them on the /shop/ page. That works fine.
But, that only works if the products template is set on the WooCommerce /shop/ page.
In my case the requirement is to have the category list set as default template on the /shop/ page. That means I need to override that template and show the filtered product list, when I run a product query with URL parameters.
(eg. example.com/shop/?param1=foo¶m2=bar)
I tried following code (and some iterations), but I'm stuck and have no clue how to override the default template when a product filter is applied:
add_filter( 'woocommerce_locate_template', [$this, 'my_include_template_function'], 10, 3 );
public function my_change_template_function($template, $template_name, $template_path)
{
if (!empty($_GET['param1']) && is_shop() && $template_name == 'content-product_cat.php') {
return wc_get_template_part('content', 'product');
} else {
return $template;
}
}
So the source of the categories loop is added in to the loop start [via a filter][1]
add_filter( 'woocommerce_product_loop_start', 'woocommerce_maybe_show_product_subcategories' );
and the woocommerce_maybe_short_product_subcategories() function changes the output based on the:
$display_type = woocommerce_get_loop_display_mode();
However, the woocommerce_get_loop_display_mode() result isn't filterable directly.
But it relies on some values from get_option() (since the setting is in the customizer) and get_option() is filterable via option_$option
So a potential way to tell WooCommerce not to display the categories when you have a particular $_GET parameter, you could filter the shop display mode option to be products when that parameter is detected in the URL. Like this:
/**
* Filter shop display type.
*
* #param string $value - 'products' | 'subcategories' | 'both'
* #return string
*/
function kia_woocommerce_shop_page_display( $value ) {
if ( ! empty( $_GET['param1'] ) {
$value = 'products';
}
return $value;
}
add_filter( 'option_woocommerce_shop_page_display', 'kia_woocommerce_shop_page_display' );
Im having a issue with function:
I need to display product variations (not parent) on category page. -
Let's say:
If is category "t-shirts" I need to display all product variations with attribute "t-shirts".
Im using this function but it's not working:
add_action( 'woocommerce_product_query', 'rc_modify_query_get_design_projects' );
function rc_modify_query_get_design_projects( $product ) {
if (is_tax('pa_tshirts') || is_tax('pa_shirts') || is_product_category('summer')) {
$product->set('post_type', 'product_variation');
}
}
Hope anyone can help.
Thanks