

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WooCommerce &#8211; Max的程式語言筆記</title>
	<atom:link href="https://stackoverflow.max-everyday.com/tag/woocommerce/feed/" rel="self" type="application/rss+xml" />
	<link>https://stackoverflow.max-everyday.com</link>
	<description>我要當一個豬頭，快樂過每一天</description>
	<lastBuildDate>Tue, 22 May 2018 10:37:27 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://stackoverflow.max-everyday.com/wp-content/uploads/2017/02/max-stackoverflow-256.png</url>
	<title>WooCommerce &#8211; Max的程式語言筆記</title>
	<link>https://stackoverflow.max-everyday.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>woocommerce_before_calculate_totals fires twice</title>
		<link>https://stackoverflow.max-everyday.com/2018/05/woocommerce_before_calculate_totals-fires-twice/</link>
					<comments>https://stackoverflow.max-everyday.com/2018/05/woocommerce_before_calculate_totals-fires-twice/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Tue, 22 May 2018 10:37:27 +0000</pubDate>
				<category><![CDATA[WordPress筆記]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=2355</guid>

					<description><![CDATA[我使用了 dynamic-price-and-d...]]></description>
										<content:encoded><![CDATA[<p>我使用了 dynamic-price-and-discounts-for-woocommerce plugin ，計算的價格會出錯，原因是因為 calculate_totals 會被執行多次。</p>
<p>&nbsp;</p>
<p>在 class-wc-cart.php 裡可以看到：</p>
<pre>public function __construct() {
	$this-&gt;session          = new WC_Cart_Session( $this );
	$this-&gt;fees_api         = new WC_Cart_Fees( $this );
	$this-&gt;tax_display_cart = get_option( 'woocommerce_tax_display_cart' );

	// Register hooks for the objects.
	$this-&gt;session-&gt;init();

	add_action( 'woocommerce_add_to_cart', array( $this, 'calculate_totals' ), 20, 0 );
	add_action( 'woocommerce_applied_coupon', array( $this, 'calculate_totals' ), 20, 0 );
	add_action( 'woocommerce_cart_item_removed', array( $this, 'calculate_totals' ), 20, 0 );
	add_action( 'woocommerce_cart_item_restored', array( $this, 'calculate_totals' ), 20, 0 );
	add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_items' ), 1 );
	add_action( 'woocommerce_check_cart_items', array( $this, 'check_cart_coupons' ), 1 );
	add_action( 'woocommerce_after_checkout_validation', array( $this, 'check_customer_coupons' ), 1 );
}</pre>
<hr />
<p>由於 calculate_totals 執行多次，所以 woocommerce_before_calculate_totals 也會被執行多次，所以放在 $cart_object 裡的變數也會被寫入多次，解法是在woocommerce_before_calculate_totals 裡，請勿取出$cart_object 的 price, 而是再從 product object 裡去取出 price 即可。</p>
<p>I see exactly the same issue &#8211; my hooked function is firing twice, leading to the option price being added at 2x the cost. My solution was to load the product, get the price from that and then add my incremental cost to that.</p>
<p>function tbk_woo_update_option_price( $cart_object ) {</p>
<pre class="lang-php prettyprint prettyprinted"><code><span class="pln">$option_price </span><span class="pun">=</span> <span class="lit">3.50</span><span class="pun">;</span>
<span class="kwd">foreach</span> <span class="pun">(</span><span class="pln"> $cart_object</span><span class="pun">-&gt;</span><span class="pln">get_cart</span><span class="pun">()</span> <span class="kwd">as</span><span class="pln"> $cart_item_key </span><span class="pun">=&gt;</span><span class="pln"> $cart_item </span><span class="pun">)</span> <span class="pun">{</span><span class="pln">

        $item_id </span><span class="pun">=</span><span class="pln"> $cart_item</span><span class="pun">[</span><span class="str">'data'</span><span class="pun">]-&gt;</span><span class="pln">id</span><span class="pun">;</span>
        <span class="com">//Hook seems to be firing twice for some reason... Get the price from the original product data, not from the cart...</span><span class="pln">
        $product </span><span class="pun">=</span><span class="pln"> wc_get_product</span><span class="pun">(</span><span class="pln"> $item_id </span><span class="pun">);</span><span class="pln">
        $original_price </span><span class="pun">=</span><span class="pln"> $product</span><span class="pun">-&gt;</span><span class="pln">get_price</span><span class="pun">();</span><span class="pln">
        $new_price </span><span class="pun">=</span><span class="pln"> $original_price </span><span class="pun">+</span><span class="pln"> $option_price</span><span class="pun">;</span><span class="pln">
        $cart_item</span><span class="pun">[</span><span class="str">'data'</span><span class="pun">]-&gt;</span><span class="pln">set_price</span><span class="pun">(</span><span class="pln"> $new_price </span><span class="pun">);</span>
<span class="pun">}</span></code></pre>
<p>} add_action( &#8216;woocommerce_before_calculate_totals&#8217;, &#8216;tbk_woo_update_option_price&#8217;, 1000, 1 );</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2018/05/woocommerce_before_calculate_totals-fires-twice/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>修改購物車頁或結帳頁中 &#8216;運送方式 1&#8242; 的翻譯為&#8217;運送方式&#8217;</title>
		<link>https://stackoverflow.max-everyday.com/2017/07/shipping_index/</link>
					<comments>https://stackoverflow.max-everyday.com/2017/07/shipping_index/#comments</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Sat, 01 Jul 2017 22:54:27 +0000</pubDate>
				<category><![CDATA[WordPress筆記]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=944</guid>

					<description><![CDATA[這篇要教大家怎麼修改 &#8216;運送方式1&#038;...]]></description>
										<content:encoded><![CDATA[<p>這篇要教大家怎麼修改 &#8216;運送方式1&#8217; 為 &#8216;運送方式&#8217;.</p>
<p>前言：</p>
<p>有些網站可能有多個運送方式，有些網站沒有，我相信幾乎大多數的小網站只會用到「一組」，不會用到第2組的運送方式，一組裡就已經有多個項目了，相信就很夠用，但WooCommerce 設計上又要弄的有彈性給更多的人使用，要解決這個問題，有多種解法。</p>
<p>我覺得最好的解法是 <span class="fwn fcg"><span class="fwb fcg" data-ft="{&quot;tn&quot;:&quot;;&quot;}"><a id="js_50b" href="https://www.facebook.com/neltseng?fref=nf" data-hovercard="/ajax/hovercard/user.php?id=100006031776607&amp;extragetparams=%7B%22fref%22%3A%22nf%22%2C%22directed_target_id%22%3A443054455850988%7D" data-hovercard-prefer-more-content-show="1" aria-controls="js_509" aria-haspopup="true" aria-describedby="js_50a">Nel Tseng</a></span></span> 所提供的，增加下面這段hook 的function 到 functions.php：</p>
<pre>// change 運送方式1 to 運送方式
// define the woocommerce_shipping_package_name callback 
function filter_woocommerce_shipping_package_name( $sprintf, $i, $package ) { 
 // make filter magic happen here... 
 return '運送方式';
}; 
add_filter( 'woocommerce_shipping_package_name', 'filter_woocommerce_shipping_package_name', 10, 3 );</pre>
<hr />
<p>WooCommerce 實際用到的程式碼：includes/wc-cart-functions.php</p>
<pre>// @codingStandardsIgnoreStart
'package_name' =&gt; apply_filters( 'woocommerce_shipping_package_name', sprintf( _nx( 'Shipping', 'Shipping %d', ( $i + 1 ), 'shipping packages', 'woocommerce' ), ( $i + 1 ) ), $i, $package ),</pre>
<hr />
<p>我之前的解法是透過「Loco Translate」外掛來解決，去置換掉翻譯用的語言對應檔，但這個解法的問題在，官方更新了新的版本的語言對應檔之後，我們的設定值又會被蓋過去，變成每次更新完又要去重新修改一次，如果是修改 functions.php 就可以解決這個問題。</p>
<p>&nbsp;</p>
<h4>資料來源：</h4>
<p>woocommerce_shipping_package_name<br />
<a href="http://hookr.io/filters/woocommerce_shipping_package_name/">http://hookr.io/filters/woocommerce_shipping_package_name/</a></p>
<p>&nbsp;</p>
<p>我的 WooCommerce 網站：<br />
<a href="http://moca-shop.com/cart/">http://moca-shop.com/cart/</a></p>
<p>購物車截圖：<br />
<img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-948" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/07/Screenshot-2017-07-02-06.55.15.png" alt="" width="784" height="990" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/07/Screenshot-2017-07-02-06.55.15.png 784w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/07/Screenshot-2017-07-02-06.55.15-475x600.png 475w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/07/Screenshot-2017-07-02-06.55.15-768x970.png 768w" sizes="(max-width: 784px) 100vw, 784px" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2017/07/shipping_index/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress教學 &#8211; 讓 外掛 與 WordPress 產生互動</title>
		<link>https://stackoverflow.max-everyday.com/2017/05/wordpress-filter_woocommerce_valid_order_statuses_for_cancel/</link>
					<comments>https://stackoverflow.max-everyday.com/2017/05/wordpress-filter_woocommerce_valid_order_statuses_for_cancel/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 10 May 2017 08:41:58 +0000</pubDate>
				<category><![CDATA[WordPress筆記]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=685</guid>

					<description><![CDATA[我希望使用者可以有權限去取消 Pending 的...]]></description>
										<content:encoded><![CDATA[<p>我希望使用者可以有權限去取消 Pending 的 Order，</p>
<p>解法：</p>
<pre>// define the woocommerce_valid_order_statuses_for_cancel callback 
function filter_woocommerce_valid_order_statuses_for_cancel( $array ) { 
 // make filter magic happen here... 
 return array('on-hold', 'pending', 'failed' ) );
}; 
 
// add the filter 
add_filter( 'woocommerce_valid_order_statuses_for_cancel', 'filter_woocommerce_valid_order_statuses_for_cancel', 10, 1 );</pre>
<hr />
<h4>資料來源：</h4>
<p>woocommerce_valid_order_statuses_for_cancel<br />
The WordPress Core woocommerce valid order statuses for cancel hook.</p>
<p><a href="http://hookr.io/filters/woocommerce_valid_order_statuses_for_cancel/#">http://hookr.io/filters/woocommerce_valid_order_statuses_for_cancel/#</a></p>
<hr />
<p>Action and Filter Hook Reference</p>
<p><a href="https://docs.woocommerce.com/wc-apidocs/hook-docs.html">https://docs.woocommerce.com/wc-apidocs/hook-docs.html</a></p>
<hr />
<p>當要嫁接一個 Action 的時候，外掛會使用如下的函式來加入</p>
<div>
<pre id="highlighter_40171" class="syntaxhighlighter  php">add_action ( 'hook_name', 'your_function_name', [priority, [accepted_args]] );</pre>
</div>
<p>當要嫁接一個 Filter 的時候，外掛會使用如下的函式來加入</p>
<pre>add_filter ( 'hook_name', 'your_function_name', [priority, [accepted_args]] );</pre>
<p>你會發現兩者長相差不多，其各個參數的意義解釋如下<br />
hook_name =&gt; 欲嫁接點的 action /filter hook 名稱<br />
your_function_name =&gt; 所需要執行的函數名稱<br />
priority =&gt; 優先權，預設值為 10，越小的數字越先執行<br />
accepted_args =&gt; 你所執行的函數有幾個輸入值(參數)，預設為 1。某些 Action (Filter) 提供操過 2 個參數時即可以此設定。</p>
<p>那你會好奇在實際寫作的時候有多少得可以使用呢？<br />
我也不清楚，這一直因為 WordPress 的版本更新而變更者，可以前往官方網站的 Codex 中有詳細說明。<br />
<a href="http://codex.wordpress.org/Plugin_API/Action_Reference" target="_blank" rel="noopener noreferrer">Action 參考列表</a><br />
<a href="http://codex.wordpress.org/Plugin_API/Filter_Reference" target="_blank" rel="noopener noreferrer">Filter 參考列表</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2017/05/wordpress-filter_woocommerce_valid_order_statuses_for_cancel/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress教學 &#8211; 取得WooCommerce 可變商品單價最高金額</title>
		<link>https://stackoverflow.max-everyday.com/2017/05/wordpress-min-max-variation-price/</link>
					<comments>https://stackoverflow.max-everyday.com/2017/05/wordpress-min-max-variation-price/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Mon, 08 May 2017 16:41:12 +0000</pubDate>
				<category><![CDATA[WordPress筆記]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=683</guid>

					<description><![CDATA[在 single-product/price.p...]]></description>
										<content:encoded><![CDATA[<p>在 single-product/price.php 裡，想顯這示可變商品的最大值和最小值。<br />
Get the min or max variation (active) price.</p>
<p>參考看看這個範例：</p>
<pre>  <span id="1" class="T_PUBLIC">public</span><span id="2" class="T_WHITESPACE"> </span><span id="3" class="T_FUNCTION">function</span><span id="4" class="T_WHITESPACE"> </span><span id="5" class="T_STRING"><a title="get_variation_price" href="http://woocommerce.wp-a2z.org/oik_api/get_variation_price">get_variation_price</a></span>(<span id="7" class="T_WHITESPACE"> </span><span id="8" class="T_VARIABLE">$min_or_max</span><span id="9" class="T_WHITESPACE"> </span>=<span id="11" class="T_WHITESPACE"> </span><span id="12" class="T_CONSTANT_ENCAPSED_STRING">'min'</span>,<span id="14" class="T_WHITESPACE"> </span><span id="15" class="T_VARIABLE">$include_taxes</span><span id="16" class="T_WHITESPACE"> </span>=<span id="18" class="T_WHITESPACE"> </span><span id="19" class="T_STRING"><a title="false" href="http://woocommerce.wp-a2z.org/oik_api/false">false</a></span><span id="20" class="T_WHITESPACE"> </span>)<span id="22" class="T_WHITESPACE"> </span>{<span id="24" class="T_WHITESPACE">
    </span><span id="25" class="T_VARIABLE">$prices</span><span id="26" class="T_WHITESPACE"> </span>=<span id="28" class="T_WHITESPACE"> </span><span id="29" class="T_VARIABLE">$this</span><span id="30" class="T_OBJECT_OPERATOR">-&gt;</span><span id="31" class="T_STRING"><a title="get_variation_prices" href="http://woocommerce.wp-a2z.org/oik_api/WC_Product_Variable::get_variation_prices">get_variation_prices</a></span>(<span id="33" class="T_WHITESPACE"> </span><span id="34" class="T_VARIABLE">$include_taxes</span><span id="35" class="T_WHITESPACE"> </span>);<span id="38" class="T_WHITESPACE">
    </span><span id="39" class="T_VARIABLE">$price</span><span id="40" class="T_WHITESPACE">  </span>=<span id="42" class="T_WHITESPACE"> </span><span id="43" class="T_CONSTANT_ENCAPSED_STRING">'min'</span><span id="44" class="T_WHITESPACE"> </span><span id="45" class="T_IS_IDENTICAL">===</span><span id="46" class="T_WHITESPACE"> </span><span id="47" class="T_VARIABLE">$min_or_max</span><span id="48" class="T_WHITESPACE"> </span>?<span id="50" class="T_WHITESPACE"> </span><span id="51" class="T_STRING"><a title="PHP docs for: current" href="http://www.php.net/manual/en/function.current.php">current</a></span>(<span id="53" class="T_WHITESPACE"> </span><span id="54" class="T_VARIABLE">$prices</span>[<span id="56" class="T_CONSTANT_ENCAPSED_STRING">'price'</span>]<span id="58" class="T_WHITESPACE"> </span>)<span id="60" class="T_WHITESPACE"> </span>:<span id="62" class="T_WHITESPACE"> </span><span id="63" class="T_STRING"><a title="PHP docs for: end" href="http://www.php.net/manual/en/function.end.php">end</a></span>(<span id="65" class="T_WHITESPACE"> </span><span id="66" class="T_VARIABLE">$prices</span>[<span id="68" class="T_CONSTANT_ENCAPSED_STRING">'price'</span>]<span id="70" class="T_WHITESPACE"> </span>);<span id="73" class="T_WHITESPACE">
    </span><span id="74" class="T_RETURN">return</span><span id="75" class="T_WHITESPACE"> </span><span id="76" class="T_STRING"><a title="apply_filters" href="http://woocommerce.wp-a2z.org/oik_api/apply_filters">apply_filters</a></span>(<span id="78" class="T_WHITESPACE"> </span><span id="79" class="T_CONSTANT_ENCAPSED_STRING"><a class="apply_filters" title="woocommerce_get_variation_price – filter" href="http://woocommerce.wp-a2z.org/oik_hook/woocommerce_get_variation_price/">'woocommerce_get_variation_price'</a></span>,<span id="81" class="T_WHITESPACE"> </span><span id="82" class="T_VARIABLE">$price</span>,<span id="84" class="T_WHITESPACE"> </span><span id="85" class="T_VARIABLE">$this</span>,<span id="87" class="T_WHITESPACE"> </span><span id="88" class="T_VARIABLE">$min_or_max</span>,<span id="90" class="T_WHITESPACE"> </span><span id="91" class="T_VARIABLE">$include_taxes</span><span id="92" class="T_WHITESPACE"> </span>);<span id="95" class="T_WHITESPACE">
  </span>}</pre>
<hr />
<p>相關文章：</p>
<p>Class WC_Product_Variable</p>
<p><a href="https://docs.woocommerce.com/wc-apidocs/class-WC_Product_Variable.html">https://docs.woocommerce.com/wc-apidocs/class-WC_Product_Variable.html</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2017/05/wordpress-min-max-variation-price/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress教學 &#8211; WooCommerce product permalinks to id</title>
		<link>https://stackoverflow.max-everyday.com/2017/05/wordpress-woocommerce-product-permalinks-to-id/</link>
					<comments>https://stackoverflow.max-everyday.com/2017/05/wordpress-woocommerce-product-permalinks-to-id/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Fri, 05 May 2017 00:10:36 +0000</pubDate>
				<category><![CDATA[WordPress筆記]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=649</guid>

					<description><![CDATA[遇到「商品的預設網址是ID」 的需求修改好了。以...]]></description>
										<content:encoded><![CDATA[<p>遇到「商品的預設網址是ID」 的需求修改好了。以後不用自己去修改「固定網址」欄位。</p>
<p>WooCommerce預設的 Rule 如下：</p>
<hr />
<h2 id="section-2">Product Permalinks</h2>
<p>These settings control the permalinks used for products:</p>
<p><img decoding="async" class="alignnone size-full wp-image-651" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/05/2016-06-13-at-11-37.png" alt="" width="1148" height="336" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/05/2016-06-13-at-11-37.png 1148w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/05/2016-06-13-at-11-37-600x176.png 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/05/2016-06-13-at-11-37-768x225.png 768w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/05/2016-06-13-at-11-37-1024x300.png 1024w" sizes="(max-width: 1148px) 100vw, 1148px" /></p>
<p>&nbsp;</p>
<p>If you’re not using pretty permalinks, ‘default’ will be the only optional available to you and will use ID based URLs. e.g. <code>yourdomain.com/?product=111</code>. If you are using pretty permalinks, the default will be <code>yourdomain.com/product/product-name</code>.</p>
<p>The other options allow you to prepend the product permalinks with something custom, such as the shop page name, or a completely custom permalink you define.</p>
<p><strong>Please note: The product custom base should not conflict with the taxonomy permalink bases.</strong> If you set the product base to ‘shop’ for example, you should not set the product category base to ‘shop’ too as this will not be unique and will conflict. WordPress requires something unique so it can distinguish categories from products.</p>
<hr />
<p>The site that I am working on uses the following &#8220;pretty&#8221; permalink structure:</p>
<pre class="lang-php prettyprint prettyprinted"><code><span class="pln">http</span><span class="pun">:</span><span class="com">//example.com/blog/my-special-post</span></code></pre>
<p>But for a custom post type my client would like to avoid having a &#8220;pretty&#8221; slug:</p>
<pre class="lang-php prettyprint prettyprinted"><code><span class="pln">http</span><span class="pun">:</span><span class="com">//example.com/product/142</span></code></pre>
<p>How can the post ID be used in place of the slug for the custom post type?</p>
<p>I believe that this might be possible using WP_Rewrite, but I do not know where to begin.</p>
<hr />
<p>This is what I use to rewrite custom post type URLs with the post ID. You need a rewrite rule to translate URL requests, as well as a filter on <code>post_type_link</code> to return the correct URLs for any calls to <code>get_post_permalink()</code>:</p>
<pre class="lang-php prettyprint prettyprinted"><code><span class="pln">add_filter</span><span class="pun">(</span><span class="str">'post_type_link'</span><span class="pun">,</span> <span class="str">'wpse33551_post_type_link'</span><span class="pun">,</span> <span class="lit">1</span><span class="pun">,</span> <span class="lit">3</span><span class="pun">);</span>

<span class="kwd">function</span><span class="pln"> wpse33551_post_type_link</span><span class="pun">(</span><span class="pln"> $link</span><span class="pun">,</span><span class="pln"> $post </span><span class="pun">=</span> <span class="lit">0</span> <span class="pun">){</span>
    <span class="kwd">if</span> <span class="pun">(</span><span class="pln"> $post</span><span class="pun">-&gt;</span><span class="pln">post_type </span><span class="pun">==</span> <span class="str">'product'</span> <span class="pun">){</span>
        <span class="kwd">return</span><span class="pln"> home_url</span><span class="pun">(</span> <span class="str">'product/'</span> <span class="pun">.</span><span class="pln"> $post</span><span class="pun">-&gt;</span><span class="pln">ID </span><span class="pun">);</span>
    <span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span>
        <span class="kwd">return</span><span class="pln"> $link</span><span class="pun">;</span>
    <span class="pun">}</span>
<span class="pun">}</span><span class="pln">

add_action</span><span class="pun">(</span> <span class="str">'init'</span><span class="pun">,</span> <span class="str">'wpse33551_rewrites_init'</span> <span class="pun">);</span>

<span class="kwd">function</span><span class="pln"> wpse33551_rewrites_init</span><span class="pun">(){</span><span class="pln">
    add_rewrite_rule</span><span class="pun">(</span>
        <span class="str">'product/([0-9]+)?$'</span><span class="pun">,</span>
        <span class="str">'index.php?post_type=product&amp;p=$matches[1]'</span><span class="pun">,</span>
        <span class="str">'top'</span> <span class="pun">);</span>
<span class="pun">}</span></code></pre>
<hr />
<p>I&#8217;m looking for a way to put a Woocommerce product ID in a URL, e.g.:</p>
<pre class="default prettyprint prettyprinted"><code><span class="pln">domain</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">product</span><span class="pun">-</span><span class="pln">category</span><span class="pun">/</span><span class="pln">id</span><span class="pun">/</span><span class="pln">product</span><span class="pun">-</span><span class="pln">title</span></code></pre>
<p>The ID is the product&#8217;s post ID, e.g. &#8220;12092&#8221;. Automatically created by <code>WooCommerce</code> in <code>WordPress</code>.</p>
<p>Can this be done in a way? Either easily through the <code>WordPress</code> permalinks settings or in a different way through hacking my way into the files.</p>
<p>This nearly works:</p>
<pre class="default prettyprint prettyprinted"><code><span class="pun">&lt;?</span><span class="pln">php

add_filter</span><span class="pun">(</span><span class="str">'post_type_link'</span><span class="pun">,</span> <span class="str">'wpse33551_post_type_link'</span><span class="pun">,</span> <span class="lit">1</span><span class="pun">,</span> <span class="lit">3</span><span class="pun">);</span>

<span class="kwd">function</span><span class="pln"> wpse33551_post_type_link</span><span class="pun">(</span><span class="pln"> $link</span><span class="pun">,</span><span class="pln"> $post </span><span class="pun">=</span> <span class="lit">0</span> <span class="pun">){</span>
<span class="kwd">if</span> <span class="pun">(</span><span class="pln"> $post</span><span class="pun">-&gt;</span><span class="pln">post_type </span><span class="pun">==</span> <span class="str">'product'</span> <span class="pun">){</span>
    <span class="kwd">return</span><span class="pln"> home_url</span><span class="pun">(</span> <span class="str">'p/'</span> <span class="pun">.</span><span class="pln"> $post</span><span class="pun">-&gt;</span><span class="pln">ID </span><span class="pun">);</span>
<span class="pun">}</span> <span class="kwd">else</span> <span class="pun">{</span>
    <span class="kwd">return</span><span class="pln"> $link</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="pun">}</span><span class="pln">

add_action</span><span class="pun">(</span> <span class="str">'init'</span><span class="pun">,</span> <span class="str">'wpse33551_rewrites_init'</span> <span class="pun">);</span>

<span class="kwd">function</span><span class="pln"> wpse33551_rewrites_init</span><span class="pun">(){</span><span class="pln">
add_rewrite_rule</span><span class="pun">(</span>
    <span class="str">'product/([0-9]+)?$'</span><span class="pun">,</span>
    <span class="str">'index.php?post_type=product&amp;p=$matches[1]'</span><span class="pun">,</span>
    <span class="str">'top'</span> <span class="pun">);</span>
<span class="pun">}</span>

<span class="pun">?&gt;</span></code></pre>
<p>But the output is:</p>
<pre class="default prettyprint prettyprinted"><code><span class="pln"> domain</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">p</span><span class="pun">/</span><span class="lit">45</span></code></pre>
<p>I&#8217;d like it to be:</p>
<pre class="default prettyprint prettyprinted"><code><span class="pln"> domain</span><span class="pun">.</span><span class="pln">com</span><span class="pun">/</span><span class="pln">p</span><span class="pun">/</span><span class="lit">45</span><span class="pun">/</span><span class="pln">title</span><span class="pun">-</span><span class="pln">of</span><span class="pun">-</span><span class="pln">the</span><span class="pun">-</span><span class="pln">post</span></code></pre>
<hr />
<p>Use a more permissive regular expression to accept any url that ends with <code>/p/[id][anything]</code></p>
<pre class="default prettyprint prettyprinted"><code><span class="pun">&lt;?</span><span class="pln">php

add_filter</span><span class="pun">(</span><span class="str">'post_type_link'</span><span class="pun">,</span> <span class="str">'wpse33551_post_type_link'</span><span class="pun">,</span> <span class="lit">1</span><span class="pun">,</span> <span class="lit">3</span><span class="pun">);</span>
<span class="kwd">function</span><span class="pln"> wpse33551_post_type_link</span><span class="pun">(</span><span class="pln"> $link</span><span class="pun">,</span><span class="pln"> $post </span><span class="pun">=</span> <span class="lit">0</span> <span class="pun">){</span>
    <span class="kwd">if</span> <span class="pun">(</span><span class="pln"> $post</span><span class="pun">-&gt;</span><span class="pln">post_type </span><span class="pun">!=</span> <span class="str">'product'</span> <span class="pun">)</span>
      <span class="kwd">return</span><span class="pln"> $link</span><span class="pun">;</span>

    <span class="kwd">return</span><span class="pln"> home_url</span><span class="pun">(</span> <span class="str">'p/'</span> <span class="pun">.</span><span class="pln"> $post</span><span class="pun">-&gt;</span><span class="pln">ID </span><span class="pun">.</span> <span class="str">'/'</span> <span class="pun">.</span><span class="pln"> $post</span><span class="pun">-&gt;</span><span class="pln">post_name </span><span class="pun">);</span>
<span class="pun">}</span><span class="pln">

add_action</span><span class="pun">(</span> <span class="str">'init'</span><span class="pun">,</span> <span class="str">'wpse33551_rewrites_init'</span> <span class="pun">);</span>
<span class="kwd">function</span><span class="pln"> wpse33551_rewrites_init</span><span class="pun">(){</span><span class="pln">
    add_rewrite_rule</span><span class="pun">(</span>
        <span class="str">'p/([0-9]+)?.*?$'</span><span class="pun">,</span>
        <span class="str">'index.php?post_type=product&amp;p=$matches[1]'</span><span class="pun">,</span>
        <span class="str">'top'</span>
    <span class="pun">);</span>
<span class="pun">}</span>

<span class="pun">?&gt;</span></code></pre>
<p>You might need to &#8216;reset&#8217; your permalinks in the settings menu for WordPress to accept the new rule.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2017/05/wordpress-woocommerce-product-permalinks-to-id/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress教學 &#8211; 增加自己的Tab在WooCommerce商品裡</title>
		<link>https://stackoverflow.max-everyday.com/2017/04/wordpress-add-new-product-tab/</link>
					<comments>https://stackoverflow.max-everyday.com/2017/04/wordpress-add-new-product-tab/#comments</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Fri, 28 Apr 2017 21:01:01 +0000</pubDate>
				<category><![CDATA[WordPress筆記]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=586</guid>

					<description><![CDATA[如何自己增加新的頁籤在WooCommerce 商...]]></description>
										<content:encoded><![CDATA[<p>如何自己增加新的頁籤在WooCommerce 商品頁面，只要加入這3個 function 就完成了，好簡單。</p>
<p>有了這個功能，就可以不用自己「手動」去新增欄位名稱和欄位內容了。</p>
<p>執行畫面：</p>
<p><img decoding="async" class="alignnone wp-image-591 size-large" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-29-05.30.10-1024x589.jpg" alt="" width="740" height="426" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-29-05.30.10-1024x589.jpg 1024w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-29-05.30.10-640x368.jpg 640w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-29-05.30.10-768x442.jpg 768w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-29-05.30.10.jpg 1450w" sizes="(max-width: 740px) 100vw, 740px" /></p>
<hr />
<p>&nbsp;</p>
<p>Source code:</p>
<pre>function max_custom_tab_options_tab_buy_source() {
?&gt;
 &lt;li class="advanced_options advanced_tab" id="max_custom_tab_buy_source"&gt;&lt;a href=""&gt;&lt;?php _e('Buy Source', 'woocommerce'); ?&gt;&lt;/a&gt;&lt;/li&gt;
&lt;?php
}

function max_custom_tab_options_buy_source() {
?&gt;
&lt;script&gt;
 jQuery(document).ready(function(){
 jQuery('#max_custom_tab_buy_source').click(function(){
 jQuery('#max_buy_source_html_content_div_main').show();
 });
 });
&lt;/script&gt;

&lt;?php 
 global $product;
 global $post;

 $val= get_post_meta($post-&gt;ID,'buy_source', true); 
?&gt;
&lt;div id="max_buy_source_html_content_div_main" class="panel woocommerce_options_panel hidden" style="display:none;" &gt;
 &lt;div class="options_group hide_if_external"&gt;
 &lt;p class="form-field _purchase_note_field "&gt;
 &lt;label for="buy_source"&gt;&lt;?php _e('Buy Source', 'woocommerce'); ?&gt;&lt;/label&gt;
 &lt;textarea class="short" style="" name="buy_source" id="buy_source" placeholder="" rows="4" cols="20"&gt;&lt;?php echo isset($val)?$val:''; ?&gt;&lt;/textarea&gt;
 &lt;/p&gt; 
 &lt;/div&gt;
&lt;/div&gt;

&lt;?php 
}

function max_process_product_meta_custom_tab_buy_source( $post_id ) 
{
 $buy= $_POST['buy_source'];
 update_post_meta( $post_id, 'buy_source', $buy );
}

add_action('woocommerce_process_product_meta', 'max_process_product_meta_custom_tab_buy_source');
add_action('woocommerce_product_write_panel_tabs', 'max_custom_tab_options_tab_buy_source'); 
add_action('woocommerce_product_write_panels', 'max_custom_tab_options_buy_source');</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2017/04/wordpress-add-new-product-tab/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress教學 &#8211; 特定商品隱藏或增加物流方式</title>
		<link>https://stackoverflow.max-everyday.com/2017/04/wordpress-shipping-hide-rates/</link>
					<comments>https://stackoverflow.max-everyday.com/2017/04/wordpress-shipping-hide-rates/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Fri, 28 Apr 2017 00:48:24 +0000</pubDate>
				<category><![CDATA[WordPress筆記]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=576</guid>

					<description><![CDATA[有些商品用郵局寄送比用宅配便宜，所以較大的商品希...]]></description>
										<content:encoded><![CDATA[<p>有些商品用郵局寄送比用宅配便宜，所以較大的商品希望可以限制客人只能用郵局郵寄。修改後的畫面如下：</p>
<p><img loading="lazy" decoding="async" class="alignnone wp-image-581 size-full" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-09.31.04.png" alt="" width="794" height="874" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-09.31.04.png 794w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-09.31.04-581x640.png 581w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-09.31.04-768x845.png 768w" sizes="(max-width: 794px) 100vw, 794px" /></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>其他無大面積商品時：</p>
<p><img loading="lazy" decoding="async" class="alignnone wp-image-582 size-full" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-09.32.10.png" alt="" width="804" height="994" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-09.32.10.png 804w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-09.32.10-518x640.png 518w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-09.32.10-768x949.png 768w" sizes="(max-width: 804px) 100vw, 804px" /></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>設定方式，目前是「純手工」，在商品的編輯畫面裡增加metadata:</p>
<pre>shipping_post_only = 1</pre>
<p><img loading="lazy" decoding="async" class="alignnone wp-image-578 size-full" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-08.39.02.jpg" alt="" width="1482" height="978" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-08.39.02.jpg 1482w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-08.39.02-640x422.jpg 640w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-08.39.02-768x507.jpg 768w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-08.39.02-1024x676.jpg 1024w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-28-08.39.02-380x250.jpg 380w" sizes="(max-width: 1482px) 100vw, 1482px" /></p>
<p>純手工去加入欄位太不人性化，增加 3個副程式，就可以在 WooCommerce 裡增加新的Tab.<br />
WordPress教學 – 增加自己的Tab在WooCommerce商品裡<br />
<a href="https://stackoverflow.max-everyday.com/2017/04/wordpress-add-new-product-tab/">https://stackoverflow.max-everyday.com/2017/04/wordpress-add-new-product-tab/</a></p>
<p>&nbsp;</p>
<hr />
<p>我的 functions.php</p>
<pre>function dynamically_show_shipping_rates( $rates ) {
 $free = array();
 $is_shipping_post_only = 0;
 foreach (WC()-&gt;cart-&gt;get_cart() as $item_key =&gt; $item) {
 if ($item['data']-&gt;needs_shipping()) {
 $val = get_post_meta($item['product_id'], 'shipping_post_only', true);
 if ($val==1) {
 $is_shipping_post_only=1;
 break;
 }
 }
 }
 if ($is_shipping_post_only==1) {
 unset($rates['flat_rate:5']);
 unset($rates['flat_rate:3']);
 }
 return $rates;
}
add_filter( 'woocommerce_package_rates', 'dynamically_show_shipping_rates', 100 );</pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<hr />
<p>有沒有方法可以讓每一訂單金額大於指定數目,只能選用指定付款方式,如over$500 只能銀行轉帳,under 500 ,可以paypal/銀行轉帳.</p>
<p>解法：</p>
<pre>add_filter('woocommerce_available_payment_gateways', 'woocs_filter_gateways', 1);

function woocs_filter_gateways($gateway_list)
{
if (WC()-&gt;cart-&gt;subtotal &gt; 1000)
{
unset($gateway_list['cod']);
}

return $gateway_list;
}</pre>
<hr />
<p>動態增加貨運方式的方法：</p>
<pre>/**
 * Developers can add additional flat rates based on this one via this action since @version 2.4.
 *
 * Previously there were (overly complex) options to add additional rates however this was not user.
 * friendly and goes against what Flat Rate Shipping was originally intended for.
 *
 * This example shows how you can add an extra rate based on this flat rate via custom function:
 *
 * add_action( 'woocommerce_flat_rate_shipping_add_rate', 'add_another_custom_flat_rate', 10, 2 );
 *
 * function add_another_custom_flat_rate( $method, $rate ) {
 * $new_rate = $rate;
 * $new_rate['id'] .= ':' . 'custom_rate_name'; // Append a custom ID.
 * $new_rate['label'] = 'Rushed Shipping'; // Rename to 'Rushed Shipping'.
 * $new_rate['cost'] += 2; // Add $2 to the cost.
 *
 * // Add it to WC.
 * $method-&gt;add_rate( $new_rate );
 * }.
 */</pre>
<hr />
<p>作法二號：</p>
<pre>/**
 * Hide paid shipping rates when free shipping is available. Also remove the
 * 'Free Shipping with Signature' option when the order total &lt; 500.
 *
 * @param array $rates Array of rates found for the package.
 * @return array
 */
function dynamically_show_shipping_rates( $rates ) {
 $free = array();
 
 $cart_subtotal = WC()-&gt;cart-&gt;subtotal;
 
 // Only display the free option and pseudo-free option when free shipping is eligible
 foreach ( $rates as $rate_id =&gt; $rate ) {
 if ( 'free_shipping' === $rate-&gt;method_id || $rate_id === 'flat_rate:7' ) {
 $free[ $rate_id ] = $rate;
 }
 }
 
 // Always remove the pseudo-free rate from $rates; it will always be in $free array if
 // if free shipping is enabled, and that will always be returned below when appropriate
 unset($rates['flat_rate:7']);
 
 return $cart_subtotal &gt;= 500 ? $free : $rates;
}
add_filter( 'woocommerce_package_rates', 'dynamically_show_shipping_rates', 100 );</pre>
<p>&nbsp;</p>
<hr />
<p>官方文章：<br />
<a href="https://docs.woocommerce.com/document/hide-other-shipping-methods-when-free-shipping-is-available/">https://docs.woocommerce.com/document/hide-other-shipping-methods-when-free-shipping-is-available/</a></p>
<p>&nbsp;</p>
<p>相關文章：<br />
Create A WooCommerce Custom Shipping Method Plugin<br />
<a href="https://www.cloudways.com/blog/create-woocommerce-custom-shipping-method-plugin/">https://www.cloudways.com/blog/create-woocommerce-custom-shipping-method-plugin/</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2017/04/wordpress-shipping-hide-rates/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress除錯 &#8211; error_log()</title>
		<link>https://stackoverflow.max-everyday.com/2017/04/wordpress-error_log/</link>
					<comments>https://stackoverflow.max-everyday.com/2017/04/wordpress-error_log/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Fri, 21 Apr 2017 13:08:37 +0000</pubDate>
				<category><![CDATA[WordPress筆記]]></category>
		<category><![CDATA[Debug]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=560</guid>

					<description><![CDATA[在 trace 別人寫的plugin 時，發現別...]]></description>
										<content:encoded><![CDATA[<p>在 trace 別人寫的plugin 時，發現別人有把 exception 寫入到 log 裡，wordpress 的 error_log() 會寫入 debug.log 到 /wp-content/ 目錄裡。</p>
<hr />
<p>但我沒有這一個檔案，所以要先去設定<a title="Editing wp-config.php" href="https://codex.wordpress.org/Editing_wp-config.php">wp-config.php</a>：</p>
<p><a href="https://codex.wordpress.org/Debugging_in_WordPress">https://codex.wordpress.org/Debugging_in_WordPress</a></p>
<h2><span id="Example_wp-config.php_for_Debugging" class="mw-headline">Example wp-config.php for Debugging</span></h2>
<p>The following code, inserted in your <a title="Editing wp-config.php" href="https://codex.wordpress.org/Editing_wp-config.php">wp-config.php</a> file, will log all errors, notices, and warnings to a file called debug.log in the wp-content directory. It will also hide the errors so they do not interrupt page generation.</p>
<pre> // Enable WP_DEBUG mode
define( 'WP_DEBUG', true );

// Enable Debug logging to the /wp-content/debug.log file
define( 'WP_DEBUG_LOG', true );

// Disable display of errors and warnings 
define( 'WP_DEBUG_DISPLAY', false );
@ini_set( 'display_errors', 0 );

// Use dev versions of core JS and CSS files (only needed if you are modifying these core files)
define( 'SCRIPT_DEBUG', true );
</pre>
<p>NOTE: You must insert this <b>BEFORE</b> <tt>/* That's all, stop editing! Happy blogging. */</tt> in the <a title="Editing wp-config.php" href="https://codex.wordpress.org/Editing_wp-config.php">wp-config.php</a> file</p>
<hr />
<p>接下來終於看到 Error message:</p>
<p>[21-Apr-2017 10:45:17 UTC] WordPress 資料庫錯誤：Column &#8216;user_id&#8217; cannot be null 由指令 INSERT INTO `wp_apsl_users_social_profile_details` (`user_id`, `provider_name`, `identifier`, `unique_verifier`, `email`, `first_name`, `last_name`, `profile_url`, `photo_url`, `display_name`, `description`, `gender`) VALUES (NULL, &#8216;facebook&#8217;, &#8216;1387607167994&#8217;, &#8216;df8c6ac9b4084ab507ea54f47ed829f26ba1&#8217;, &#8216;qoo@yahoo.com.tw&#8217;, &#8216;xx&#8217;, &#8216;x&#8217;, &#8216;https://www.facebook.com/app_scoped_user_id/1387607167994/&#8217;, &#8216;https://fb-s-a-a.akamaihd.net/h-ak-xap1/v/t1.0-1/c0.13.50.50/p50x50/n.jpg&#8217;, &#8216;xx x&#8217;, &#8221;, &#8216;female&#8217;) 引發，錯誤來自 require(&#8216;wp-load.php&#8217;), require_once(&#8216;wp-config.php&#8217;), require_once(&#8216;wp-settings.php&#8217;), do_action(&#8216;init&#8217;), WP_Hook-&gt;do_action, WP_Hook-&gt;apply_filters, APSL_Lite_Class-&gt;login_check, include(&#8216;/plugins/accesspress-social-login-lite/inc/frontend/login_check.php&#8217;), APSL_Lite_Login_Check_Class-&gt;__construct, APSL_Lite_Login_Check_Class-&gt;onFacebookLogin, APSL_Lite_Login_Check_Class::UpdateUserMeta</p>
<p>&nbsp;</p>
<p>原來是 username 卡關，多增加下面的檢查即可：</p>
<hr />
<p>先用這一個 <span class="nf">validate_username</span><span class="p">(</span> <span class="nv">$username</span> <span class="p">) 來檢查 username. </span></p>
<pre class="php hljs "><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">validate_username</span><span class="hljs-params">( <span class="hljs-variable">$username</span> )</span> {</span>
	<span class="hljs-variable">$sanitized</span> = sanitize_user( <span class="hljs-variable">$username</span>, <span class="hljs-keyword">true</span> );
	<span class="hljs-variable">$valid</span> = ( <span class="hljs-variable">$sanitized</span> == <span class="hljs-variable">$username</span> &amp;&amp; ! <span class="hljs-keyword">empty</span>( <span class="hljs-variable">$sanitized</span> ) );

	<span class="hljs-comment">/**
	 * Filters whether the provided username is valid or not.
	 *
	 *<span class="hljs-phpdoc"> @since</span> 2.0.1
	 *
	 *<span class="hljs-phpdoc"> @param</span> bool   $valid    Whether given username is valid.
	 *<span class="hljs-phpdoc"> @param</span> string $username Username to check.
	 */</span>
	<span class="hljs-keyword">return</span> apply_filters( <span class="hljs-string">'validate_username'</span>, <span class="hljs-variable">$valid</span>, <span class="hljs-variable">$username</span> );
}</pre>
<p>&nbsp;</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2017/04/wordpress-error_log/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress教學 – 可變商品價格批次更新</title>
		<link>https://stackoverflow.max-everyday.com/2017/04/wordpress-variation-price/</link>
					<comments>https://stackoverflow.max-everyday.com/2017/04/wordpress-variation-price/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Tue, 18 Apr 2017 10:12:20 +0000</pubDate>
				<category><![CDATA[WordPress筆記]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=521</guid>

					<description><![CDATA[我有一個商品，用款式x尺寸x顏色下去組合，會組合...]]></description>
										<content:encoded><![CDATA[<p>我有一個商品，用款式x尺寸x顏色下去組合，會組合出120種變化，WooCommerce 一次自動產生組合，可以變化出50種，所以按3次組合，就可以全部組合完成。</p>
<p>接著，不可能一筆一筆去設規格品的價格，還好 WooCommerce 很貼心提供批次更新的功能：</p>
<p><img loading="lazy" decoding="async" class="alignnone wp-image-524 size-full" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-18-17.37.07.jpg" alt="" width="1200" height="993" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-18-17.37.07.jpg 1200w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-18-17.37.07-640x530.jpg 640w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-18-17.37.07-768x636.jpg 768w, https://stackoverflow.max-everyday.com/wp-content/uploads/2017/04/Screenshot-2017-04-18-17.37.07-1024x847.jpg 1024w" sizes="(max-width: 1200px) 100vw, 1200px" /></p>
<p>&nbsp;</p>
<p><strong>資料來源：</strong><br />
Apply the price of a WooCommerce product variation to all variations of the same product<br />
<a href="https://www.gowp.com/blog/apply-the-price-of-a-woocommerce-product-variation-to-all-variations-of-the-same-product/">https://www.gowp.com/blog/apply-the-price-of-a-woocommerce-product-variation-to-all-variations-of-the-same-product/</a></p>
<p>If you have a <a href="http://www.woothemes.com/woocommerce/" target="_blank">WooCommerce</a> product with a lot of variations that are all the same price, entering/updating the price can be a little frustrating as you must update the price manually for each variation. For a product with three colour and size attributes this is only nine total variations, but if you could update the price for all nine with one click, surely you would? With a little bit of JavaScript added via an <a href="http://codex.wordpress.org/Plugin_API/Hooks" target="_blank">action hook</a>, you can.<span id="more-1309"></span></p>
<p><strong>UPDATE: </strong>As it turns out, WooCommerce has added built-in support for the bulk editing of variations. While editing your product, after having added one or more variations, the “Add Variation” drop-down at the top of the Variations tab also includes a number of bulk edit options; you can set both regular and sale prices or modify either, increasing or decreasing by a percentage or set amount, for example.</p>
<div id="attachment_1435" class="wp-caption aligncenter">
<p><img loading="lazy" decoding="async" class="size-full wp-image-1435" src="https://www.gowp.com/wp-content/uploads/ss-wc-variations-bulk.png" alt="Variation bulk editing in WooCommerce" width="692" height="359" /></p>
<p class="wp-caption-text">Variation bulk editing in WooCommerce</p>
</div>
<p>Assuming you’re not using a very old version of WooCommerce, you can use the above and ignore the rest of this article.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2017/04/wordpress-variation-price/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>WordPress教學 – eMail 設定</title>
		<link>https://stackoverflow.max-everyday.com/2017/04/wordpress-email/</link>
					<comments>https://stackoverflow.max-everyday.com/2017/04/wordpress-email/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Mon, 17 Apr 2017 16:26:21 +0000</pubDate>
				<category><![CDATA[WordPress筆記]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[WooCommerce]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=516</guid>

					<description><![CDATA[在WordPress 裡寄送Email的問題，讓...]]></description>
										<content:encoded><![CDATA[<p>在WordPress 裡寄送Email的問題，讓我在程式裡卡了很久。實在是進退二難，如果最後弄不出來就用Ajax來解決，但Ajax又怕跟Server的連線還沒完成,User Redirect，不能確定這個情況下 email 有沒有被正確的寄出去。最後是用了  WooCommerce 3.0 + Postman SMTP Email Plugin 解決我在沒有multi-thread 的 php 架構裡，寄送 Email 造成 UI 會卡住的問題。</p>
<p>要記得把寄信給管理者的那個寄信用的Email，在信箱的設定裡加入為「聯絡人」，不然都要到「廣告信」裡去找Email.</p>
<p>資料來源：<a href="https://docs.woocommerce.com/document/email-faq/">https://docs.woocommerce.com/document/email-faq/</a></p>
<hr />
<p>WooCommerce, and most plugins sending email, send mail by using the <a href="http://codex.wordpress.org/Function_Reference/wp_mail">wp_mail()</a> function that is a core function of WordPress.</p>
<p>In most cases, if email is not being sent/received, then the issue is not with WooCommerce itself, but with the core email function on your web host. See the “How does email work with WordPress?” section below.</p>
<h2 id="section-1">I am getting orders, but no emails are sending!</h2>
<p>There are multiple factors that can be the cause. Before explaining the actual mail side of the issue, please ensure that it’s not one described below.</p>
<h3 id="section-2">Check for Pending Orders</h3>
<ul>
<li>Your orders are pending. Pending orders are orders where the customer clicked “Place Order” but abandoned the payment page (depending on the payment gateway, i.e., PayPal) or had their credit card transaction declined. If you have pending orders, these orders will not generate emails.</li>
<li>You have pending orders but receive payment for them via a payment gateway such as PayPal, which requires a notice to update order status on your site. In this case, you are not looking at a email issue; you are looking at an issue with your payment gateway and may need to <a href="http://woothemes.zendesk.com/requests/new">submit a support ticket</a>.</li>
</ul>
<h3 id="section-3">Check your Email Settings</h3>
<p>Orders that are properly updating to Processing should generate an email. Another possible issue is having mistakenly disabled emails from sending. Double-check that “Enable this email notification” is ticked for order notifications. This can be found under <strong>WooCommerce &gt; Settings &gt; Email</strong> and click on the “processing order” email template. An additional test should be setting the Email Type to Multipart.</p>
<p><a href="https://docs.woocommerce.com/wp-content/uploads/2013/05/screen-shot-2016-07-21-at-3-37-30-pm.png?w=649" rel="prettyPhoto"><img loading="lazy" decoding="async" class="aligncenter size-large wp-image-162967" src="https://docs.woocommerce.com/wp-content/uploads/2013/05/screen-shot-2016-07-21-at-3-37-30-pm.png?w=649" alt="Screen Shot 2016-07-21 at 3.37.30 PM" width="649" height="528" /></a></p>
<h3 id="section-4">Your emails are broken</h3>
<p>If emails are enabled and orders are updating to Processing, then there is most likely an issue in the email delivery to a recipient. This could be a spam blocker that classified your emails are spam and stopped them. The best way to resolve this is to sign up for an account with a dedicated SMTP provider, and an average site can usually stay within free usage accounts that are available.</p>
<h2 id="section-5">How does email work?</h2>
<p>Comparing a path an email takes to reach its destination vs. driving a car on a road trip, emails do not travel directly from point A to point B. The final destination is more like point W, and the email is bouncing from one server to another at the speed of light while being filtered at each step.</p>
<p>Google has put together this representation of the path an email follows. This story is specific to Gmail and delivering to a mobile device but helps explain how email works. <a href="http://www.google.com/green/storyofsend/desktop/">Learn more</a>.</p>
<h2 id="section-6">How does email work with WordPress?</h2>
<h3 id="section-7">wp_mail() function</h3>
<p>WooCommerce, and most plugins sending email, send mail by using the <a href="http://codex.wordpress.org/Function_Reference/wp_mail">wp_mail()</a> function that is a core function of WordPress.</p>
<h3 id="section-8">Does that mean WordPress sends my email? Is WooCommerce sending the email?</h3>
<p>Neither are sending the email. What happens is WooCommerce calls the wp_mail() function, which then requests WordPress to send the email. Because WordPress is not an email server, it typically asks PHP to send the email for it. PHP then checks for a local email server within the web server and tells that email server to send the email. Your email passes three steps before reaching your web server.</p>
<p>As of WooCommerce 3.0.0, a delayed CRON event is used to send emails.</p>
<h3 id="section-9">If email is passed to my web server to be sent, how does a dedicated SMTP provider fit in?</h3>
<p>By using a plugin the SMTP provider has available or the <a href="https://wordpress.org/plugins/postman-smtp/" target="_blank">Postman SMTP</a> plugin, the wp_mail() function reroutes the email from PHP to your SMTP provider. From there, the SMTP provider receives the request and adds your email to a queue to be sent.</p>
<h2 id="section-10">What is a dedicated SMTP provider? Is that like my Gmail account?</h2>
<p>A dedicated SMTP provider is like a web host in the sense that you have an account and use their servers. The difference is instead of hosting websites, you use their servers to send emails. Gmail is an email provider where you send and receive emails; a dedicated SMTP provider is similar to having half of Gmail, where you only send emails and not receive them.</p>
<h3 id="section-11">Can I use Gmail as my SMTP provider?</h3>
<p>Yes, but it is not ideal. Gmail will disable your account if you send emails to more than 500 unique recipients in an 24-hour period. This includes emails you send yourself and all emails your website is sending. <a href="https://support.google.com/mail/answer/22839?hl=en">Learn more</a>.</p>
<h2 id="section-12">Suggested Dedicated SMTP Providers</h2>
<p>There are dedicated SMTP providers that will work for most websites. All have their own plugin on WordPress.org and can be installed from your WordPress dashboard, and have support available to help you get started.</p>
<ul>
<li><a href="http://sendgrid.com/">SendGrid</a> (<a href="https://wordpress.org/plugins/sendgrid-email-delivery-simplified/">Plugin</a>) – Send 12,000 emails free per month. （這個規則改了，沒有這個好康）</li>
<li><a href="http://www.mailgun.com/">Mailgun</a> (<a href="http://wordpress.org/plugins/mailgun/">Plugin</a>) – Send 10,000 emails free per month.</li>
<li><a href="https://www.sparkpost.com/">SparkPost</a> (<a href="https://wordpress.org/plugins/sparkpost/">Plugin</a>) – Send 100,000 emails free per month. （這個顯示我所屬地區不能申請，可能需要用VPN連去米國試看看）</li>
<li><a href="http://mandrill.com/">Mandrill</a> (<a href="http://wordpress.org/plugins/wpmandrill/">Plugin</a>) – From <a href="http://mailchimp.com/">MailChimp</a> as a paid add-on. It costs $10/month for up to 25,000 emails. Emails are tracked and tagged for stats in the Mandrill Dashboard, and it can be used with MailChimp.</li>
</ul>
<h2 id="section-13">If emails are being blocked by spam filters, why is it not in my spam folder?</h2>
<p>The spam filter is the last and final spam filter your email is filtered through, at which point it is going to spam based on your email client settings or how you mark other emails as spam. If your emails are denied by another spam filter before that, it simply is not delivered.</p>
<h2 id="section-14">My contact form emails work, so why do WooCommerce emails get blocked?</h2>
<p>Without deep investigation into server logs and tracking exact email paths, this is not a simple question to answer.</p>
<ul>
<li>The short version is there is a lot more to spam filters than scanning for Nigerian Princes and typical spam. Spam filters check the IP address of origin, the sending user and domain, the amount of email that IP/sender has sent, how many times emails from that sender have been marked as spam, and the wording of emails.</li>
<li>The most common factor is where the email originates, which brings your overall score with spam filters down low enough that even minor differences in wording and formatting of WooCommerce emails may be flagged as spam and not sent.</li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2017/04/wordpress-email/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
