Sometimes it is good to store form data in session – for example when working with filters, when you want to save last filters state when user gets back to page. But if your form has Entity type in choice field, you will see error like:
“Entity of type XXX passed to the choice field must be managed. Maybe you forget to persist it in the entity manager?”
How to deal with this? Read below.Continue reading
Tag: php
How to install pcntl PHP extension in Mamp Pro on MacOS Catalina?
Ever wanted to install pnctl extension for Mamp PRO on Catalina in 16 easy steps? If yes, read below.Continue reading
How to find Woocommerce products out of stock with stock greater then zero?
Sometimes, especially if you are using custom plugins to import stock data, you may find products with some stock value (greater then 0) but with stock flag set to “out of stock”. How to find them? Using WP_Query of course:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
$args = array( 'post_type' => 'product_variation', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => '_stock_status', 'value' => 'outofstock', ), array( 'key' => '_stock', 'value' => '0', 'compare' => '>', 'type' => 'NUMERIC' ), ), ); $products = new WP_Query($args); |
Notice ‘post_type’ => ‘product_variation’ – if you are not using variations, just change it to ‘product’.
How to make Woocommerce pages work with Polylang?
This can be really frustrating, you wanted your woocommerce shop running multilingual, but from cart page “Checkout” button leads to default language version? Solution is very simple, but not elegant – so far did not found another solution. First step is to create all woocommerce pages in every language you need, this is very simple but can take some time. Then, in your child theme (remember not to make any modifications to theme you are using!), in functions.php add such code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
function get_woocommerce_checkout_polylang() { return pll_get_post(get_option('woocommerce_checkout_page_id' ) ); } add_filter('woocommerce_get_checkout_page_id', "get_woocommerce_checkout_polylang"); function get_woocommerce_shop_polylang() { return pll_get_post(get_option('woocommerce_shop_page_id' ) ); } add_filter('woocommerce_get_shop_page_id', "get_woocommerce_shop_polylang"); function get_woocommerce_cart_polylang() { return pll_get_post(get_option('woocommerce_cart_page_id' ) ); } add_filter('woocommerce_cart_page_id', "get_woocommerce_cart_polylang"); function get_woocommerce_pay_polylang() { return pll_get_post(get_option('woocommerce_pay_page_id' ) ); } add_filter('woocommerce_pay_page_id', "get_woocommerce_pay_polylang"); function get_woocommerce_thanks_polylang() { return pll_get_post(get_option('woocommerce_thanks_page_id' ) ); } add_filter('woocommerce_thanks_page_id', "get_woocommerce_thanks_polylang"); function get_woocommerce_myaccount_polylang() { return pll_get_post(get_option('woocommerce_myaccount_page_id' ) ); } add_filter('woocommerce_myaccount_page_id', "get_woocommerce_myaccount_polylang"); function get_woocommerce_edit_address_polylang() { return pll_get_post(get_option('woocommerce_edit_address_page_id' ) ); } add_filter('woocommerce_edit_address_page_id', "get_woocommerce_edit_address_polylang"); function get_woocommerce_view_order_polylang() { return pll_get_post(get_option('woocommerce_view_order_page_id' ) ); } add_filter('woocommerce_view_order_page_id', "get_woocommerce_view_order_polylang"); function get_woocommerce_terms_polylang() { return pll_get_post(get_option('woocommerce_terms_page_id' ) ); } add_filter('woocommerce_terms_page_id', "get_woocommerce_terms_polylang"); |
All those functions are using same pll_get_post from Polylang plugin, to translate your default page ID you set in Woocommerce/Settings/Checkout/Checkout Pages to their translated version. Hope you will find it useful.
How to fix WooCommerce 404 error on product page
This can be frustrating when you are new to wordpres and/or woocommerce. You just created your first product, and got 404 when trying to see it. Fortunately solution is quite simple. In backend panel go to “Settings”, then “Permalinks”. In “Common settings” section make sure “Post Name” is selected, if not, choose it – it will look like “http://newbl.blastar.biz/sample-post/”. Then look below, in “Product permalink base” section choose “Shop base” – it will look like “http://newbl.blastar.biz/shop/sample-product/”. Click “Save CHanges” and below you should see new box with .htaccess settings like:
1 2 3 4 5 6 7 8 |
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> |
Just copy it and paste to your .htaccess. All links should work now!
Send pdf automatically when invoice is created in Magento
It is a bit different then method showed in previous post – this time its not transactional email sent at demand, but we will catch event fired after invoice is generated. So (I suppose you have your module already) in etc/config.xml add:
1 2 3 4 5 6 7 8 9 10 11 12 |
<global> <events> <sales_order_invoice_save_after> <observers> <yourvendor_yourmodule_invoice_observer> <class>YourVendor_YourModule_Model_Observer</class> <method>sendInvoice</method> </yourvendor_yourmodule_invoice_observer> </observers> </sales_order_invoice_save_after> </events> </global> |
Then create your class at app/code/local/YourVendor/YourModule/Model/Observer.php (or use existing one) and add new method:
1 2 3 4 5 6 7 8 9 10 |
public function sendInvoice($observer) { try { /* @var $order Magento_Sales_Model_Order_Invoice */ $invoice = $observer->getEvent()->getInvoice(); $invoice->sendEmail(); } catch (Mage_Core_Exception $e) { Mage::log("Error: " . $e->getMessage()); } return $this; } |
And invoice will be attached to email defined at:
Quicktip: SQLSTATE[HY000] [2002] Not a directory trying doctrine:schema:create
If you are using MAMP and trying to execute doctrine:schema:create or anything db related you may encounter that error, solution is simple – you need to create symlink to mysql.sock:
1 2 3 4 |
sudo mkdir /var/mysql sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock sudo chown _mysql /var/mysql/mysql.sock sudo chmod 777 /var/mysql/mysql.sock |
Tutorial: How to create Amazon Fulfillment order using php?
Follow my steps and you will get working service:Continue reading
Quick tip: PHP Extensions “0” must be loaded error in Magento install
Such error happens in 1.7.x branch (I saw it in 1.7.2), when entering database parameters you’re seeing “PHP Extensions “0” must be loaded” instead of next step. This is internal Magento bug, you can correct it by finding file app/code/core/Mage/Install/etc/config.xml and replacing:
1 2 3 |
<extensions> <pdo_mysql/> </extensions> |
with:
1 2 3 |
<extensions> <pdo_mysql>1</pdo_mysql> </extensions> |
Cannot find autoconf when using phpize on Mavericks
running:
1 |
phpize |
command can cause such sample output:
1 2 3 4 5 6 |
Configuring for: PHP Api Version: 20100412 Zend Module Api No: 20100525 Zend Extension Api No: 220100525 Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this script. |
solution is simple, just put:
1 |
brew install autoconf |
in terminal, push enter and rerun phpize after brew finishes hiw work