I'm trying to get tax rates from products in my custom WooCommerce Plugin.
Below some code:
foreach ($order_items($order) as $item)
{
$item_data = $item->get_data();
$product_id = $item->get_product_id();
$_product = wc_get_product($product_id);
$tax_rates = WC_Tax::get_base_tax_rates($_product->get_tax_class(true));
if (!empty($tax_rates)) {
$tax_rate = reset($tax_rates);
fatt_24_trace('davide_tax_rate :', $tax_rate['rate']);
}
I enabled WP debug, and I'm not able to place an order: I stop during checkout process.
Then I got this debug.log:
PHP Fatal error: Uncaught Error: Class 'myplugin_name\WC_Tax' not found in path/folders/...
Any suggestions? Thanks
I solved by myself. I edited my code in this way:
$tax_rates = \WC_Tax::get_base_tax_rates($_product->get_tax_class(true));
Now I get tax class correctly, then I get the rates in this way:
if (!empty($tax_rates))
$tax_rate = reset($tax_rates);
$vat = $tax_rate['rate'];
Related
I am trying to pass the active product variation to a wordpress widget.
The widget code for displaying the image is currently:
$product = wc_get_product(get_the_ID());
$artwork_url = wp_get_attachment_url($product->get_image_id());
$unit = get_option('woocommerce_dimension_unit');
$height2 = $product->get_height();
$width2 = $product->get_width();
$height = $height2;
$title = get_the_title();
$price = $product->get_price();
$measurements_display = $widget->dimensions == 1 ? 'WxH' : 'HxW';
The $artwork_url is what I am having problems with. When I visit the site, the variation options successfully display the appropriate variation image. I would like to pass that value onto the widget with the $artwork_url expression. I've tried pulling thumbnails, feature images, wo-post-images, and am not getting anywhere.
If anyone could offer a suggestion to this, i'd greatly appreciate any advice.
Thanks
Cameron
I've tried defining $variations and $post-image, $variable_product, but can't seem to get the language right.
To get the active variation image URL in the widget, you can try the following code:
$product = wc_get_product(get_the_ID());
$variation_id = isset($_REQUEST['variation_id']) ? $_REQUEST['variation_id'] : '';
if ($variation_id) {
$variation = new WC_Product_Variation($variation_id);
$artwork_url = wp_get_attachment_url($variation->get_image_id());
} else {
$artwork_url = wp_get_attachment_url($product->get_image_id());
}
$unit = get_option('woocommerce_dimension_unit');
$height2 = $product->get_height();
$width2 = $product->get_width();
$height = $height2;
$title = get_the_title();
$price = $product->get_price();
$measurements_display = $widget->dimensions == 1 ? 'WxH' : 'HxW';
This code first checks if a variation_id is present in the request parameters. If it is, it creates a new WC_Product_Variation object with that ID and gets the variation's image URL. If not, it gets the parent product's image URL as before.
I have a site with Woocommerce and WPML + Multilingual Woocommerce installed. My problem is that I try to insert a product as a translation of a previously entered product without being aware of the ID of the main product. If I enter the ID as translation_of it works; both products share the same SKU and the translation has the SKU field disabled, which is how I want it to work. But I don't want to enter translation_of into the data that gets sent to Woocommerce. I want to only use the SKU and then let Wordpress first check if a product with that SKU already exists and replace sku with translation_of if it does.
This is how I went about it:
add_filter('woocommerce_api_create_product_data', '__create_product_data', -100, 2);
function __create_product_data($data, $api) {
if(isset($data['sku']) && $product_id = wc_get_product_id_by_sku($data['sku'])) {
$product_id = apply_filters('wpml_object_id', $product_id, 'product');
$data['translation_of'] = $product_id;
unset($data['sku']);
}
return $data;
}
But it seems to me that execution arrives at this point long after the SKU has been checked, because I noticed that I can return nothing and I still get product_invalid_sku error back. What would be the correct hook or does such a hook even exist?
My own solution:
add_filter('rest_pre_dispatch', '__rest_pre_dispatch', 10, 3);
function __rest_pre_dispatch($result, $server, $request) {
$sku = $request->get_param('sku');
if ($sku) {
$id = wc_get_product_id_by_sku($sku);
if ($id) {
$product_id = apply_filters('wpml_object_id', $id, 'product');
$request->set_param('translation_of', $product_id);
$request->offsetUnset('sku');
}
}
return $result;
}
I am adding a filter in wordpress plugin.
Filter:
add_filter('wp_insert_post_data', 'saveuser_filter');
And calling it in functions.php file with below code:
function saveuser_filter($use_number, $use_code){
$use_num = $use_number;
$use = $use_code;
global $wpdb;
$mydb = new wpdb('username','password','database','host');
$stmt1 = $mydb->query("INSERT INTO wpconfig (use_num, use) VALUES ('$use_num', '$use')");
$resu = $mydb->query($mydb->prepare($stmt1));
if($resu)
{
$resu="Data Inserted Successfully:";
echo json_encode($resu);
}
else {
$error="Not Inserted,Some Probelm occur.";
echo json_encode($error);
}
}
However, I am saving these details after user completes an order on woocommerce. But anyhow, its giving error: Unable to create order. and order is not getting placed. Please suggest what I am doing wrong here.
I'm trying to load product images on list page. Here is my code:
$_productCollection = $block->getLoadedProductCollection();
foreach ($_productCollection as $_product){
$images = $product->getMediaGalleryImages();
The $_product variable is fine. The variable $images is always null.
I was able to solve this with following code:
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$_productCollection = $block->getLoadedProductCollection();
foreach ($_productCollection as $_product){
$product = $objectManager->create('Magento\Catalog\Model\Product')->load($id);
$images = $product->getMediaGalleryImages();
}
You need to use the addMediaGalleryData method.
See this answer on magento.stackexchange.com for more details.
I am making a plugin that works in collaboration with woocommerce WordPress plugin. I am getting an error when trying to get items for an order:
"Fatal error: Call to a member function get_items() on a non-object in /home/telesqua/public_html/mehtab/wp-content/plugins/order-grabber/order-grabber.php on line 283"
and this is what is on line 283 and around it:
function post_order()
{
if(isset($order_id))
{
$order = new WC_Order($order_id);
}
$items = $order->get_items();
$number_of_items_in_this_order = $order->get_item_count();
Can anyone help me point out the problem? I can provide the source file if needed.
this is because it an add_action has to be executed after using the above code
add_action('init', 'post_order');