Remove Quantity number before stock status in Woocommerce? - php

i use plugin called WC Product Stock Status into my Woocommerce site, and want to remove number before stock status. I tryed to see if quantity number is set in different CSS class, but seems that is all in one class, so hidding via CSS looks not possible.
.woocommerce div.product p.stock
{font-size: .92em;}
.woocommerce div.product .s_in_stock_color {
color:#77a464;
}
Unfortinally this plugin is outdated and looks no longer supported be author, so asking question on plugin page, is useless. So i want just to remove number of quantity that is in stock, and keep the label, like on image bellow:
I checked into code and found that that table is generated fetching this data in class-variable-status.php file:
public function print_stock_status(){
global $product;
if($product->is_type( 'variable' ) ) {
$childrens = $product->get_children();
$values = array();
foreach($childrens as $child){
$prod = wc_get_product($child);
$search_replace = array();
$search_replace['%formatted_name%'] = $prod->get_formatted_name();
$search_replace['%name%'] = wc_get_formatted_variation($prod);
$search_replace['%sku%'] = $prod->get_sku();
$search_replace['%id%'] = $prod->get_id();
$format = wc_pstocks_option('variable_title');
$format = str_replace(array_keys($search_replace),array_values($search_replace),$format);
$status = $prod->get_availability();;
$status['title'] = $format;
$status['ID'] = $child;
$status['formatted_name'] = $prod->get_formatted_name();
$status['name'] = wc_get_formatted_variation($prod);
$status['sku'] = $prod->get_sku();
$status['id'] = $prod->get_id();
$values[$child] = $status;
}
$this->generate_table($values);
}
}
public function generate_table($values){
wc_pstocks_get_template('stock-variation-
table.php',array("args"=>$values));
}
and this is code from stock-variation-
table.php
<?php
foreach($args as $arr){
$text = empty($arr['availability']) ? '' : '<p class="stock '.esc_attr($arr['class']).'">'.$arr['availability'].'</p>';
echo '<tr>';
echo '<td>'.$arr['title'].'</td>';
echo '<td>'.$text.'</td>';
echo '</tr>';
}
?>
I think that something neeed to be updated into this line:
$text = empty($arr['availability']) ? '' : '<p class="stock '.esc_attr($arr['class']).'">'.$arr['availability'].'</p>';
Any help how to remove that?

I was able to fix myself.. This is what i do:
From Woocommerce settings goes to Products>Inventory and just set this:
Now showing statuses, but without quantity.

Related

PHP Syntax error issue, display title correctly using parameters

I have a php Code as below but unable to display title with correct php value
For instance on standalone basis it displays correct data as
$this->CatName; - Displays category
$this->prodDet->prod_name; - Displays Product Name
$this->prodDet->v_location; - Displays Location
I want to create a combined title as
Used <catname> <prod_name> for sale in <v_location>
Like
Used Fisher Milk Bottle for sale in Capetown
But when i modify code in
$title = 'Used' ($this->CatName $this->prodDet->prod_name) 'for Sale in' ($this->prodDet->v_location);
It shows syntax error and does not work
Here is complete code
<?php
defined('_JEXEC') or die;
jimport('joomla.application.component.view');
/**
* Prepares the document
*
* #return void
*
* #throws Exception
*/
protected function _prepareDocument()
{
$app = JFactory::getApplication();
$menus = $app->getMenu();
$title = null;
$menu = $menus->getActive();
if ($menu) {
$this->params->def('page_heading', $this->params->get('page_title', $menu->title));
} else {
$this->params->def('page_heading', JText::_('COM_USEDCAR_DEFAULT_PAGE_TITLE'));
}
$title = $this->params->get('page_title', '');
if (empty($title)) {
$title = $app->get('sitename');
} elseif ($app->get('sitename_pagetitles', 0) == 1) {
$title = JText::sprintf('JPAGETITLE', $app->get('sitename'), $title);
} elseif ($app->get('sitename_pagetitles', 0) == 2) {
$title = JText::sprintf('JPAGETITLE', $title, $app->get('sitename'));
}
$title = $this->CatName;
$this->document->setTitle($title);
}
?>
Can any one help how to display correct title
Use the concatenation operator . , read more here
change
$title = 'Used' ($this->CatName $this->prodDet->prod_name) 'for Sale in' ($this->prodDet->v_location);
to
$title = 'Used' . ($this->CatName .' '.$this->prodDet->prod_name). 'for Sale in' . ($this->prodDet->v_location);
Use concatenation(.) operator there like below:-
$title = 'Used '.($this->CatName.' '.$this->prodDet->prod_name).' for Sale in '.($this->prodDet->v_location);
Note:- Also add spaces in between variables and string so that output look bit good.(I have already did that in my solution)
Reference:-String Operators

Magento $h1Tags regex for all tags

This code only replace to h1 tag product name I would like to replace in all tags
public function replaceName($observer)
{
$accessToPage = array(
'catalog_product_view',
'review_product_list',
);
if(!in_array(Mage::helper('mage_seoall/request')->getCurrentFullActionName(), $accessToPage)){
$block = $observer->getBlock();
if($block->getNameInLayout() != 'product.info'){
return;
}
$productSeoName = Mage::registry('current_product')->getProductSeoName();
if(!$productSeoName){
return;
}
$transport = $observer->getTransport();
$normalOutput = $observer->getTransport()->getHtml();
$modifyOutput = $normalOutput;
$h1Tags = array();
preg_match_all('#<h1[^>]*>(.*)?</h1>#is', $normalOutput, $h1Tags);
for ($i = 0; $i < count($h1Tags[1]); $i++) {
$modifyOutput = str_replace($h1Tags[1][$i], $productSeoName, $modifyOutput);
}
$transport->setHtml($modifyOutput);
}
}
This magento extension changing product name which is only in h1 tag but on my template h1 tags is only product page so product name is seeing different on other page such as listing checkout, cart ajax cart...
I am try to change regex for all tags but I couldnt......

How to get all woocommerce shipping packages in a custom template

I need to let customer edit their pending payment order. By default, woocommerce only allow change the payment method. So, I have created a custom template for this feature.
The problem now I encountered is I can't get the shipping packages in the template.
Here is the code that I adapted from the wc_cart_totals_shipping_html() :
$packages = WC()->shipping->get_packages();
print_r($packages);
foreach ( $packages as $i => $package ) {
//blah blah blah
}
The print_r($packages) give me the null array. But on the checkout page, it's working fine.
Any idea why? Or, get the shipping packages by other method?
Please try this -
global $woocommerce;
$customerZipCode = 75098;
$zipResultArr = csd_check_zip_and_state($customerZipCode);
$bh_packages = $woocommerce->cart->get_shipping_packages();
$bh_packages[0]['destination']['state'] = $zipResultArr['state'];
$bh_packages[0]['destination']['postcode'] = $customerZipCode ;
$bh_packages[0]['destination']['city'] = $zipResultArr['city'];
$bh_packages[0]['destination']['address'] = '';
$bh_packages[0]['destination']['address_2'] = '';
//Calculate costs for passed packages
$bh_shipping_methods = array();
foreach( $bh_packages as $bh_package_key => $bh_package ) {
$bh_shipping_methods[$bh_package_key] = $woocommerce->shipping->calculate_shipping_for_package($bh_package, $bh_package_key);
}
$shippingArr = $bh_shipping_methods[0]['rates'];
if(!empty($shippingArr)) {
$response = array();
foreach ($shippingArr as $value) {
$shipping['label'] = $value->label;
$shipping['cost'] = $value->cost;
$response['shipping'][] = $shipping;
}
}
// This is your shipping
print_r($response);

Programmatically stop decreasing stock qty while placing an order in magento and increaging qty when order state is Compleate

In Magento 1.9.0.1 by default, it decreases stock qty while placing an order. I want to stop decreasing stock qty while placing an order for the purpose of fake order. And I also want the functionality of increasing stock qty while order state is complete, not pending, not processing. How to do it programmatically or by setting up in Admin panel. If any one knows about it, please reply back.
Thank You
Ankan
Just simply open your admin panel, go to System->Configuration->Catalog Tab->Inventory Then click on Stack Option and change Decrease Stock When Order is Placed to NO.
Thanks,
Lovekesh
In Admin panel, System->Configuration->Catalog Tab->Inventory Then click on Stack Option and change Decrease Stock When Order is Placed to NO. So that it stops the stock qty increasing.In Model/Automatically/Complete/Order/Observer.php page, class Webspidy_Customoption_Model_Automatically_Complete_Order_Observer
{ public function __construct(){}
public function automaticallycompleteorder($observer)
{
//Mage::log('Ankan');
$order = $observer->getEvent()->getOrder();
/$orders = Mage::getModel('sales/order_invoice')->getCollection()
->addAttributeToFilter('order_id', array('eq'=>$order->getId()));/
/*if ((int)$orders->count() !== 0) {
return $this;
}*/
//Mage::log($orderstate);
//if($order->getState() == 'complete'){
if(($order->getState() == 'processing') || ($order->getState() == 'pending_payment')){
if($order->hasInvoices()){
//Mage::log($orderstate);
//Mage::log('Ankan');
//Mage::log($order->getData());
//====== Order Details ================
$orderNumber = $order->getIncrementId(); //Mage::log($orderNumber);
$orderDet = Mage::getModel('sales/order')->load($orderNumber, 'increment_id');
//Mage::log($orderDet->getData());
$orderItems = $orderDet->getItemsCollection()
->addAttributeToSelect('*')
->load();
foreach($orderItems as $orderItem){
$productId = $orderItem->getProductId();
$productOptionSku = $orderItem->getSku();
$productQty = $orderItem->getQtyOrdered();
$product = Mage::getModel('catalog/product')->load($productId);
$sku = $product->getSku();
$centralqty = $product->getStockItem()->getQty();
$values = array();
foreach ($product->getOptions() as $o) {
$p = $o->getValues();
}
foreach($p as $v)
{
$optionSku = $v->getSku();
$optionItem = $v->getQty();
//Mage::log($optionItem);
//Mage::log($productOptionSku.".....".$sku."-".$optionSku.".....".$optionItem);
if($productOptionSku == ($sku."-".$optionSku)){
if($centralqty >= ($optionItem*(int)$productQty)){
//$stockQty = (($centralqty-($optionItem*(int)$productQty))+$productQty);
$stockQty = ($centralqty-($optionItem*(int)$productQty));
}
}
}
$product->save();
//Mage::log($stockQty);
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
//$stockItem = Mage::getModel('cataloginventory/stock_item')->load($productId);
$stockItemId = $stockItem->getId();
$stockItem->setData('qty', $stockQty);//(integer)$XMLproduct->QtyInStock
$stockItem->setData('manage_stock',1);
$stockItem->save();
}
}
//======== End ======
}
}
}
And in etc/config.xml,
<global><events>
<sales_order_save_after><observers>
<webspidy_customoption_automatically_complete_order_observer>
<type>singleton</type> <class>Webspidy_Customoption_Model_Automatically_Complete_Order_Observer</class>
<method>automaticallycompleteorder</method>
</webspidy_customoption_automatically_complete_order_observer>
</observers></sales_order_save_after></events></global> After a long process, I have got a success by creating Observer in my custom module.
I am using Magento 1.9
I have tried with the following code. it's OK But i have another smart code
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$stockItemId = $stockItem->getId();
$stockItem->setData('qty', $stockQty);
$stockItem->setData('manage_stock',1);
$stockItem->save();
My code is following:-
Mage::getModel('cataloginventory/stock')->backItemQty($productId,$new_qty);

WordPress & Woocommerce with Dynamic Pricing unserialized string not parsing as array foreach

The following serialized string is being pulled from the meta data of a Woocommerce product.
a:2:{s:17:"set_51fb76f97cc57";a:6:{s:15:"conditions_type";s:3:"all";s:10:"conditions";a:1:{i:1;a:2:{s:4:"type";s:8:"apply_to";s:4:"args";a:2:{s:10:"applies_to";s:5:"roles";s:5:"roles";a:1:{i:0;s:14:"trade_customer";}}}}s:9:"collector";a:1:{s:4:"type";s:7:"product";}s:4:"mode";s:5:"block";s:5:"rules";a:1:{i:1;a:4:{s:4:"from";s:0:"";s:2:"to";s:0:"";s:4:"type";s:14:"price_discount";s:6:"amount";s:0:"";}}s:10:"blockrules";a:1:{i:1;a:5:{s:4:"from";s:1:"*";s:6:"adjust";s:1:"1";s:4:"type";s:16:"fixed_adjustment";s:6:"amount";s:4:"8.37";s:9:"repeating";s:3:"yes";}}}s:17:"set_51fb76f97d6a2";a:6:{s:15:"conditions_type";s:3:"all";s:10:"conditions";a:1:{i:1;a:2:{s:4:"type";s:8:"apply_to";s:4:"args";a:2:{s:10:"applies_to";s:5:"roles";s:5:"roles";a:1:{i:0;s:19:"bulk_trade_customer";}}}}s:9:"collector";a:1:{s:4:"type";s:7:"product";}s:4:"mode";s:5:"block";s:5:"rules";a:1:{i:1;a:4:{s:4:"from";s:0:"";s:2:"to";s:0:"";s:4:"type";s:14:"price_discount";s:6:"amount";s:0:"";}}s:10:"blockrules";a:1:{i:1;a:5:{s:4:"from";s:1:"*";s:6:"adjust";s:1:"1";s:4:"type";s:16:"fixed_adjustment";s:6:"amount";s:5:"9.428";s:9:"repeating";s:3:"yes";}}}}
That's using Woocommerce with the Dynamic Pricing plugin.
I also have this function which I am writing in order to pull out the variable product data and display it on the site:
function mi_price_adjust(){
global $post, $current_user, $user_roles;
$meta = get_post_meta($post->ID);
$curPrice = (float)$meta['_regular_price'][0];
$variations = unserialize($meta['_pricing_rules'][0]);
$user_roles = $current_user->roles;
$theRoll = '';
$thePrice = $curPrice;
foreach($user_roles as $miroll){
if(in_array($miroll, $user_roles)){
$theRoll = $miroll;
}
}
if($theRoll != ''){
foreach($variations as $curvar){
$var_roll = $curvar['conditions'][1]['args']['roles'][0];
$var_cost = (float)$curvar['blockrules'][1]['amount'];
if($var_roll == $theRoll){
$thePrice = $curPrice - $var_cost;
}
}
}
if($thePrice != $curPrice){
return "<strike>".$curPrice."</strike><br /><span class=\"youpayText\">You pay £$thePrice</span>";
}else{
return $thePrice;
}
}
Everything is working fine apart from one thing. I am getting the following error:
Invalid argument supplied for foreach
The line in question is
foreach($variations as $curvar){
And the $variations variable is being populated using this:
$variations = unserialize($meta['_pricing_rules'][0]);
Which returns the serialized string at the top of this post.
Can anyone shed any light on why this might be happening and what we might be able to do to either fix it or circumvent it?

Categories